Java program to compute the sum of digits in a given numbers
- Home
- Calculations
- Java program to compute the sum of digits in a given numbers
- On
- By
- 0 Comment
- Categories: Calculations, Find elements
Java program to compute the sum of digits in a given numbers
Java program to compute the sum of digits in a given numbers
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 to find the sum of digits
Calculate the sum of digits using the for loop
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
Calculate the sum of digits using the while loop
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
Calculate the sum of digits using the do-while loop
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
- Declare the variable num,count and digits as long type.
- Declare and initialize variable sum to Zero.
- Receive input from the user for the value to store the variable num and to find the sum of digits.
- Using a loop (for loop , while loop, do-while loop) get each digit of the number and add it to the to a variable to find the sum.
- Display the sum of the digits of the number.
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