Monday, July 29, 2013

Java Program to Find the Sum of the Digits of a Number

Program to Find the Sum of the Digits of a Number
import java.io.*;
class Add
{
void number()
{
try
{
int num,sum=0;
System.out.println("Enter any number:");
DataInputStream dts=new DataInputStream(System.in);
int n=Integer.parseInt(dts.readLine());
while ( n > 0 )
{
num = n % 10;
n = n / 10;
sum = sum + num;
}
System.out.println("The sum of the digits is:"
+sum);
}
catch(Exception s)
{
System.out.println(s.getMessage());
}
}
}

class Digit_add
{
public static void main(String args[])
{
Add a = new Add();
a.number();
}
}


No comments:

Post a Comment