A nested for loop is a loop inside another loop. The outer loop is executed first, followed by the inner loop. The inner loop is executed completely for each iteration of the outer loop. Here's the general structure of a nested for loop:
Syntax:
for( initial ; Condition ; increment / decrement ) // Outer Loop Statements
{
for( initial ; Condition ; increment / decrement ) // Inner Loop Statements
{
// code block to be executed
}
}
In this loop, the outer loop runs first and initializes a counter variable. The outer loop also has a condition that is checked before each iteration. If the condition is true, the inner loop is executed. The inner loop also has a counter variable, a condition, and an update statement. The code block inside the inner loop is executed for each iteration of the inner loop.
This program prints a pattern of numbers in a triangle shape. The input value a is the number of rows in the triangle. Here is how the program works:
#include<iostream> using namespace std; int main() { int i,j,a; cout<<"\nEnter the value:"; cin>>a; for(i=1;i<=a;i++) { cout<<"\n"; for(j=1;j<=i;j++) { cout<<i; } } return 0; }To download raw file Click Here
Enter the value:5 1 22 333 4444 55555
#include<iostream> using namespace std; int main() { int i,j,a; cout<<"\nEnter the value:"; cin>>a; for(i=1;i<=a;i++) { cout<<"\n"; for(j=1;j<=i;j++) { cout<<j; } } return 0; }To download raw file Click Here
Enter the value:5 1 12 123 1234 12345
#include<iostream> using namespace std; int main() { int i,j,a,b=1; cout<<"\nEnter the value:"; cin>>a; for(i=1;i<=a;i++) { cout<<"\n"; for(j=1;j<=i;j++) { cout<<" "<<b; b++; } } return 0; }To download raw file Click Here
Enter the value:5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<iostream> using namespace std; int main() { int i,j,a; cout<<"\nEnter the value:"; cin>>a; for(i=1;i<=a;i++) { cout<<"\n"; for(j=1;j<=i;j++) { cout<<(i-j+1); } } return 0; }To download raw file Click Here
Enter the value:10 Enter the value:5 1 21 321 4321 54321
#include<iostream> using namespace std; int main() { int i,j,a; cout<<"\nEnter the value:"; cin>>a; for(i=1;i<=a;i++) { cout<<"\n"; for(j=1;j<=(a-i);j++) { cout<<" "; } for(j=1;j<=i;j++) { cout<<j; } } return 0; }To download raw file Click Here
Enter the value:5 1 12 123 1234 12345
#include<iostream> using namespace std; int main() { int i,j,a; cout<<"\nEnter the value:"; cin>>a; for(i=1;i<=a;i++) { cout<<"\n"; for(j=a-i+1;j>0;j--) { cout<<j; } } return 0; }To download raw file Click Here
Enter the value:5 54321 4321 321 21 1
#include<iostream> using namespace std; int main() { int i,j,a; cout<<"\nEnter the value:"; cin>>a; for(i=1;i<=a;i++) { cout<<"\n"; for(j=1;j<=i;j++) { cout<<" "; } for(j=1;j<=(a-i+1);j++) { cout<<j; } } return 0; }To download raw file Click Here
Enter the value:6 123456 12345 1234 123 12 1
#include<iostream> using namespace std; int main() { int i,j,n; cout<<"\nEnter the value :"; cin>>n; for(i=1;i<=n;i++) { cout<<"\n"; for(j=1;j<=(n-i);j++) { cout<<" "; } for(j=1;j<=i;j++) { cout<<" "<<j; } for(j=1;j<i;j++) { cout<<" "<<(i-j); } } for(i=1;i<=n;i++) { cout<<"\n"; for(j=1;j<=i;j++) { cout<<" "; } for(j=1;j<(n-i);j++) { cout<<" "<<j; } for(j=(n-i);j>0;j--) { cout<<" "<<j; } } return 0; }To download raw file Click Here
Enter the value :5 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1 1
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions