Java Program to Find Sum of All Digits

You are currently viewing Java Program to Find Sum of All Digits

Java program to find sum of all digits

import java.util.Scanner;

public class AddDigits
{
    public static void main(String args[])
    {
    	// initializing and declaring the objects.
        int num, rem=0, sum=0, temp;
        Scanner scan = new Scanner(System.in);
		
        // enter number here.
        System.out.print("Enter the Number : ");
        num = scan.nextInt();
		
        // temp is to store number.
        temp = num;
		
        while(num>0)
        {
            rem = num%10;
            sum = sum+rem;
            num = num/10;
        }
		
        System.out.print("Sum of Digits of " +temp+ " is : " +sum);        
    }
}

Output

First run:
Enter the Number : 582
Sum of Digits of 582 is : 15

Second run:
Enter the Number : 256868
Sum of Digits of 256868 is : 35

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply