Page 460 - Cs_withBlue_J_C11_Flipbook
P. 460

Armstrong class is as follows:

                1       import Eleven.Number; // importing package

                2       import java.util.*;
                3       class Armstrong

                4       { int x,s=0;
                5         void accept()

                6         { Scanner sc=new Scanner(System.in);
                7           System.out.println("Enter the number");

                8           x=sc.nextInt();
                9         }

                10        void check()
                11        { int l=3,copy,d,p;
                12           Number ob=new Number(); // creating object of class Number to call its methods

                13          l=ob.numlength(x); // method call of class Number in package Eleven

                14          copy=x;
                15          while(copy>0)
                16          { d=copy%10;

                17            p=ob.topower(d,l); // method call of class Number in package Eleven
                18            s=s+p;

                19            copy=copy/10;
                20          }

                21          if(s==x)
                22            System.out.println(x+" is an Armstrong number");

                23          else
                24            System.out.println(x+" is not an Armstrong number");

                25          }
                26      }

              The output of the preceding program is as follows:
              Enter a number
              153
              153 is an Armstrong number
              Enter number
              1672
              1672 is not an Armstrong number
              Enter number
              1634
              1634 is not an Armstrong number



                458458  Touchpad Computer Science-XI
   455   456   457   458   459   460   461   462   463   464   465