forked from SanthanCH/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial.c
More file actions
41 lines (32 loc) · 738 Bytes
/
factorial.c
File metadata and controls
41 lines (32 loc) · 738 Bytes
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
/* A program to find
* the factorial of a number given by the user.
*/
#include <stdio.h>
/*User defined function*/
void factorial(int n)
{
int i;
unsigned long long fact = 1;
// shows error if the user enters a negative integer
if (n < 0)
printf("\nError! Factorial of a negative number doesn't exist.");
else
{
for (i = 1; i <= n; ++i)
{
fact *= i; // Multiplying each increment
}
printf("\nFactorial of %d = %llu", n, fact);
}
}
/*The main driver code*/
int main(void)
{
int n;
/*Taking the user inputs*/
printf("Enter an integer: ");
scanf("%d", &n);
/*Function call*/
factorial(n);
return 0;
}