Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 161 - CoreGPT(Podar)_Flipbook_C8
P. 161

    Program 6: To display the following output.
                     1
                     1 2

                     1 2 3
                     1 2 3 4

                     1 2 3 4 5
                     #include<iostream.h>
                     #include<conio.h>
                     void main()
                     {
                         clrscr();
                         int r;

                                                                                          Enter number of rows: 5
                         cout<< "Enter number of rows: ";
                                                                                          1
                         cin>> r;                                                         1 2
                                                                                          1 2 3
                                                                                          1 2 3 4
                         for(int i = 1; i <= r; i++)
                                                                                          1 2 3 4 5
                         {
                             for(int j = 1; j <= i; j++)
                             {

                                 cout<< j << " ";
                             }
                             cout<< "\n";
                         }
                         getch();
                     }
                     Infinite Loop

                     A loop that never ends is called an infinite loop. If the condition in a loop never becomes false,
                     the loop will continue running indefinitely. To prevent an infinite loop, ensure that the condition

                     changes  somewhere  in  the  loop’s  header  or  body  so  that  it  can  eventually  become  false.
                     For example:
                     int i = 10;
                     while(i <= 10)

                     {
                         cout<< "Hello";
                         i--;
                     }

                     The preceding loop will run infinitely, because the value of the variable i will never become
                     greater than 10.

                                                                                      Developing Programs in C++        159

| 156 157 158 159 160 161 162 163 164 165 166 |