Page 65 - Modular_V2.0_C++_Flikpbook
P. 65

Program 5: To display the first 10 natural numbers in reverse order.

                 #include<iostream.h>
                 #include<conio.h>
                 void main()                              DOSBox 0.74, Cpu speed: max 100% cycles, Frames 0, Program:
                 {                                     First 10 natural numbers in reverse order:
                                                       10 9 8 7 6 5 4 3 2 1
                     clrscr();
                     int n = 10;
                     cout<< "First 10 natural numbers in reverse order:\n";
                     do

                     {
                         cout<< n << " ";
                         n--;
                     }
                     while(n > 0);
                     getch();

                 }
                 This loop prints numbers starting from 10 and going down to 1, until the n becomes 0 which is
                 the terminating condition of the loop.

                    NESTED LOOP


                 If a loop executes inside another loop, it is known as nested loop. Both the loops will execute
                 depending on its conditional statement. But the inner loop must terminate before the outer
                 loop. The syntax to create a nested loop is:
                      for(... ...)                           // Outer loop
                      {

                          // Code inside the outer loop


                          for(... ...)                       // Inner loop
                          {

                               // Code inside the inner loop
                          }
                      }
                 Program 6: To display the following output.

                 1

                 1 2
                 1 2 3

                 1 2 3 4

                 1 2 3 4 5
                                                                                                                  63
                                                                                                         Loops
   60   61   62   63   64   65   66   67   68   69   70