Understanding the Nested For in C Programming


C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Let's observe an example of nesting loops in C. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop.

Syntax:
   for( initial ; Condition ; increment / decrement )  // Outer Loop Statements
   {
            for( initial ; Condition ; increment / decrement )  // Inner Loop Statements
            {
                       . . . .
            }
   }

This program is written in C language and uses nested for loops to print two different patterns of asterisks (*).

The first for loop, with the loop variable "i" starts at 0 and runs for 5 iterations. Within this for loop, there is another for loop, with the loop variable "j", which also starts at 0 and runs for 5 iterations. The inner for loop prints an asterisk (*) on each iteration, so it will print 5 asterisks in total during each iteration of the outer for loop. This creates a pattern of 5 rows of 5 asterisks each.

Then a line of separator is added to distinguish the two different patterns

The second set of nested loops starts with i = 1 and runs for 5 iterations. Within this for loop, there is another for loop, with the loop variable "j" which starts at 1 and runs for i iterations. The inner for loop prints an asterisk (*) on each iteration, so it will print i asterisks in total during each iteration of the outer for loop. This creates a pattern of 5 rows of asterisks, with the first row having 1 asterisk, the second row having 2 asterisks, and so on, up to the fifth row which has 5 asterisks.

In the end, the main function returns 0 to indicate successful execution of the program.


Source Code

//Nested For Loop
 
/*
    *****
    *****
    *****
    *****
    *****
 
    *
    **
    ***
    ****
    *****
*/
 
#include<stdio.h>
int main()
{
    int i,j;
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            printf("*");
        }
        printf("\n");
    }
    printf("\n----------------------------\n");
 
    for(i=1;i<=5;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}
 
To download raw file Click Here

Output

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

----------------------------
*
* *
* * *
* * * *
* * * * *

Programming Exercises

1. Factors of a Number

Write a program to find the factor of the given number.

2. Prime Number

Write a program to find whether the given number is prime or not.

3. Armstrong Number

Write a program to find the given number is Armstrong number or not.

Example:
153

1³ = 1
5³ = 125
3³ = 27

1 + 125 + 27 = 153

4. Armstrong Numbers Between 1 to 10000

Write a program to print the Armstrong numbers between 1 to 10000.

5. Count Odd and Even Numbers

Write a program to count and print the number of odd and even numbers.

Example:
[2, 85, 6, 77, 5]

Enter the limit: 5
Enter the value: 10
Enter the value: 25
Enter the value: 65
Enter the value: 12
Enter the value: 45

Even: 2
Odd: 3

6. Reverse of a Number

Write a program to find the reverse of an n-digit number using a while loop.
Clue: Use % and // operators.

7. Decimal to Binary

Write a program to find the binary number from a given decimal number.

8. Binary to Decimal

Write a program to find the decimal number from a given binary number.

9. Factorial

Write a program to find the factorial of the given number.

Enter the number: 5
Factorial of 5 is: 120

10. Fibonacci Series

Write a program to find the Fibonacci series of the given number.

Enter the limit: 8
0
1
1
2
3
5
8
13

11. Perfect Number

Write a program to find whether the given number is a perfect number.

Example:
6

Factors: 1, 2, 3
1 + 2 + 3 = 6

12. Perfect Numbers Between 1 and 1000

Write a program to print the perfect numbers between 1 and 1000.

13. Strong Number

Write a program to find whether the given number is a strong number.

Example:
145

Digits:
1
4
5

Factorials:
1! = 1
4! = 24
5! = 120

Sum:
1 + 24 + 120 = 145

145 is a Strong Number

14. Power of a Number

Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another.

Example:
Enter the Base: 2
Enter the Power: 3
Clue: 2 × 2 × 2 = 8

Result: 8

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C