...

View Full Version : for



Xiang
02-16-2003, 09:23 AM
Dear sir,

I use for loop to produce the following output, but my second for can't be functioned properly .

Proper output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

#include <stdio.h>
#include <conio.h>

void main(void)
{
int a;
int b;

clrscr();

for (a = 1; a <= 9; a++)
{ for (b = 1; b <= a; b++)
printf("%i\t", b);
printf("\n");
}


for (a = 1; a <= 8; a++)
{ for (b = 8; b >= a; b--)
printf("%i\t", b);
printf("\n");
}

getch();
}


Thanks


Xiang

DaZ
02-17-2003, 10:34 AM
Originally posted by Xiang

for (a = 1; a <= 8; a++)
{ for (b = 8; b >= a; b--)
printf("%i\t", b);
printf("\n");
}


try this for your second for statement
-----------------------------------------------

ive just reversed the numbers that it prints out.


for (a = 8; a >= 1; a--)
{ for (b = 1; b <= a; b++)
printf("%i\t", b);
printf("\n");
}

see if thats ok



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum