Skip to main content

Write a program to print the sum of all the digits of a given number. Example1) I/P:1234 O/P:10

 Write a program to print the sum of all the digits of a given number.


Example1) 

I/P:1234

O/P:10



import java.util.Scanner;

public class SumofDigits {


public static void main(String[] args)

 {

int n,rem,sum;

Scanner s=new Scanner(System.in);

System.out.println("Enter the number:");

n=s.nextInt();


for(sum=0;n!=0;n/=10) {

rem=n%10;

sum=sum+rem;

}

  System.out.println(sum);

}


}


Comments