CH. 5   NESTED LOOPS IN C

1. Write c programs to display the following patterns using nested loop construct.
a. 1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
Code:-

#include <stdio.h>
int main() {
int i, j;

for (i = 1; i < 6; i++) {
for (j = 1; j <=3; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}

b.
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
Code:-

#include <stdio.h>
int main() {
int i, j;

for (i = 1; i < 6; i++) {
for (j = 1; j <=2; j++) {
printf("%d ", j);
}
printf("1");
printf("\n");
}
return 0;
}

c.4 3 2 1
4 3 2 1
4 3 2 1
4 3 2 1
4 3 2 1
Code:-

#include <stdio.h>
int main() {
int i, j;

for (i = 1; i < 6; i++) {
for (j = 4; j >=1; j--) {
printf("%d ", j);
}

printf("\n");
}
return 0;
}

d. 2
234
23456
Code:-

#include<stdio.h>
int main()
{
int i, j, k;
for(i=1;i<=3; i++){
for(j=1; j<=(3-i); j++)
{
printf(" ");
}
for (k=1; k<=(2*i-1); k++){
printf("%d", k+1);
}
printf("\n");
}
return 0;
}

e. 1
121
12321
Code:-

#include<stdio.h>
int main()
{
int i, j;
for(i=1;i<=3; i++) {
for(j=1;j<=3-i;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d", j);
}
for(j=i-1;j>=1;j--)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}

f.   *
***
*****
*******
*****
***
*
Code:-

#include <stdio.h>
int main() {
int n=7;

int spaces=n-1;
int stars=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=spaces;j++)
{
printf(" ");
}
for(int k=1;k<=stars;k++)
{
printf("*");
}
if(spaces>i)
{
spaces=spaces-1;
stars=stars+2;
}
if(spaces<i)
{
spaces=spaces+1;
stars=stars-2;
}
printf("\n");
}
return 0;
}

g.

X              X
X X           X X
X X X        X X X
X X X X     X X X X
X X X X X  X X X X X

Code:

#include<stdio.h>
int main()
{
int i,j,k,p;
for(k=1;k<=5;k++)
{
for(i=1;i<=k;i++)
{
printf("X ");
}
for(j=1;j<=(5-k);j++)
{
printf(" ");
}
for(p=1;p<=k;p++)
{
printf("X ");
}
printf("\n");
}
return 0;
}

2. Modify the solution of question no. 1 to accept the number of lines as the input. The program should make the display pattern accordingly (Hint: write separate programs).
a.

1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
……………

Code:-

#include <stdio.h>
int main() {
int i, j,a;
printf("\n Enter the value for number of lines :");

scanf("%d", &a);

for (i = 1; i <= a; i++) {
for (j = 1; j <=3; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}

b.

1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
……………

Code:-

#include <stdio.h>
int main() {
int i, j,a;
printf("\n Enter the value for number of lines :");

scanf("%d", &a);
for (i = 1; i <= a; i++) {
for (j = 1; j <=2; j++) {
printf("%d ", j);
}
printf("1");
printf("\n");
}
return 0;
}

c.

4 3 2 1
4 3 2 1
4 3 2 1
4 3 2 1
4 3 2 1
…………………….

Code:-

#include <stdio.h>
int main() {
int i, j,a;
printf("\n Enter the value for number of lines :");

scanf("%d", &a);
for (i = 1; i <=a; i++) {
for (j = 4; j >=1; j--) {
printf("%d ", j);
}printf("\n");
}
return 0;
}

d.

2
234
23456
………………….
Code:-

#include<stdio.h>
int main()
{
int i, j, k,a;
printf("\n Enter the value for number of lines :");
scanf("%d", &a);
for(i=1;i<=a; i++){
for(j=1; j<=(3-i); j++)
{
printf(" ");
}
for (k=1; k<=(2*i-1); k++){
printf("%d", k+1);
}
printf("\n");
}
return 0;
}

e.

1
121
12321
……………………..
Code:-

#include<stdio.h>
int main()
{
int i, j,a;
printf("\n Enter the value for number of lines :");
scanf("%d", &a);
for(i=1;i<=a; i++) {
for(j=1;j<=3-i;j++)
{
printf(" ");
}

for(j=1;j<=i;j++)
{
printf("%d", j);
}

for(j=i-1;j>=1;j--)
{
printf("%d", j);
}
printf("\n");
}

return 0;
}

f.

*
***
*****
*******
*****
***
*
………………………
Code:-

#include <stdio.h>
int main() {
int n;
printf("\n Enter the value for number of lines :");
scanf("%d", &n);
int spaces=n-1;
int stars=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=spaces;j++)
{
printf(" ");
}
for(int k=1;k<=stars;k++)
{
printf("*");
}
if(spaces>i)
{
spaces=spaces-1;
stars=stars+2;
}
if(spaces<i)
{
spaces=spaces+1;
stars=stars-2;
}
printf("\n");
}
return 0;
}

g.

X              X
X X           X X
X X X        X X X
X X X X     X X X X
X X X X X  X X X X X

……………………………

Code:

#include<stdio.h>
int main()
{
int i,j,k,p,a;
printf("\n Enter the value for number of lines :");

scanf("%d", &a);
for(k=1;k<=a;k++)
{
for(i=1;i<=k;i++)
{
printf("X ");
}
for(j=1;j<=(a-k);j++)
{
printf(" ");
}
for(p=1;p<=k;p++)
{
printf("X ");
}
printf("\n");
}
return 0;
}

3. Extend the programs of Example 5.6 and Example 5.7 to make it dynamic by accepting the number of lines as an input from the keyboard.
a. X
XXX
XXXXX
XXXXXXX
XXXXXXXXX
…………………..
Code:-

#include <stdio.h>
int main()
{
int i, j,k,a;
printf("\n Enter the value for number of lines :");
scanf("%d", &a);
for (k = 1; k <=a; k++) {
for(i = 1; i<=(5-k); i++) {
printf(" ");

}

for(j= 1; j <=(2*k-1);j++)
{
printf("X");
}
printf("\n");
}
return 0;
}

b. 1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
……………………………..
Code:-

#include <stdio.h>
int main()
{
int i, j, k,a;
printf("\n Enter the value for number of lines :");

scanf("%d", &a);
for (k = 1; k <=a; k++){

for(i = 1; i<=(a-k); i++){
printf(" ");
}
for(j= 1; j <=(2*k - 1); j++){
printf("%d ", j);
}
printf("\n");
}
return 0;
}

4. What is a nested loop? Why do one use nested loops in our programs?
Ans:-Nesting of loops is the feature in C that allows the looping of statements inside another loop. The first loop is called the outer loop and the loop that appears inside another loop is called the inner loop.
Syntax of Nested loop:-

Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
}

