In this tutorial, we will learn about Nested do while loop in C programming language
In C programming language, one do-while loop inside another do-while loop is known as nested do -while loop
Syntax
statements do{ statements do{ statements }while(condition); statements }while(condition);
initially, the initialization statement is executed only once and statements(do part) execute only one. Then, the flow of control evaluates the test expression.
When the test expression is true, the flow of control enter the inner loop and codes inside the body of the inner loop is executed and updating statements are updated. This process repeats until the test expression is false(inner while loop)
Then, when the test expression is false, loop exit from the inner loop and flow of control comes to the outer loop. Again, the flow of control evaluates test condition.
If test condition for outer loop returns as true, the flow of control executes the body of while loop statements and return as false. The flow of control stops execution and goes to rest.
program 1
This program displays a square number pattern in C language
#include <stdio.h> #include <stdlib.h> int main() { int i,j; i=1; printf("Square number pattern\n\n"); printf("Here your pattern\n\n"); do{ j=1; do{ printf("%d",j); j++; }while(j<=10); printf("\n"); i++; }while(i<=10); getch(); return 0; }
When the above code is executed, it produces the following results:
Square number pattern Here your pattern 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910
Program 2
This program displays a square star pattern in C language
#include <stdio.h> #include <stdlib.h> int main() { int i,j; i=1; printf("Asterisk number pattern\n\n"); printf("Here your pattern\n\n"); do{ j=1; do{ printf("*"); j++; }while(j<=9); printf("\n"); i++; }while(i<=9); getch(); return 0; }
When the above code is executed, it produces the following results:
Asterisk number pattern Here your pattern ********* ********* ********* ********* ********* ********* ********* ********* *********
The above program displays a square star pattern in C language using nested do-while loop
Program 3
This program displays a floyd’s triangle number pattern in C language using nested do-while loop
#include <stdio.h> #include <stdlib.h> int main() { int i,j; i=1; printf("Triangle number pattern\n\n"); printf("Here your pattern\n\n"); do{ j=1; do{ printf("%d",j); j++; }while(j<=i); printf("\n"); i++; }while(i<=9); getch(); return 0; }
When the above code is executed, it produces the following results:
Triangle number pattern Here your pattern 1 12 123 1234 12345 123456 1234567 12345678 123456789
This program displays a Floyd triangle number pattern using nested do-while loop in C language
Program 4
Print multiplication table using nested do-while loop in C
#include <stdio.h> #include <stdlib.h> int main() { int i,j; i=1; printf("Multiplication table\n\n"); printf("Here multiplication table\n\n"); do{ j=1; do{ printf("%d\t",i*j); j++; }while(j<=10); printf("\n"); i++; }while(i<=10); getch(); return 0; }
Sugested for you
Do-while loop in Java language
Do-while loop in C language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…