Showing posts with label sum of digit of given number. Show all posts
Showing posts with label sum of digit of given number. Show all posts

Thursday, 13 June 2013

program in java for sum of digit of given number

import java.io.*;
class simple_sum_of_digit_of_given_number
{
    public static void main(String args[])
    {
        try
        {
            int sum=0,n,no,rem;
            DataInputStream cin=new DataInputStream(System.in);
            System.out.println("Enter a number to calculate its digit's sum = ");
            n=Integer.parseInt(cin.readLine());
            no=n;

            while(no!=0)
            {
                rem=no%10;
                sum=sum+rem;
                no=no/10;
            }

            System.out.println(n+" of sum of digit is "+sum);
        }
        catch(Exception e)
        {
            System.out.println("wrong input");
        }
    }
}