Thursday, September 10, 2015

Program to find Sum of Digits

import java.io.*;
class SumOfDigits
{
    public static void main(String args[])
    {
        int num,i=0,temp,sum=0;
        DataInputStream r = new DataInputStream(System.in);
        try
        {
            System.out.println("Enter the Number");
            num = Integer.parseInt(r.readLine());
            while(i<num)
            {
                temp = num%10;
                num = num/10;
                sum = sum+temp;
            }
            System.out.println("Sum of Digits = "+sum);
        }
        catch(Exception e)
        {
            System.out.println("error is occur in"+e);
        }
    }
}

No comments:

Post a Comment