Page 385 - CA_Blue( J )_Class10
P. 385

To create an array of 5 elements and using switch case do the following.
                  Program 5      Case 1: prints all the Armstrong numbers
                                 Case 2: print all the odd positive numbers

                   1  import java.util.*;

                   2  class menu_driven
                   3  {

                   4      public static void main()
                   5      {

                   6          Scanner sc= new Scanner(System.in);
                   7          int ar[]=new int[5];

                   8          int i,ch,t,s=0;
                   9          for (i=0; i<5; i++)

                  10          {
                  11              System.out.print("Enter any five numbers(three digit) : ");

                  12              ar[i] =sc.nextInt();
                  13          }

                  14          System.out.println("Enter 1 for Armstrong Numbers :");
                  15          System.out.println("Enter 2 for odd positive numbers :");

                  16          System.out.println("Enter your choice ");
                  17          ch=sc.nextInt();

                  18          switch(ch)
                  19          {

                  20              case 1:
                  21              System.out.print("The Armstrong numbers are : ");

                  22              for(i=0;i<5;i++)
                  23              {
                  24                  t=ar[i];

                  25                  s=0;

                  26                  while(t>0)
                  27                  {   s=s+(int)Math.pow(t%10,3);
                  28                      t=t/10;

                  29                  }
                  30                  if(s==ar[i])

                  31                  {   System.out.print(ar[i]+ " : " );    }
                  32              }



                                                                                                                       383
                                                                                                              Arrays   383
   380   381   382   383   384   385   386   387   388   389   390