Nesting of Loops In C language
“Success is when your ‘Signature’ changes to ‘Autograph’ “
Hello everyone!!!!
Hope you all must be doing well 🙂 In last articles we have seen three different types of loops in detail.
Today we are going to discuss the following topics 👇
1) Nesting of For loop.
2) Nesting of While loop.
3) Nesting of Do while loop.
1)Nesting of for loop:-
Let’s see the syntax first 👇
for (initialization; condition; update)
{
for(initialization; condition; update)
{
// inner loop statements.
}
// outer loop statements.
}
The working is very simple same as for loop only difference is that there are more than one for loop is present in nesting of loops. First the outer for loop condition will be checked then it will enter the inner for loop. It will get executed until and unless condition becomes false, then the outer loop will again check it’s condition and the process continues till the outer loops condition becomes false.
Let’s see some of the examples… so that it will make you understand more efficiently.
Example:- Write a C program to display the table of n numbers given by the user.
#include<stdio.h>
int main()
{
int n;
printf(“Enter the value of n :”);
scanf(“%d”,&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=10;j++)
{
printf(“%d\t”,(i*j));
}
printf(“\n”);
}
}
Output:-
Enter the value of n :15
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
11 22 33 44 55 66 77 88 99 110
12 24 36 48 60 72 84 96 108 120
13 26 39 52 65 78 91 104 117 130
14 28 42 56 70 84 98 112 126 140
15 30 45 60 75 90 105 120 135 150
2) Nesting of While loop:-
Syntax:-
while(outer condition)
{
Outer while statements;
while(inner condition)
{
Inner while statements;
}
Outer while statements;
}
Now let’s move towards example.
Example:- Write a C program to display multiplication tables from 1-10.
#include<stdio.h>
int main()
{
printf(“Multiplication table\n\n”);
int i=1,j;
while(i<=10){
j=1;
while(j<=10){
printf(“%d\t”,j*i);
j++;
}
printf(“\n”);
i++;
}
return 0;
}
output:-Multiplication table
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
3) Nesting of Do-while loop :-
Syntax:-
do
{
statement n;
do
{
statement n;
}
while(test condition);
}
while(test expression);
Let’s see the example.
Example:- Write a C program to display multiplication tables from 1-10.
#include<stdio.h>
int main()
{
int i,j;
i=1;
printf(“Multiplication table\n\n”);
printf(“Here is multiplication table\n\n”);
do{
j=1;
do{
printf(“%d\t”,i*j);
j++;
}while(j<=10);
printf(“\n”);
i++;
}while(i<=10);
return 0;
}
output:- Multiplication table
Here is multiplication table
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
I hope you must have got an idea about how exactly the nesting of loops works.
Now will have a look on some different types of examples using nesting of loops.
Q1) Write a C program to print the “*” as many times user wants to print in a pyramid format.
#include<stdio.h>
main()
{
int i,j,rows;
printf(“Enter the number of rows:”);
scanf(“%d”,&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
{
printf(“*”);
}
printf(“\n”);
}
}
Output:- Enter the number of rows:5
*
**
***
****
*****
Q2) Write a C program to display numbers in pyramid format in number of rows entered by the user.
#include<stdio.h>
main()
{
int i,j,rows;
printf(“Enter the number of rows:”);
scanf(“%d”,&rows);
for(i=1;i<=rows;++i)
{
for(j=1;j<=i;++j)
{
printf(“%d\t”,j);
}
printf(“\n”);
}
}
Output:- Enter the number of rows:5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Q3) Write a C program to display numbers in reverse order in pyramid format in number of rows entered by the user.
#include<stdio.h>
int main()
{
int i, n, dn;
printf(“ENTER A NUMBER :”);
scanf(“%d”, &n);
i = n;
while(i >= 1)
{
printf(“\n”);
dn = i;
while(dn >= 1)
{
printf(“%d “, dn);
dn = dn – 1;
}
i = i – 1;
}
return 0;
}
output:- ENTER A NUMBER 7
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Like wise you can write different types of programs to print different different things of your choice using nesting of loops.
Soo will meet in next article.
Till then good bye have a wonderful day ahead 🙂
And yes don’t forget to put comments 🙂
Thank you all for your support :).
You must log in to post a comment.