Outer for loop selects the rows. Inner for loop prints elements of that row. Next outer for loop selects the next row, and the inner for loop prints elements for that selected row. This continues until all the elements of the Matrix are printed.
Video Tutorial: C Program To Print Matrix using Nested For Loop
Note: 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.
Total 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.
Video Tutorial: Nested For Loop In C Programming Language
Source Code: Nested For Loop In C Programming Language
#include<stdio.h>
int main()
{
int i, j;
for(i = 1; i <= 3; i++)
{
printf("\n%d Apple\n", i);
for(j = 1; j <= 2; j++)
{
printf("\t%d Oracle\n", j);
}
}
return 0;
}
Output:
1 Apple
1 Oracle
2 Oracle
2 Apple
1 Oracle
2 Oracle
3 Apple
1 Oracle
2 Oracle
Explanation of above C source code Here when i value is 1 – in first iteration. j value gets initialized to 1. And inner for loop executes until j value is equal to 2. For second iteration of outer for loop i value is 2, now again j value gets initialized to 1. Now the inner for loop executes until j value is equal to 2. For third iteration of outer for loop i value is 3, now again j value gets initialized to 1. Now again the inner for loop executes until j value is equal to 2.
Source Code: Nested For Loop In C Programming Language
#include<stdio.h>
int main()
{
int i, j;
for(i = 1; i <= 3; i++)
{
printf("\n%d Apple\n", i);
for(j = 1; j <= i; j++)
{
printf("\t%d Oracle\n", j);
}
}
return 0;
}
Output:
1 Apple
1 Oracle
2 Apple
1 Oracle
2 Oracle
3 Apple
1 Oracle
2 Oracle
3 Oracle
Explanation of above C source code During first iteration of outer for loop, i value will be 1. Now j gets initialized to 1 and inner for loop iterates until j is equal to the value of i(that is 1). Now for second iteration of outer for loop i value is 2. Again j gets initialized to 1 and inner for loop executes until j value is equal to i. and so on ..
Source Code: Nested For Loop In C Programming Language
#include<stdio.h>
int main()
{
int i, j, k;
for(i = 1; i <= 5; i++)
{
printf("\n%d Apple\n", i);
for(j = 1; j <= 3; j++)
{
printf("\t%d Oracle\n", j);
for(k = 1; k <= 2; k++)
{
printf("\t\t%d IBM\n", k);
}
}
}
return 0;
}
Output:
1 Apple
1 Oracle
1 IBM
2 IBM
2 Oracle
1 IBM
2 IBM
3 Oracle
1 IBM
2 IBM
2 Apple
1 Oracle
1 IBM
2 IBM
2 Oracle
1 IBM
2 IBM
3 Oracle
1 IBM
2 IBM
3 Apple
1 Oracle
1 IBM
2 IBM
2 Oracle
1 IBM
2 IBM
3 Oracle
1 IBM
2 IBM
4 Apple
1 Oracle
1 IBM
2 IBM
2 Oracle
1 IBM
2 IBM
3 Oracle
1 IBM
2 IBM
5 Apple
1 Oracle
1 IBM
2 IBM
2 Oracle
1 IBM
2 IBM
3 Oracle
1 IBM
2 IBM
Note: We can have while loop inside a for loop or a for loop inside a while loop. This is also called as nested looping.
Source Code: C Program To Calculate Sum of Natural Numbers Between Range using For Loop
#include<stdio.h>
int main()
{
int min, max, temp, count, sum = 0;
printf("Enter 2 positive numbers\n");
scanf("%d%d", &min, &max);
if(min > max)
{
temp = min;
min = max;
max = temp;
}
printf("\nNatural Numbers from %d to %d are:\n", min, max);
for(count = min; count <= max; count++)
{
printf("%d\n", count);
sum = sum + count;
}
printf("Sum of Natural Numbers from %d to %d is %d\n", min, max, sum);
return 0;
}
Output 1: Enter 2 positive numbers 5 10
Natural Numbers from 5 to 10 are: 5 6 7 8 9 10 Sum of Natural Numbers from 5 to 10 is 45
Output 2: Enter 2 positive numbers 14 10
Natural Numbers from 10 to 14 are: 10 11 12 13 14 Sum of Natural Numbers from 10 to 14 is 60
Logic To Calculate Sum of Natural Numbers Between Range using For Loop
We ask the user to enter minimum and maximum number(i.e., the range) and we store it inside variable min and max. If value of min is greater than value of max, then we swap the values of min and max.
We initialize the variable count to min and iterate through the for loop until count value is less than or equal to value of max. We keep incrementing the value of count by 1 for each iteration of for loop.
Inside for loop we add the value of count to previous value of sum and once the control exits the for loop we display the value present in variable sum.
Source Code: C Program to Calculate the Sum of Natural Numbers From 1 to N using For Loop
#include<stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive number\n");
scanf("%d", &num);
printf("\nNatural Numbers from 1 to %d are:\n", num);
for(count = 1; count <= num; count++)
{
printf("%d\n", count);
sum = sum + count;
}
printf("\nSum of natural numbers from 1 to %d is: %d\n", num, sum);
return 0;
}
Output 1: Enter a positive number 5
Natural Numbers from 1 to 5 are: 1 2 3 4 5
Sum of natural numbers from 1 to 5 is: 15
Output 2: Enter a positive number 10
Natural Numbers from 1 to 10 are: 1 2 3 4 5 6 7 8 9 10
Sum of natural numbers from 1 to 10 is: 55
Logic To Calculate the Sum of Natural Numbers From 1 to N
We ask the user to enter a positive number and store it in variable num.
For Loop Variable count is assigned value 1. For loop executes until count value is less than or equal to the user entered number. For each iteration of for loop, count value increments by 1.
Inside for loop, we display the value of count(which is natural number between 1 and user entered number). We also keep adding the value of count to the previous value of variable sum. Once the control exits for loop, sum will have the sum of all the natural numbers between 1 to user entered number.
Source Code: C Program to Print Natural Numbers Between Two Numbers using for loop
#include<stdio.h>
int main()
{
int min, max, temp, count;
printf("Enter 2 positive numbers\n");
scanf("%d%d", &min, &max);
if(min > max)
{
temp = min;
min = max;
max = temp;
}
printf("Natural numbers from %d to %d are:\n", min, max);
for(count = min; count <= max; count++)
{
printf("%d\n", count);
}
return 0;
}
Output 1: Enter 2 positive numbers 10 15 Natural numbers from 10 to 15 are: 10 11 12 13 14 15
Output 2: Enter 2 positive numbers 15 10 Natural numbers from 10 to 15 are: 10 11 12 13 14 15
Logic To Print Natural Numbers Between Two Numbers using for loop
We ask the user to enter 2 numbers, and store it inside variables min and max. For loop keeps iterating till min is less than or equal to max. We assign the value of min to count and keep incrementing the value of count by 1 for each iteration of the for loop. Once value of count is greater than value of max, control exits for loop.