Page 201 - CA_Blue( J )_Class10
P. 201

Note: The loop control variable should be unique for both outer and inner loops. If they are same,
                       then both the loops will conflict with each other.



                     9.2 NESTED WHILE LOOP
                 If a while loop is used within another while loop, then it is called a nested while loop. Let us take an example.


                  Program 2    To input 2 numbers and print the factorial of each number.


                   1  import java.util.*;

                   2  class nested_while
                   3  {    public static void main()
                   4      {   int i,j,n,f;

                   5          Scanner sc= new Scanner(System.in);

                   6          i=1;
                   7          while(i<=2)

                   8          {   System.out.print("Enter a number : ");
                   9              n=sc.nextInt();

                  10              j=1; f=1;
                  11              while(j<=n)
                  12              {   f=f*j;

                  13                  j++;

                  14              }
                  15              System.out.println("Factorial of " + n + " is : "+f);
                  16              i++;

                  17          }
                  18      }

                  19  }

                 The output of the above program:












                 Dry Run of the above program:








                                                                                                                       199
                                                                                                         Nested Loop   199
   196   197   198   199   200   201   202   203   204   205   206