As we have seen already about the if statements, now let us see about the for loops. Nested for loops are a bit difficult to understand as it could be asked in different shapes used in mathematics.
A. PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
int i,j;
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=0;j<=5;j++)
{
printf("%d",i);
}
{
for(j=0;j<=5;j++)
{
printf("%d",i);
}
printf("\n");
}
}
OUTPUT:
000000
111111
222222
333333
444444
555555
B. PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=0;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
}
#include<string.h>
void main()
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=0;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
}
OUTPUT:
0
11
222
3333
44444
555555
C. PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("%d",i);
}
printf("\n");
}
}
OUTPUT:
000000
11111
2222
333
44
5
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("%d",j);
}
printf("\n");
}
}
OUTPUT:
543210
54321
5432
543
54
5
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=0;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
}
OUTPUT:
0
01
012
0123
01234
012345
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=5;j>=5-i;j--)
{
printf("%d",j);
}
printf("\n");
}
}
OUTPUT:
5
54
543
5432
54321
543210
Tags: different for loops, for loops for interview, triangle for loops, for loops
Copy Article URL:
No comments:
Post a Comment