Java program for sum of digits of number

In this post, we will see how to find sum of digits of number in java.
You can find unit’s place digit by number%10, add it to the total and divide the number by 10 to remove the unit’s place.

import java.util.Scanner;

public class DigitAdditionCalc
{
    public static void main(String args[])
    {
        int number, remdr=0, total=0, tempNum;
        Scanner scan = new Scanner(System.in);

        System.out.print("Enter a Number : ");
        number = scan.nextInt();

        tempNum = number;

        while(number>0)
        {
            remdr = number%10;
            total = total+remdr;
            number = number/10;
        }

        System.out.print("Sum of digits of number " +tempNum+ " is " +total);        
    }
}

Output:

Enter a Number : 5385
Sum of digits of number 5385 is 21

That’s all about Java program to add digits of number.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.