by Dinesh Thakur Category: Control Structures

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.out.print("Enter A Number : ");
             num =sl.nextInt();
             while(num >0)
                     r=num %10;
                     num =num/10;
                     System.out.println("The Sum of Digit is : "+ sum);

 Java Sum of Digits

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles