C

Nested for loop

We can also have nested for loops, i.e one for loop inside another for loop.

Syntax:

for(initialization; condition; increment/decrement)
{
    for(initialization; condition; increment/decrement)
    {
        statement;
    }
}

Example:

//Write a C Program which Print Half Pyramid using nested for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j;
    for(i=1;i<5;i++)
    {
        printf("\n");
        for(j=i;j>0;j--)
        {
            printf("%d",j);
        }
    }
    getch();
}

Output:

Command Prompt
1
21
321
4321
54321



Subscribe us on Youtube

Share This Page on


Ask Question