BACK TO 10+2 STEM LAB
10+2 MATHEMATICS & DRAWINGS•2016-08-15•7 MIN READ

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.
Notice

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:

bash
> 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); }

SHELL
**The syntax of a nested while loop  is as follows :**

c while(condition) { while(condition) { Block of statement(s); } Block of statement(s); }

SHELL
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 );

SHELL
## 

#

# 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 #1

c 1 22 333 444 55555

c
#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:

nested pattern1nested pattern1

Print Pattern Progam #2

c
1
 12
 123
 1234
 12345

c #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(); }

SHELL
**Output #2:**

[![Pattern2](http://technhit.in/wp-content/uploads/2016/08/nested-pattern2.png)](http://technhit.in/wp-content/uploads/2016/08/nested-pattern2.png)

## Print Pattern Program #3

c 12345 1234 123 12 1

c
#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:

Pattern3Pattern3

Print Pattern Program #4

c
55555
4444
333
22
1

c #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(); }

SHELL
**Output #4:**

[![Pattern4](http://technhit.in/wp-content/uploads/2016/08/nested-pattern4.png)](http://technhit.in/wp-content/uploads/2016/08/nested-pattern4.png)

## Print Pattern Program #5

c 1 232 34543 4567654 567898765

c
#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:

Pattern5Pattern5

Print Pattern Program #6

c
* * * * *
  * * * *
    * * *
      * *
        *

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(); }

SHELL
**Output #6:**

[![pattern6](http://technhit.in/wp-content/uploads/2016/08/nested-pattern6.png)](http://technhit.in/wp-content/uploads/2016/08/nested-pattern6.png)

## Print Pattern Program #7

c *

c
#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:

Pattern7Pattern7

Print Pattern Program #8

c
*
   ***
  *****
 *******
*********

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(); }

SHELL
** Output #8:**

[![Pattern8](http://technhit.in/wp-content/uploads/2016/08/nested-pattern8.png)](http://technhit.in/wp-content/uploads/2016/08/nested-pattern8.png)

## Print Pattern Program #9

c *

c
#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:

Pattern9Pattern9

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.

c
#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 animationprint animation

Print Pattern Program #11 (Print Inverted Right Triangle Star Pattern)

c
/* 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

c
* * * * * * *
 * * * * * *
 * * * * *
 * * * *
 * *
 *

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.

π
TechNHIT STEM Faculty
Department of Applied Mathematics & Physics
Nested Loop with example C Language | TechNHIT 10+2 STEM Lab | TechNHIT