U
upyzl
I want to calculate n!,and my C code is as follows:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int nonnega_int, counter;
unsigned long result = 1;
printf("Enter a nonnegative integer (as n)\n");
printf("and I'll calculate its factorial(EOF to end):\n");
scanf("%d", &nonnega_int);
while ( ( nonnega_int = getchar() ) != EOF ){
if ( nonnega_int > 0){
for ( counter = 1; counter <= nonnega_int; counter++){
result *= counter;
}
}
else if ( nonnega_int = 0)
result = 0;
printf("n! = %lu\n", result);
}
system("pause");
return 0;
}
but the result is always 3628800
I don't know why it is wrong.
Please help me check it out.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int nonnega_int, counter;
unsigned long result = 1;
printf("Enter a nonnegative integer (as n)\n");
printf("and I'll calculate its factorial(EOF to end):\n");
scanf("%d", &nonnega_int);
while ( ( nonnega_int = getchar() ) != EOF ){
if ( nonnega_int > 0){
for ( counter = 1; counter <= nonnega_int; counter++){
result *= counter;
}
}
else if ( nonnega_int = 0)
result = 0;
printf("n! = %lu\n", result);
}
system("pause");
return 0;
}
but the result is always 3628800
I don't know why it is wrong.
Please help me check it out.