In this tutorial, we will discuss a concept of Java program to compute the sum of digits in a given numbers
In the Java programming language, we can use for loop ,while loop and do-while loop to compute the sum of digits in a given numbers
In this article, we are going to learn how to find the sum of digits in a given numbers
Program 1
import java.util.Scanner; class SumOfDigits1{ public static void main(String args[]){ long count,sum=0,digits; Scanner scan=new Scanner(System.in); //create a scanner object for input //get input from the user for rows System.out.print("Enter the number for sum: "); long num=scan.nextInt(); count=num; for(count=num; num>0; num/=10) { digits=num%10; sum=sum+digits; } System.out.print("Given number is: " + count); System.out.print("\nSum of the digits: " + sum); } }
When the above code is executed, it produces the following results
Case 1
Enter the number for sum:1100 Given number is: 1100 Sum of the digits:2
Case 2
Enter the number for sum:3456 Given number is: 3456 Sum of the digits:18
Program 2
import java.util.Scanner; class SumOfDigits{ public static void main(String args[]){ long count,sum=0,digits; Scanner scan=new Scanner(System.in); //create a scanner object for input //get input from the user for rows System.out.print("Enter the number for num: "); long num=scan.nextInt(); count=num; while(num>0) { digits=num%10; sum=sum+digits; num/=10; } System.out.print("Given number is: " + count); System.out.print("\nSum of the digits: " + sum); } }
When the above code is executed, it produces the following results
case 1
Enter the number:1200 Given number is: 1200 Sum of the digits:3
case 2
Enter the number:1468 Given number is: 1468 Sum of the digits:19
Program 3
import java.util.Scanner; class SumOfDigits2{ public static void main(String args[]){ long count,sum=0,digits; Scanner scan=new Scanner(System.in); //create a scanner object for input //get input from the user for rows System.out.print("Enter the number for num: "); long num=scan.nextInt(); count=num; do{ digits=num%10; sum=sum+digits; num/=10; } while(num>0); System.out.print("Given number is: " + count); System.out.print("\nSum of the digits: " + sum); } }
When the above code is executed, it produces the following results
case 1
Enter the number for num: 1111 Given number is:1111 Sum of the digits:4
case 2
Enter the number for num: 9876 Given number is:9876 Sum of the digits:30
Methods
Similar post
C program to compute the sum of digits in a given numbers
C++ program to compute the sum of digits in a given numbers
Python program to compute the sum of digits in a given numbers
Suggested for you
do-while loop in Java language
Loops in Java language
Data type and variable in Java language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…