Sum of Digits of a Number in Java

In this post, we will write a program to print the sum of all the digits of a given number in Java. We will find the sum of digits of a number in Java using for loop. The while loop also can be used for this purpose. We can also develop a Java program to calculate the sum of digits without using any loop.

Program description:- Write a program to print the sum of all the digits of a given number in Java.

Example of the sum of digits of an integer number:- 54321 = 5+4+3+2+1 = 15

Procedure to find the sum of digits of a given integer number,

  1. Take a number
  2. Declare two variables:- lastDigit and sum
  3. Initialize the sum variable with 0
  4. Find the last digit of the number, lastDigit = number%10
  5. Add the value of lastDigit to the variable sum
  6. Remove the last digit of the number. number = number/10
  7. Repeat 4 to 6 steps until the number does not become 0

Using while loop

import java.util.Scanner;

public class SumOfDigitsProgram {

   public static int digitSum(int number) {
      // declare variables
      int lastDigit = 0;
      int sum = 0;

      // loop to repeat the process
      while(number != 0) {

         // find last digit
         lastDigit = number % 10;

         // add last digit to sum
         sum = sum + lastDigit;

         // remove last digit
         number = number / 10;
      }

      // sum value is the sum of digits
      // of the given number
      return sum;
  }

  public static void main(String[] args) {
      // declare variables
      int number = 0;
      int sumOfDigits = 0;

      // create Scanner class object 
      // for reading the values
      Scanner scan =  new Scanner(System.in);

      // read input
      System.out.print("Enter an integer number::");
      number = scan.nextInt();

      // find sum of digits of number
      sumOfDigits = digitSum(number);

      // display result
      System.out.println("The sum of digits of" +
         " the number "+number+" = "+sumOfDigits);

      // close Scanner class object
      scan.close();
  }
}

The output for the different test-cases are:-

Enter an integer number:: 12345
The sum of digits of the number 12345 = 15

Enter an integer number:: 95423
The sum of digits of the number 95423 = 23

In this Java program, we have used a while loop to find the sum of digits of a given integer number. While loop is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated.

We can find the last digit of the number and then add it to the sum variable using one line. It will reduce the number of line of the method which is using while loop.

public static int digitSum(int number) {
     // declare variables
     int sum = 0;

     // loop to repeat the process
     while(number != 0) {
         // find last digit and
         // add it to sum
         sum = sum + number % 10;

         // remove last digit
         number = number / 10;
     }

     // sum value is the sum of digits
     // of the given number
     return sum;
}

Instead of using the while loop, we can also use for loop or do-while loop to find the sum of digits of a given integer number in java. The for loop is also a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed.

Sum of Digits of a Number in Java using for loop

public static int digitSum(int number) {

   // declare variables
   int sum;

   // loop to repeat the process
   for(sum = 0; number!=0; number/=10)
   // find last digit & add to sum
   sum = sum + number % 10;

   // sum value is the sum of digits
   // of the given number
   return sum;
}

we can write more than one update expression inside for loop. So in the above program, we can reduce the number of lines in the for loop. Don’t forget to add semicolon (;) at the end of the for loop.

public static int digitSum(int number){
  int sum;
  for(sum=0; number!=0; sum+=(number%10), number/=10);
  return sum;
}

It can be also written as,

public static int digitSum(int number) {
  for(int sum=0; ; sum+=(number%10), number/=10)
  if(number==0) return sum;
}

Sum of digits in Java without using the loop

We can also find the sum of digits in java without using any loop. For this purpose, we need to take the help of recursion technique. A method that calls itself is called the recursive method. Recursion is one of the ways to find a solution to the problem.

In the below program, we developed a recursive method digitSum(), this method finds the sum of the digits of a given number by using the recursion technique.

import java.util.Scanner;

public class SumOfDigitsProgram {

   public static int digitSum(int n) {
      if(n==0) return 0; // base case
      else // general case
      return ( (n%10) + digitSum(n/10) );
   }

   public static void main(String[] args) {

      // declare variables
      int number = 0;
      int sumOfDigits = 0;

      // create Scanner class object 
      // for reading the values
      Scanner scan =  new Scanner(System.in);

      // read input
      System.out.print("Enter integer number::");
      number = scan.nextInt();

      // find sum of digits of number
      sumOfDigits = digitSum(number);

      // display result
      System.out.println("The sum of digits of " +
           "the number "+number+" = "+sumOfDigits);

      // close Scanner class object
      scan.close();
   }
}

The if-else statement is used to write the condition and statement for the base and general case. We can also use the ternary operator.

public static int digitSum(int n) {
  return (n==0) ? 0 : 
      ((n%10) + digitSum(n/10));
}

Also See:-

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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