Page 223 - CA_Blue( J )_Class9
P. 223

Write a  program to  input  a  number and  print  whether it  is  an  Armstrong  numbers. An
                  Program 12
                                 Armstrong number is a number which is equal to the sum of cubes of its digits. 1, 153, 370,
                                 371, 407 are some examples.
                   1      import java.util.*;

                   2      class prog_loop4
                   3      {

                   4          public static void main ()
                   5          {

                   6              Scanner sc = new Scanner (System.in);
                   7              int n, r,t, sum=0;
                   8              System.out.print("Enter a number to check for Armstrong number: ");

                   9              n=sc.nextInt();

                  10              t=n;
                  11              while(t>0)
                  12              {

                  13                  r=t%10;

                  14                  sum=sum+(r*r*r);
                  15                  t = t / 10;
                  16              }

                  17              if(n==sum)
                  18                  System.out.println (n + " is Armstrong Number.");

                  19              else
                  20                  System.out.println (n + " is not Armstrong Number.");

                  21          }
                  22      }

                 You will get the following output:

























                                                                                       Iterative Constructs in Java  221
   218   219   220   221   222   223   224   225   226   227   228