Previous Next

Write a Java program to find the sum of digits?


/*Java Program to find the sum of digits */
public class SumOfDigits{

     public static void main(String args[]) {
           int number = 333;
           int sum = 0;
           int num = number;
           while (num > 0) {
                int lastDigit = num % 10;
                sum += lastDigit;
                num /= 10;
           }
           System.out.println("Sum of digits : "+sum);
     }
}
Previous Next