Page 295 - CA_Blue( J )_Class9
P. 295

(viii) Programs based on digit extraction
                                Write a program to input a number and print whether the number is an Armstrong Number or not.
                  Program 17
                                Examples of Armstrong Numbers:
                                            1
                                1       :   1               =   1
                                2       :  2 1              =   2
                                            1
                                3       :   3               =   3
                                153     :   1  + 5  + 3     =   1 + 125+ 27       =   153
                                                    3
                                            3
                                                3
                                125     :   1  + 2  + 5     =   1 + 8 + 125       =   134 (Not an Armstrong Number)
                                            3
                                                3
                                                    3
                                                        4
                                                    4
                                                4
                                1634    :   1 + 6  + 3  + 4    =   1 + 1296 + 81 + 256   =   1643
                                            4
                   1      import java.math.*;                                       //importing “math” package
                   2      import java.util.*;                                       //importing “util” package
                   3      class Armstrong
                   4      {
                   5          public static void main()
                   6          {
                   7              Scanner sc= new Scanner(System.in);

                   8              int n,rem,sum=0,temp,count=0;                     //Declaration of valriable
                   9              System.out.print ("Enter a number : ");

                  10              n=sc.nextInt();                                   //Accepting value
                  11              temp=n;                                //Taking “n” to temporary variable

                  12              while(temp>0)                          //Loop for finding number of digits
                  13              {

                  14                  count++;                      //counter variable for number of digits
                  15                  temp=temp/10;
                  16              }

                  17              temp= n;

                  18              while(temp>0)                          //Loop for digit extraction
                  19              {
                  20                  rem=temp%10;

                  21                  sum=sum+(int)Math.pow(rem,count);
                  22                  temp=temp/10;

                  23              }
                  24              if(n==sum)                              // checking of Armstrong number

                  25                  System.out.println(n+" is Armstrong Number");
                  26              else

                  27                  System.out.println(n+" is not Armstrong Number");
                  28          }}



                                                                                             Internal Assessment  293
   290   291   292   293   294   295   296   297   298   299   300