There can be more than one inner loop in a program.The nested loop construct simplifies programming in many cases. This also reduces the program length.

5. Do we need to use same type of loop as outer and inner loops? Justify with some code segments.

Ans: No, we do not need to use same type of loop as outer and inner loops. we do not need to use same type of loop as outer and inner loops because the outer loop encloses the inner loop.So,The inner loop is a part of the outer loop and must start and finish within the body of outer loop.
For example:

for (int i=0; i<5; i++)
{
// body of outer for loop
for (int j=0; j<5; j++)
{
// body of inner for loop
}//inner loop closed
// body of outer for loop
}//outer loop closed

6. Can we put a third loop inside the inner loop of a nested loop constract ? Write a C program to justify your answer.
Ans:-Yes, we can put a third loop inside the inner loop of a nested loop constract .
For example:-

X              X
X X           X X
X X X        X X X
X X X X     X X X X
X X X X X  X X X X X

To draw the above pattern we use the following code:-

#include<stdio.h>
int main()
{
int i,j,k,p;
for(k=1;k<=5;k++)
{
for(i=1;i<=k;i++)
{
printf("X ");
}
for(j=1;j<=(5-k);j++)
{
printf(" ");
}
for(p=1;p<=k;p++)
{
printf("X ");
}
printf("\n");
}
return 0;
}

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here