Tuesday 7 January 2014

Nested loop in c

*  /*program to print this
1
12
123
1234
12345
*/

#include<stdio.h>
main()
{
int i,j;
for(i=1; i<=5; ++i)
{
for(j=1; j<=i; ++j)
{
printf("%d",j);
}
printf("\n");
}
}


/* program to print this
1
22
333
4444
55555
*/

#include<stdio.h>
main()
{
int i,j;
for(i=1; i<=5; ++i)
{
for(j=1; j<=i; ++j)
{
printf("%d",i);
}
printf("\n");
}
}


*    /* Program to print this

    1
   121
  12321
 1234321
123454321
*/

#include<stdio.h>
main()
{
int i,sp=4,j;
for(i=1; i<=5; ++i)
{
for(j=1; j<=sp; ++j)
{
printf(" ");
}
for(j=1; j<=i; ++j)
{
printf("%d",j);
}
for(j=j-2; j>=1; --j)
{
printf("%d",j)
}
printf("\n");
--sp;
}
}

No comments:

Post a Comment