Monday , December 18 2017
Home / JAVA / Beginners / JAVA program to calculate digit sum of an integer

JAVA program to calculate digit sum of an integer

The digit sum of a given integer number is the sum of all its individual digits. Java Source code to calculate sum of digits of a number is given with example and detailed explanation. The sum of the digits of a number are calculated or used in many further programs.

Digit Sum Example:-

Digit Sum

Iteration 1 :

  • 1894%10 = 4
  • 1894/10= 189
  • value of S=0+ 4

Iteration 2 :

  • 189%10=9
  • 189/10= 18
  • Value of S=4+9

Iteration 3 :

  • 18%10=8
  • 18/10=1
  • Value of S=13+8

Iteration 4 :

  • 1%10=1
  • 1/10=0
  • Value of S= 21+1

Iteration 5 :

  • Terminates because of false Condition of loop.
  • import java.util.*;
    class DigitSum
    {
       public static void main(String args[])
       {
           int n,i=0, sum=0;
           n=Integer.parseInt(args[0]);           //taking integer from command line 
           while(n!=0)                     //calculate digit sum
           {
               i=n%10;
               n=n/10;
               sum=sum+i;
           }
           System.out.println(sum);
       }
    }

    Output:-

    Digit Sum

  • import java.util.*;
    
    public class DigitSum{
    
        public static void main(String args[]) {
    
            Scanner sy = new Scanner(System.in);
    
            System.out.println("Please enter a number to calculate digit sum:");
            int number = sy.nextInt();
    
            // Remember number/10 reduces one digit from number
            // and number%10 gives you last digit
            int s = 0;
            int i = n;
            while (i != 0)   //calculate digit sum while input> 0
            {
                int lastdigit = i % 10;
                s += lastdigit;
                i /= 10;
            }
    
            System.out.printf("Digit Sum of number %d is %d", n, s);
            // Scanner close to prevent resource leak
            sy.close();
    
        }
    
    }

     

    Output:
    Please enter a number to calculate digit sum:12321
    Digit Sum of number 12321 is 9

     

    Please enter a number to calculate digit sum:123456789987654321
    Digit Sum of number 123456789987654321 is 90

  • import java.util.*;
    
     public class DigitSum
     {
          public static void main(String[] args)
          {
                Scanner sc = new Scanner(System.in);
                int a;
                System.out.print("Enter a positive integer: ");
                a = in.nextInt();
                if (a <= 0)
                System.out.println("Integer you've entered is non-positive.");
                else
                {
                      int s = 0;
                      while (a != 0)
                     {
                            s += a % 10;
                            a /= 10;
                      }
                      System.out.println("Digit Sum of integer: " + s);
                }
          }
    }

    Output:-

    Enter a positive integer: 12321

    Digit Sum of integer: 9

    Enter a positive integer: -342424

    Integer you’ve entered is non-positive.

    Enter a positive integer: 342424

    Digit Sum of integer: 19

  • Just One line solution for Sum of the digits

    int num = 12346; // any number
    int sum = 0;  
    for (int n = num; n > 0; sum += n % 10, n /= 10) ;

 

Also See:   Program to count the number of alphabets, digits, spaces, special symbols

 

Check Also

Java program to implement polymorphism

Java program to implement polymorphism: Polymorphism is a technique in java with which you can ...

Leave a Reply

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