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
Addition of two numbers in Java using method
5 method to find sum of two numbers
JavaScript program to add two numbers
Suggested for you
do-while loop in Java language
Data type and variable in Java language
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…