Tuesday , December 6 2016
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.
  • Output:-

    Digit Sum

  •  

    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

  • 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

 

 

Also See:   Java program for Method overriding

Check Also

Program for Matrix Multiplication in JAVA

Matrix Multiplication:– Here is given a Java Program for Matrix Multiplication also call matrix product. In ...

Leave a Reply

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

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.