58 lines
1.1 KiB
C
58 lines
1.1 KiB
C
|
/*
|
|||
|
file name: factorUsingIterative.c
|
|||
|
Description: Factorial numbers count using iterative
|
|||
|
<EFBFBD>Q<EFBFBD>ΰj<EFBFBD>鰵N<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>p<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
|
|||
|
#include <stdio.h>
|
|||
|
#include <ctype.h>
|
|||
|
#include <stdlib.h>
|
|||
|
|
|||
|
/* <20><><EFBFBD>ƭ쫬<C6AD>ŧi */
|
|||
|
long Factorial(long);
|
|||
|
void flushBuffer(void);
|
|||
|
|
|||
|
int main()
|
|||
|
{
|
|||
|
char ch;
|
|||
|
long n;
|
|||
|
|
|||
|
printf("-----Factorial counting using Iterative-----");
|
|||
|
do {
|
|||
|
printf("\nEnter a number(0 <= n <= 12) to count n!: ");
|
|||
|
scanf("%ld",&n);
|
|||
|
flushBuffer();
|
|||
|
if (n < 0 || n > 12)
|
|||
|
printf("Input out of range!\n");
|
|||
|
else
|
|||
|
printf("%ld! = %ld\n", n, Factorial(n));
|
|||
|
|
|||
|
printf("Continue(y/n)? ");
|
|||
|
ch = toupper(getchar());
|
|||
|
} while (ch == 'Y');
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
long Factorial(long n)
|
|||
|
{
|
|||
|
long sum = 1;
|
|||
|
int i;
|
|||
|
|
|||
|
if (n == 0 || n ==1) /* <20><> n=0 <20><> n=1 <20><>, 0!=1, 1!=1 */
|
|||
|
return (1); /* <20>G<EFBFBD><47><EFBFBD><EFBFBD><EFBFBD>Ǧ^1 */
|
|||
|
else {
|
|||
|
for (i = 2; i<= n; i++) /* sum <20>O<EFBFBD><4F><EFBFBD>ثe<D8AB><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>M */
|
|||
|
sum *= i; /* sum <20>P i <20>ۭ<EFBFBD><DBAD><EFBFBD><EFBFBD>M<EFBFBD><4D><EFBFBD>^ sum <20><> */
|
|||
|
}
|
|||
|
return (sum);
|
|||
|
}
|
|||
|
|
|||
|
void flushBuffer()
|
|||
|
{
|
|||
|
while (getchar() != '\n') {
|
|||
|
continue;
|
|||
|
}
|
|||
|
}
|
|||
|
|