Nested for loop
Note:
Before beginning with this lesson you should study the previous lessons of C Programming Language Tutorial VIII and
should read beginning part of C Programming Language Tutorial IX. This is mandatory to understand the
concept of nested for loop.
Let
us perform some programs of nested for loop:
You
can write first program as you had done in nested while loop in which you had
printing value of i and j.
//By
nested for loop program to print Asteriks Graph
#include<stdio.h>
#include<conio.h>
void
main()
{
int r,c;
clrscr();
for(r=1;r<=5;r++) //rows start
from one till 5
{
for(c=1;c<=r;c++) //columns start
from one till current value of r
{
printf("* ");
}
printf("\n"); //after
finishing inner loop used ‘\n’ for a new line
}
getch();
}
|
o/p:
*
*
*
*
* *
*
* * *
*
* * * *
|
//Asterik's
graph
#include<stdio.h>
#include<conio.h>
main()
{
int r,c,n;
clrscr();
printf("\nEnter number of rows");
scanf("%d",&n);
for(r=1;r<=n;r++) //n is used
instead of fixed value 5
{
for(c=1;c<=r;c++)
{
printf("* ");
}
printf("\n");
}
getch();
}
|
Enter
number of rows: 7
*
*
*
*
* *
*
* * *
*
* * * *
*
* * * * *
*
* * * * * *
|
What
should do if you want a opposite pattern i.e. first 5 rows then 4..3..2..1. for
this program you have to change inner loop to for(c=n;c>=r;c- -). After this
change you will get following output
*
* * * * * *
*
* * * * *
*
* * * *
*
* * *
*
* *
*
*
*
|
//Asterik's
graph in triangle shape
#include<stdio.h>
#include<conio.h>
main()
{
int r,c,n,sp;
clrscr();
printf("\n Enter number of rows: ");
scanf("%d",&n);
for(r=1;r<=n;r++)
{
for(sp=1;sp<=r;sp++) //space is
used for getting triangle pattern. Space starts from 1 till current value of
row i.e. in 1st row one space, in 2nd row two spaces
like this spaces will print.
{
printf(" "); //put a single
space in printf
}
for(c=n;c>=r;c--)
{
printf("* ");
}
printf("\n");
}
getch();
}
|
o/p:
Enter
no. of rows?
10
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
|
Now
you can try following output
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
|
*
* * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
|
Let
us use values instead of ‘*’
//By
nested for loop program to print Asteriks Graph
#include<stdio.h>
#include<conio.h>
void
main()
{
int r,c;
clrscr();
for(r=1;r<=5;r++) //rows start
from one till 5
{
for(c=1;c<=r;c++) //columns start
from one till current value of r
{
printf("%d ",c); //current
value of column
}
printf("\n"); //after
finishing inner loop used ‘\n’ for a new line
}
getch();
}
|
o/p:
1
1
2
1
2 3
1
2 3 4
1
2 3 4 5
|
Assignments
1
2
2
3
3 3
4
4 4 4
5
5 5 5 5
|
1
2
3
4
5 6
7
8 9 10
|
5
5 5 5 5
4
4 4 4
3
3 3
2
2
1
|
Let
us use only 0 and 1 instead of values
//By
nested for loop program to print Asteriks Graph
#include<stdio.h>
#include<conio.h>
void
main()
{
int r,c;
clrscr();
for(r=1;r<=5;r++) //rows start
from one till 5
{
for(c=1;c<=r;c++) //columns start
from one till current value of r
{
If((c+1)%2==0) //if
if is true then it will print 1 else 0
printf("1 ");
else
printf("0 ");
}
printf("\n"); //after
finishing inner loop used ‘\n’ for a new line
}
getch();
}
|
o/p:
1
1
0
1
0 1
1
0 1 0
1
0 1 0 1
|
Assignments
1
0
0
1
1 1
0
0 0 0
1
1 1 1 1
|
1
0
1
1
0 1
0
1 0 1
1
0 1 0 1
|
0
1
1
0
0 0
1
1 1 1
0
0 0 0 0
|
No comments:
Post a Comment