//File name-->SumOfDigit.java
import java.io.*;
class SumOfDigit{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int no,sum=0,temp;
System.out.println("Enter number:");
no=temp=Integer.parseInt(br.readLine());
while(no>0)
{
sum+=(no%10);
no=no/10;
}
System.out.println("Sum of Digit for number "+temp+" is:"+sum);
}
}
OUTPUT
javac SumOfDigit.java
java SumOfDigit
Enter number:
12345
Sum of Digit for number 12345 is:15
import java.io.*;
class SumOfDigit{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int no,sum=0,temp;
System.out.println("Enter number:");
no=temp=Integer.parseInt(br.readLine());
while(no>0)
{
sum+=(no%10);
no=no/10;
}
System.out.println("Sum of Digit for number "+temp+" is:"+sum);
}
}
OUTPUT
javac SumOfDigit.java
java SumOfDigit
Enter number:
12345
Sum of Digit for number 12345 is:15