ComputerShikshak.com

Java - Sum of Digits of a Number

Problem Statement

Write a program in Java to find the sum of the digits present in an integer number entered by the user.

Brief Description

According to the problem statement we have to write a program in Java that will take an integer value as input from the user through the keyboard and will count the total number of digits present in that number.

For example, if the user enters 132 as input, the program will generate 3 as output since the number 132 contains 3 digits.

Program

				
					import java.util.*;
class SumDigits
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int num,temp,rem,sum=0;
        System.out.print("Enter a number: ");
        num=sc.nextInt();
        temp=Math.abs(num);
        while(temp!=0)
        {
            rem=temp%10;
            sum=sum+rem;    //OR    sum+=rem;
            temp=temp/10;    //OR    temp/=10;
        }
        System.out.print("Sum of the digits present in "+num+" is "+sum);
    }
}
				
			

Output

Enter a number: 132
Sum of the digits present in 132 is 6

Explanation

In this program:
  • 4 integer variable namely num, temp, rem and sum are declared.
  • Initially 0 is stored in sum.
  • The integer value entered by the user is stored in num.
  • The absolute value corresponding to the entered value is stored in temp.
The step-by-step working of the while loop (when num and temp both contains 132) is shown in the table given below:
numtemp!=0rem=temp%10;remsum=sum+rem;sumtemp=temp/10;tempComment
132----0-132Initial Values
132132!=0
↓
true
rem=132%10;2sum=0+2;2temp=132/10;13Iteration 1
13213!=0
↓
true
rem=13%10;3sum=2+3;5temp=13/10;1Iteration 2
1321!=0
↓
true
rem=1%10;1sum=5+1;6temp=1/10;0Iteration 3
1320!=0
↓
false
------While
loop
stops
working
as
temp!=0
is
evaluated
to
false
While executing the while loop:
  • At first value of temp is checked to see whether it is 0 or not.
  • The condition temp!=0 will produce either true or false after evaluation.
    • If it is true, the statements written within while loop are executed and the loop continues to iterate till the condition is true.
    • If it is false then the statements written within while loop are not executed and the control goes to the statement written immediately after the loop.
  • Within while loop, at first the last digit is extracted from the value stored in temp by performing modulo division operation using the expression temp%10. Thus the expression temp%10 will give the remainder after integer division which is the last digit of the value stored in temp and the result is stored in rem. 

  • After that the value stored in rem is added with the value stored in sum and the result is stored in the same variable i.e. sum. 

  • The value stored in temp is divided by 10 to remove the last digit of the value stored in it and the result is stored in the same variable i.e. temp.

Thus the above process is used to find the sum of the digits present in the given number.

The final result is stored in sum. 

Finally the sum of the digits present in the entered integer value along with the value is displayed on the output screen.

Share this page on
Scroll to Top