-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorial.c
45 lines (31 loc) · 863 Bytes
/
factorial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
/* A program to prompt the user for a series of non-negative integers.
For each number entered, the program will calculate and display the
factorial of the number */
int factorial (int); /* function prototypes */
void main()
{
int n;
/* Prompt the user for the first number */
printf("Enter a non-negative number (-1 to end): ");
scanf("%d", &n);
fflush(stdin);
while (n != -1)
{ printf("Factorial of %d is %d\n\n", n, factorial(n)); // call the function
/* Prompt the user for the next number */
printf("Enter a non-negative number (-1 to end): ");
scanf("%d", &n);
fflush(stdin);
}
}
/* Function definition */
int factorial (int num) // function heading
{
int fact = 1;
while (num > 1)
{
fact = fact * num;
num --;
}
return fact; // return value (note must be of same data type)
}