Summation of a set of Numbers, It used to calculate the sum of digits of given numbers :)
import java.io.*;
public class Summation {
public static void main(String arg[]) {
try {
BufferedReader din = new BufferedReader(new InputStreamReader(System.in));
int num, rem, sum;
System.out.println(" Summation of a set of Numbers ");
System.out.println("Please enter the number the sum of a digit: ");
num = Integer.parseInt(din.readLine());
rem = 0;
sum = 0;
while (num > 0) {
rem = num % 10; //getting remainder value stored in rem variable
sum = sum + rem; // adding rem values to sum
num = num / 10; // divide the number by 10 get remove the remainder value
}
System.out.println("The total Summation of the digit is : " + sum);
} catch (IOException e) {
System.out.println("Wrong Input");
}
}
}
output:
Summation of a set of Numbers
Please enter the number the sum of a digit:
12121
The total Summation of the digit is :7
No comments:
Post a Comment