Turn Desktop View Off
by Dinesh Thakur

Algorithm for Java Sum of Digits :

 

step 1: Set sum=0

step 2: Read num

step 3: Repeat through step-5 while num greater than 0

step 4: temp=num % 10

step 5: sum=sum+temp

step 6: num=num/10

step 7: Print sum

step 8: Exit

Here is the Java Example for Java Sum of Digits:

import java.util.Scanner;

public class SumofDigits
{
             public static void main(String args[])
         {
               int r,sum=0,num;
             Scanner sl=new Scanner(System.in);
             System.out.print("Enter A Number : ");
             num =sl.nextInt();
             while(num >0)
                   {
                     r=num %10;
                     sum=sum+r;
                     num =num/10;
                   }
                     System.out.println("The Sum of Digit is : "+ sum);
          }
}

 Java Sum of Digits