DEV Community

Vignesh . M
Vignesh . M

Posted on

SUM OF THE DIGIT IN JAVA

what is sum of the digit in java?

  • sum of the digit means add the each digit as a single number Ex : 12 // sum of the digit = 1+2= 3 .
  • 3 is the value of sum of the digit .
  • In java write the code for sum of the digit is step 1 = find the last digit .(in mathematical for of the number last digit is called Tes's). so find the Ten's value .
  • To seprate the last digit using this operater - (%)modules oprator.
  • modules (%) is given the remender value. 12%10 = 2 step 2 = find the hunder's value to using the divide(/) - operater.
  • dived operater is giving the cosent vale . 123/10 = 1 step 3 = The add the digit 2+1 =3 sample program :
public class SumOfDigit{

public static void main (String arg[]){

int number = 12356;
    int sum =0;
    while(number>0){
   int digit = number%10;//3
    sum = sum + digit;//0+123
    number = number/10;
}
System.out.println("YOUR SUM OF THE DIGIT IS :"+sum);        
}
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
YOUR SUM OF THE DIGIT IS : 17

Top comments (0)