Page 305 - computer science (868) class 11
P. 305

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      }



                                                                                                                       303
                                                                                                              Arrays   303
   300   301   302   303   304   305   306   307   308   309   310