Program which reads a four digit integer number and calculate sum of its digits.

import java.io.*;
class Sumofdigits
{

public static void main(String[] asda) throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int n,sum=0;
String num;
System.out.println("Enter a four digit number::");
num=in.readLine();
n=Integer.parseInt(num);
while(true)
{
sum=sum+n%10;
n=n/10;
if(n==0) break;
}
System.out.println("Sum of digits is="+ sum);
}
  }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s