Nested Loop with example C Language
When loop inside a loop is called nested loop. Working procedure of nested loop is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass ...
Key Architecture Takeaways
- ›Point 1: Core computer science and engineering computing concept: Nested Loop with example C Language.
- ›Point 2: Detailed trace of memory representation, algorithmic complexity, and control structures.
- ›Point 3: Includes portable ISO C (C17/C23) code examples verified with modern GCC and Clang.
Compiler & Toolchain Modernization
While legacy educational environments used 16-bit DOSBox Turbo C++, all code presented here adheres to standard ANSI/ISO C standards. You can compile these programs cleanly on modern Linux, Windows (MSYS2/WSL), or macOS using:
> gcc -Wall -Wextra -O2 program.c -o program
> ./program
> ```
---
When loop inside a loop is called nested loop. Working procedure of nested loop is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes.
## Syntax of nested loop (for, while and do-while):
**The syntax of a nested for loop: is as follows :**c for ( start; condition; increment or decrement ) { for ( start; condition; increment or decrement ) { Block of statement(s); } Block of statement(s); }
**The syntax of a nested while loop is as follows :**c while(condition) { while(condition) { Block of statement(s); } Block of statement(s); }
The syntax of a **nested do-while loop** is as follows :c do { Block of statement(s); do { Block of statement(s); } while( condition ); }while( condition );
##
#
# Nested Loop Example Program:
- **C Program to print various pattern using * and space and number:**
Here we describe how to create various patterns using c programming. Most of these c programs involve usage of nested loops number, star (*) and space. A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may form a sequence. Some of these patterns are triangles which have special importance in mathematics. Some patterns are symmetrical while other are not. Please see the complete page and look at comments for many different patterns.
## Print Pattern Program #1c 1 22 333 444 55555
#include<stdio.h>
void main()
{
int i,j,term;
clrscr();
printf("Enter the number of row you want to see");
scanf("%d",&term);
for (i = 1 ; i <= term ; i++)
{
for(j = 1 ; j <= i ; j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}Output #1:
Print Pattern Progam #2
1
12
123
1234
12345c #include<stdio.h> void main() { int i,j,term; clrscr(); printf("Enter the number of row you want to see"); scanf("%d",&term); for (i = 1 ; i <= term ; i++) { for(j = 1 ; j <= i ; j++) { printf("%d",j); } printf("\n"); } getch(); }
**Output #2:**
[](http://technhit.in/wp-content/uploads/2016/08/nested-pattern2.png)
## Print Pattern Program #3c 12345 1234 123 12 1
#include<stdio.h>
void main()
{
int i,j,term;
clrscr();
printf("Enter the number of row you want to see");
scanf("%d",&term);
for (i = term ; i >= 1 ; i--)
{
for(j = 1 ; j <= i ; j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}Output #3:
Print Pattern Program #4
55555
4444
333
22
1c #include<stdio.h> void main() { int i,j,term; clrscr(); printf("Enter the number of row you want to see"); scanf("%d",&term); for (i = term ; i >= 1 ; i--) { for(j = 1 ; j <= i ; j++) { printf("%d",i); } printf("\n"); } getch(); }
**Output #4:**
[](http://technhit.in/wp-content/uploads/2016/08/nested-pattern4.png)
## Print Pattern Program #5c 1 232 34543 4567654 567898765
#include<stdio.h>
void main()
{
int n, j, i, num = 1, space;
clrscr();
printf("Enter the number of rows in pyramid of stars you want to see ");
scanf("%d",&n);
space = n - 1;
for ( i = 1 ; i <= n ; i++ )
{
num = i;
for ( j = 1 ; j <= space ; j++ )
printf(" ");
space--;
for ( j = 1 ; j <= i ; j++ )
{
printf("%d", num);
num++;
}
num--;
num--;
for ( j = 1 ; j < i ; j++)
{
printf("%d", num);
num--;
}
printf("\n");
}
getch();
}Output #5:
Print Pattern Program #6
* * * * *
* * * *
* * *
* *
*c #include <stdio.h> void main() { int n,i,j,k,samp=1; clrscr(); printf("Enter the number of rows in pyramid of stars you want to see "); scanf("%d",&n); for (i=n; i>=1; i--) { for (k=samp; k>=0; k--) { printf(" "); // Put only 1 space } for (j=i; j>=1; j--) { printf("*"); } samp = samp + 1; printf("\n"); } getch(); }
**Output #6:**
[](http://technhit.in/wp-content/uploads/2016/08/nested-pattern6.png)
## Print Pattern Program #7c *
#include <stdio.h>
void main() {
int n,i,j;
clrscr();
printf("Enter the number of rows in pyramid of stars you want to see ");
scanf("%d",&n);
for (i=n; i>=1; i--)
{
for (j=1; j<=i; j++)
{
printf(" * ");
}
printf("\n");
}
getch();
}Output #7:
Print Pattern Program #8
*
***
*****
*******
*********c #include <stdio.h> void main() { int row, c, n, temp; clrscr(); printf("Enter the number of rows in pyramid of stars you want to see "); scanf("%d",&n); temp = n; for ( row = 1 ; row <= n ; row++ ) { for ( c = 1 ; c < temp ; c++ ) printf(" "); temp--; for ( c = 1 ; c <= 2row - 1 ; c++ ) printf(""); printf("\n"); } getch(); }
** Output #8:**
[](http://technhit.in/wp-content/uploads/2016/08/nested-pattern8.png)
## Print Pattern Program #9c *
#include <stdio.h>
#include <conio.h>
void main() {
int n,i,j,k,samp=1;
clrscr();
printf("Enter the number of rows in pyramid of stars you want to see ");
scanf("%d",&n);
for (i=1; i<=n; i++)
{
for (k=samp; k<=n; k++)
{
printf(" ");
}
for (j=0; j< i; j++)
{
printf("*");
}
samp = samp + 1;
printf("\n");
}
samp = 1;
for (i=n-1; i>=1; i--)
{
for (k=samp; k>=0; k--)
{
printf(" ");
}
for (j=i; j>=1; j--)
{
printf("*");
}
samp = samp + 1;
printf("\n");
}
getch();
}Output #9:
Pattern9
Print Pattern Program #10
This program makes an animation using the pattern with color. Here we user cprintf function from conio.h library to print color. Also, we use delay function from dos.h library to hold screen some moment.
#include <stdio.h>
#include <conio.h>
#include<dos.h>
void main()
{
int row,c,tmp,color=1;
while(!kbhit()) //kbhit() returns true if anybody press any key from keyboard.
{
clrscr();
if (color==7)
color=1;
textcolor(color); // set corsor color
tmp=5;
for ( row = 1 ; row <= 5 ; row++ )
{
for ( c = 1 ; c <tmp ; c++ )
cprintf(" ");
tmp--;
for ( c = 1 ; c <= 2*row - 1 ; c++ )
cprintf("*");
printf("\n");
}
delay(200);
clrscr();
textcolor(color);
tmp=1;
for ( row = 5 ; row >= 1 ; row-- )
{
for ( c = 1 ; c < tmp ; c++ )
cprintf(" ");
tmp++;
for ( c = 1 ; c <= 2*row - 1 ; c++ )
cprintf("*");
printf("\n");
}
delay(200);
color++;
}
getch();
}Output #10
Print Pattern Program #11 (Print Inverted Right Triangle Star Pattern)
/* C Program to Print Inverted Right Triangle Star Pattern */
#include <stdio.h>
int main()
{
int Rows, i, j;
printf("Please Enter the Number of Rows: ");
scanf("%d", &Rows);
printf("\nPrinting Inverted Right Angled Triangle \n \n");
for ( i = Rows ; i > 0 ; i-- )
{
for ( j = i ; j > 0 ; j-- )
{
printf("* ");
}
printf("\n");
}
return 0;
}Output#11
* * * * * * *
* * * * * *
* * * * *
* * * *
* *
*Using Nested loop number, alphabet special characters you can be created your desired pattern for yourself just applying your thought and using nested loop properly.





