You are here: Home / Topics / Program to Sum the digit of any given number in Java

Program to Sum the digit of any given number in Java

Filed under: Java on 2023-08-18 06:56:11

// Sum of digits of given number

public class SumOfDigits 
{   
    public static void main(String[] args) 
    {

       int i=521;
       
       int sum=0;
       while(i>0)
       {
              int r=i%10;
               sum=sum+r;
               i=i/10;       
       }
        System.out.println("Sum of digits of 521 is "+sum);         
   }
}


Output:

Sum of digits of 521 is 8


About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.