Nasted Loops
Using loops with in a loops is known as a nasted loops. For e.g.
For loops:
Write a program to print the following output.
*
* *
* * *
* * * *
* * * * *
- #include<iostream.h>
- int main()
- {
- int a,b;
- for(a=1;a<=10;a++)
- {
- cout<<"\n";
- for(b=1;b<=a;b++)
- cout<<"x";
- }
- system("pause");
- return 0;
- }
Reverse shape:
- #include<iostream.h>
- int main()
- {
- int a,b;
- for(a=10;a>=1;a––)
- {
- cout<<"\n";
- for(b=1;b<=a;b++)
- cout<<"x";
- }
- system("pause");
- return 0;
- }
While Loop:
- #include<iostream.h>
- int main()
- {
- int a,b;
- a=1;
- while(a<=10)
- {
- cout<<"\n";
- b=1;
- while(b<=a)
- {
- cout<<"*";
- b++;
- }
- a++;
- }
- system("pause");
- return 0;
- }
Reverse shape:
- #include<iostream.h>
- int main()
- {
- int a,b;
- a=10;
- while(a>=1)
- {
- cout<<"\n";
- b=1;
- while(b<=a)
- {
- cout<<"*";
- b++;
- }
- a– –;
- }
- system("pause");
- return 0;
- }
Do While Loops:
- #include<iostream.h>
- int main()
- {
- int a,b;
- a=1;
- do
- {
- cout<<"\n";
- {
- b=1;
- do
- {
- cout<<"x";
- b++;
- }
- while(b<=a);
- }
- a++;
- }
- while(a<=10);
- system("pause");
- return 0;
- }
Reverse shape:
- #include<iostream.h>
- int main()
- {
- int a,b;
- a=10;
- do
- {
- cout<<"\n";
- {
- b=1;
- do
- {
- cout<<"x";
- b++;
- }
- while(b<=a);
- }
- a––;
- }
- while(a>=1);
- system("pause");
- return 0;
- }
Write a Program to Show a Output 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
- #include<iostream.h>
- int main()
- {
- int a,b;
- for(a=1;a<=7;a++)
- {
- cout<<"\n";
- for(b=1;b<=a;b++)
- cout<<b;
- }
- system("pause");
- return 0;
- }
Write a Program to Show a Output 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9 10
- #include<iostream.h>
- int main()
- {
- int a,b;
- for(a=1;a<=10;a++)
- {
- cout<<"\n";
- for(b=1;b<=a;b++)
- cout<<b;
- }
- system("pause");
- return 0;
- }