Pages

Labels

Write a program to compute sum of digits of a given number

import java.io.*;
public class Adddigit
{
    public static void main(String [] args)throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a number to find the sum of digits of a number");
        String num = br.readLine();
        int a = Integer.parseInt(num);
        int sum=0;
        while(a!=0)
        {
            int i=a%10;
            sum=sum+i;
            a=a/10;
        }
        System.out.println("the sum of the digits entered = "+sum);
    }
}