# 7.12 – Nested loops – programs to practice -1

by on October 18, 2017

Specification: Print the following output using a C program

Show Logic

We use inner loop to print 5 to 1 natural numbers

```for(i=5;i>=1;i--)
printf("%5d",i);
```

We run the above loop for 4 times using an outer loop to print the pattern

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

Program:

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

Specification: Print the following output using a C program

Show Logic

To print 1, we use the loop

```for(i=1;i<=1;i++)
printf("%5d",i);
```

To print 1   2, we use the loop

```for(i=1;i<=2;i++)
printf("%5d",i);
```

To print 1   2   3, we use the loop

```for(i=1;i<=3;i++)
printf("%5d",i);
```

In all the above loops i=1, i++ are common but, only i<=? is changing. To print 1st row it must be i<=1, to print 2nd row it must be i<=2 and to print the 5th row it must be i<=5.

Hence to print the pattern the condition to inner loop must be set as i<=j and j must start from 1 and incremented by 1 for every iteration up to 5 by the outer loop.

```for(j=1;j<=5;j++)  /* to set target to inner loop */
{
for(i=1;i<=j;i++)     /* to print actual terms  */
printf("%5d",i);
printf("\n");
}
```

Program:

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

Specification: Print the following output using a C program

Show Logic

We divide the above pattern into two parts and will print using two different loops

Loop for 1st half would be

```for(j=1;j<=5;j++)  /* to set target to inner loop */
{
for(i=1;i<=j;i++)    /* to print actual terms */
printf("%5d",i);
printf("\n";
}
```

Loop for 2nd half would be

```for(j=4;j>=1;j--)  /* to set target to inner loop */
{
for(i=1;i<=j;i++)   /* to print actual terms */
printf("%5d",i);
printf("\n");
}
```

Program:

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

Specification: print the following output using a C program

Program:

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

Specification: Print the following output using a C program

Show Logic

To print 5  4  3  2  1, the loop would be

```for(i=5;i>=1;i--)
printf("%5d",i);
```

To print 4   3   2   1, the loop would be

```for(i=4;i>=1;i--)
printf("%5d",i);
```

To print 3  2  1, the loop would be

```for(i=3;i>=1;i--)
printf("%5d",i);
```

In all the above loops i>=1, i– are common, only the beginning i=? is changing. i must be 5 for first row, i must be 4 for second row and i must be 1 for last row.

Hence to print the above pattern the initialization statement to inner loop must be set as i=j and j must start from 5 and decremented by 1 for every iteration up to 1 through the outer loop.

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

Program:

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

Specification: Print the following output using a C program

Program:

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

Specification: Print the following output using a C program

Program:

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

Specification: Print the following output using a C program

Program:

```#include<stdio.h>
int main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=j;i++)
printf("   *    ");   /* rather printing i, here printing '*'  */
printf("\n\n");
}
for(j=4;j>=1;j--)
{
for(i=1;i<=j;i++)
printf("   *    ");
printf("\n\n");
}
return 0;
}
```

Specification: Print the following output using a C program

Program:

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

Specification: Print the following output using a C program
Logic:
It needs two inner loops one is to print spaces and another is to print terms.
Outer loop is used to control both the inner loops.
Program:

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

Specification: Print the following output using a C program
Program:

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

Specification: Print the following output using a C program
Program:

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

Specification: Print the following output using a C program
Program:

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

Previous post: