Sie sind auf Seite 1von 1

/** * Write a program to compute sum of digits of a given number.

(Hint: Separate digits one by one from the number and then add) */ import java.io.*; class q2 { public static void main(String args[]) throws java.io.IOException { String line = null; int n=0,r=0,sum=0; System.out.println("Program to compute Sum of the digits of given no. "); System.out.println(" "); try { BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); System.out.print(" Enter a Number : "); line = is.readLine(); n = Integer.parseInt(line); System.out.print("Sum of the digits of "+n+" is : "); while(n!=0) { r=n%10; sum=sum+r; n=n/10; } System.out.println(sum); } catch(Exception e) { } } }

Das könnte Ihnen auch gefallen