...

View Full Version : for

Xiang
02-07-2003, 04:20 PM
Dear sir,

Refers to the below codes, my program can't be run in properly. Do you know why???

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

void main()
{
int n, count, factorial;

clrscr();
printf("Enter a positive integer: ");
scanf("%i", &n);
printf("Integer \t Factorial\n");
for (n = 1; n >= 1; --n)
{ printf(" %3i ", n);
factorial = 1;
for (count = 1; count <= n; ++count)
factorial *= count;
printf("%i\n", factorial);
}
getch();
}

Thanks

Xiang

joh6nn
02-07-2003, 06:29 PM
clrscr();
printf("Enter a positive integer: ");
scanf("%i", &n);
printf("Integer \t Factorial\n");
for (n = 1; n >= 1; --n)
{ printf(" %3i ", n);
factorial = 1;
for (count = 1; count <= n; ++count)
factorial *= count;
printf("%i\n", factorial);
}

the program can never run, because n is less than one. n-- means use n, then subtract one, but --n means subtract one from n, and then use n. so for (n = 1; n >= 1; --n) means that n is 0, and therefore, for (count = 1; count <= n; ++count) can never work, because count is 1, and greater than 0