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 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 :
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 :
while(condition) { while(condition) { Block of statement(s); } Block of statement(s); }
The syntax of a nested do-while loop is as follows :
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 #1
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 12345
#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:
Print Pattern Program #3
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 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",i); } printf("\n"); } getch(); }
Output #4:
Print Pattern Program #5
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
* * * * * * * * * * * * * * *
#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:
Print Pattern Program #7
* * * * * * * * * * * * * * *
#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
* *** ***** ******* *********
#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 <= 2*row - 1 ; c++ ) printf("*"); printf("\n"); } getch(); }
Output #8:
Print Pattern Program #9
* * * * * * * * * * * * * * * * * * * * * * * * *
#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:
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.
Subroto Mondal
Latest posts by Subroto Mondal (see all)
- Installing and Configuring OpenShift: A Step-by-Step Guide for Linux Experts with Terminal Code - February 19, 2023
- Sed Command in Linux with Practical Examples - February 19, 2023
- Rsync Command uses with examples - February 19, 2023
thanks
Please put even the program for inverted right angle triangle
Updated.
Can you add a nested loop program to print
2 4 6
8
10 12 14