Are you in college, or high school? This is like every programmers first problem. Did you even try to Google the answer?
I didn't compile and check this, but I'm pretty sure this will work.
[CODE=C]
#include <stdio.h>
int factorial ( int n);
int main ( int argc, char ** argv) {
int n = 0;
int i = 0;
int product = 0;
/* Scan std input and store the value into the address of n */
scanf ("%d", &n);
printf ( " n Factorial\n");
printf ( " - ---------\n");
product = n;
for ( i = 1; i != n; i ++ {
product *= ( product - i );
printf ( " %d", i );
printf ( " " );
printf ( "%d", product );
printf ( "\n" );
}
return 0;
}
/**
*
* This is the recursive version of the for-loop above. I'm not calling it in
* this program. This is much faster, but I didn't use this because a) I
* was taught only to call printf() in main() only, and 2), I couldn't figure
* out how to use it in this context.
*
*/
int factorial ( int n ) {
printf (" n");
if ( n <= 1)
return 0;
return (n * factorial( n -1) );
}
[/CODE]
EDIT:
And if you have any other questions, go to
Stack Overflow. The programming boards on this website are largely ignored.