Sum of digits of the given number in Java programming

import java.util.Scanner;

public class SumDigits {

    public static void main(String[] args) {
        Scanner myScanner=new Scanner(System.in);
       
        int n;
        System.out.print("Enter the number : ");
        n=myScanner.nextInt();
        int rem,sum=0;
        while (n!=0) {
            rem=n%10;
            sum=sum+rem;
            n=n/10;
        }
       
        System.out.println("Sum of digits of the given number is : "+sum);
    }

}
//Enter the number : 123456
//Sum of digits of the given number is : 21

Share this

Related Posts

Previous
Next Post »