Nested While Loop: C Program


In this video tutorial we’ll demonstrate the use of nested while loop in C programming.

Related Read:
C Program to print Armstrong Numbers between 1 and 500

Number of Iterations In Nested Loops
Number of iterations will be equal to the number of iterations in the outer loop multiplied by the number of iterations in the inner loop.

Nested While Loop: C Program


[youtube https://www.youtube.com/watch?v=D7jMIAQkhgI]

YouTube Link: https://www.youtube.com/watch?v=D7jMIAQkhgI [Watch the Video In Full Screen.]


Source Code: Single While Loop: C Program

#include < stdio.h >

int main()
{
    int count1 = 1;

    while(count1 <= 5)
    {
        printf("%d\n", count1);
        count1++;
    }

    return 0;
}

Output:
1
2
3
4
5

Source Code: Nested While Loop: C Program

#include < stdio.h >

int main()
{
    int count1 = 1, count2;

    while(count1 <= 5)
    {
        printf("%d\n", count1);
        count2 = 2;

        while(count2)
        {
            printf("   %d\n", count2);
            count2--;
        }

        count1++;
    }

    return 0;
}

Output:

1
   2
   1
2
   2
   1
3
   2
   1
4
   2
   1
5
   2
   1

For every single iteration of the outer while loop, the inner while loop completes its iterations.

A loop inside another loop is called a nested loop. Consider a nested loop where the outer loop runs x times and consists of another loop inside it. The inner loop runs y times. Then, the total number of times the inner loop runs during the program execution is x*y times.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *