Java

Program to Find the Sum of the Digits of a Number

By June 3, 2016 November 9th, 2016 No Comments

/****  Java 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();
}
}

Output:





Screenshot (280)

 

Leave a Reply