You are on page 1of 1

Add Digits of Number in Java

To add digits of any number in Java programming, you have to ask to the user to enter the
number to add their digits and display the summation of digits of that number.

Java Programming Code to Add Digits of Number

Following Java program ask to the user to enter the number to add their digits and will
display the addition result :

/* Java Program Example - Add Digits of Number */

import java.util.Scanner;

public class JavaProgram


{
public static void main(String args[])
{
int num, rem=0, sum=0, temp;
Scanner scan = new Scanner(System.in);

System.out.print("Enter a Number : ");


num = scan.nextInt();

temp = num;

while(num>0)
{
rem = num%10;
sum = sum+rem;
num = num/10;
}

System.out.print("Sum of Digits of " +temp+ " is " +sum);


}
}

When the above Java Program is compile and executed, it will produce the following output:

You might also like