Page 59 - Modular_V1.1_Flipbook
P. 59
Program 5: To display the first 10 natural numbers in reverse order.
#include<iostream.h>
#include<conio.h>
void main()
{ Output:
10 9 8 7 6 5 4 3 2 1
int n = 10;
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
{
. . . . . .
for ( ... ... ...) //Inner loop
{
. . . . . .
. . . . . .
}
}
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()
{
Loops 57

