Thursday, April 4

Summation of a given set of Numbers using java (calculate given sum of digits in java)

Hi All,

 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

Backup files to google drive using PHP coding

 Dear All, To backup files to Google Drive using PHP coding, you can use the Google Drive API and the Google Client Library for PHP. Here...