sum of digit java program:

Step 1: Get the modulus/remainder of the number.

Step 2: sum the remainder of the number.

Step 3: Divide the number by 10.

Step 4: Repeat the step 2 while number is greater than 0.

Example program:

public class Sum of digit
{
public static void main(String[] args)
{
int box =1234;
int total= 0;
while(box>0)
{
int rem = box%10;
total = total+rem;
box = box/10;
}
System.out.println(total);

}

}

output:

Leave a Comment