Enter any integer number as input. After that we use modulus and division operation respectively to find the sum of digits of number as output.
Here is the source code of the Java Program to Compute the Sum of Digits in a given Integer. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
public class Digit_Sum
public static void main(String args)
int m, n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
m = s.nextInt();
while(m > 0)
n = m % 10;
sum = sum + n;
m = m / 10;
System.out.println("Sum of Digits:"+sum);
$ javac Digit_Sum.java $ java Digit_Sum Enter the number:456 Sum of Digits:15
Sanfoundry Global Education & Learning Series – 1000 Java Programs.