Sum of the digits of a number in java

package Loops;
import java.util.Scanner;
public class sumofdigit {
 public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int number=sc.nextInt();
        System.out.println("the number is "+number);
        int sum=0;
        int temp=number;
        while(temp>0) {
            int lastDigit=temp%10;
            temp=temp/10;
            sum=sum+lastDigit;
            System.out.println(lastDigit +" "+temp + " "+sum);
          }
        System.out.println("the sum of the digit of the " + number+ " is " +sum );
    }
}
output:-
345
the number is 345
5 34 5        //process
4 3 9
3 0 12
the sum of the digit of the 345 is 12

Comments