Page 205 - CA_Blue( J )_Class9
P. 205

Write a program to input 5 even or odd numbers, and print the square of the odd numbers
                  Program 6
                                 using while loop.
                   1      import java.util.*;

                   2      public class odd_square
                   3      {

                   4       public static void main()
                   5       {

                   6           Scanner sc = new Scanner(System.in);
                   7           int n, i = 1;

                   8           while(i<=5)
                   9           {

                  10               System.out.print("Enter a number: ");
                  11               n = sc.nextInt();
                  12               if(n%2==1)

                  13               {

                  14                   System.out.println("Square of "+n+" :"+(n*n));
                  15               }
                  16               else

                  17               {
                  18                   System.out.println(n +" is an even number");

                  19               }
                  20               i++;

                  21              }
                  22          }

                  23      }
                 You will get the following output:

























                                                                                       Iterative Constructs in Java  203
   200   201   202   203   204   205   206   207   208   209   210