Sunday, March 31, 2013

How to Calculate the Sum of Digits of Given Number



package com.swain.cell;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SumOFNumber {
       public static void main(String[] args) {
              try {
                     BufferedReader din = new BufferedReader(new InputStreamReader(
                                  System.in));

                     int n, r, s;
                     System.out
                                  .println("Please enter the number to find the sum of a digit: ");

                     n = Integer.parseInt(din.readLine());
                     r = 0;
                     s = 0;

                     while (n > 0) {
                           r = n % 10;
                           s = s + r;
                           n = n / 10;
                     }

                     System.out.println("The sum of the digit is : " + s);
              } catch (IOException e) {
                     System.out.println("Wrong Input");
              }
       }

}

No comments:

Post a Comment

How ChatGPT can Benefit Coding: Your Guide to Leveraging an AI Language Model

 Introduction: Hello, coders! Welcome to this blog post on how ChatGPT, an AI language model, can benefit your coding skills and projects. A...