Page 220 - CA_Blue( J )_Class9
P. 220
Program 9 Write a program to input a number and print the sum and product of the digits.
1 import java.util.*;
2 class prog_loop1
3 {
4 public static void main()
5 {
6 Scanner sc = new Scanner(System.in);
7 int n, sum=0, pro = 1 ,r, t;
8 System.out.println("Enter a number");
9 n=sc.nextInt();
10 t=n;
11 while (t > 0)
12 {
13 r=t%10;
14 sum=sum+r;
15 pro=pro*r ;
16 t = t / 10 ;
17 }
18 System.out.println("Sum of Digits = "+ sum);
19 System.out.println("Product of digits = "+ pro);
20 }
21 }
You will get the following output:
218 Touchpad Computer Applications-IX

