//Java program finding the sum of the digits of a number
SumOfDigitsofaNumberDemo.java
public class SumOfDigitsofaNumberDemo
{
public static void main(String[] args)
{
int originalNumber=2345;
int rem=0;
int number = originalNumber;
int sum = 0;
//Find sum of the digits of the number
while(number > 0){
rem = number % 10;
number = number / 10;
sum = sum + rem;
}
System.out.println("Sum of the Digits of the "+originalNumber+ " is "+sum);
}
}
o/p
Sum of the Digits of 2345 is 14.
Dry Run
/*
* rem = 2345 % 10 = 5 rem stores remainder
* number = 2345 / 10 = 234 number stores quotient the remaining number
*
* rem number sum=sum+rem
* 5 234 0+5= 5
* 4 23 5+4= 9
* 3 2 9+3= 12
* 2 0 12+2 = 14
*
* */

