Page 329 - Cs_withBlue_J_C11_Flipbook
P. 329
Program 6 Write a program to input 10 numbers in a SDA and print the sum of all the Armstrong numbers
in the array. An Armstrong number is a number whose sum of the cube of the digits is same
as the number.
1 import java.util.*;
2 class armstrong {
3 public static void main()
4 {
5 Scanner sc= new Scanner(System.in);
6 int ar[ ]=new int[10];
7 int i, t, r, sum=0,sum1=0;
8 for (i=0; i<10; i++)
9 {
10 System.out.print("Enter a number: ");
11 ar[i] =sc.nextInt();
12 }
13 System.out.print("The Armstrong numbers are: ");
14 for(i=0; i<10; i++)
15 {
16 t=ar[i];
17 sum=0;
18 while(t>0)
19 {
20 r=t%10;
21 sum=sum+(int)Math.pow(r,3);
22 t=t/10;
23 }
24 if(sum==ar[i])
25 {
26 System.out.print(ar[i]+ " : " );
27 sum1=sum1+ar[i];
28 }
29 }
30 System.out.println("\nSum of all the Armstrong numbers are: "+sum1);
31 }
32 }
327
Arrays 327

