forked from karan/Projects
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathe_to_n_digit.c
52 lines (44 loc) · 922 Bytes
/
e_to_n_digit.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
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 18
unsigned int factorial(unsigned int n);
int main(int argc, char *argv[])
{
if (argc != 2) {
printf("USAGE: ./e_to_n_digit [DIGITS]\n");
return -1;
}
int N = atoi(argv[1]);
int N_real;
if (N < 1) {
printf("Digits should be bigger than 0\n");
return -1;
} else if (N > MAX) {
N_real = MAX;
} else if (N < 5) {
N_real = 5;
} else {
N_real = N;
}
int i;
double e = 1.0;
for (i = 1; i != N_real + 1; ++i) {
e += 1.0 / factorial(i);
}
if (N < MAX) {
printf("e = %.*f\n", N, e);
} else {
printf("e = %.*f\n", MAX - 1, e);
}
return 0;
}
unsigned int factorial(unsigned int n)
{
unsigned int i;
unsigned val = 1;
for (i = 1; i != n + 1; ++i) {
val *= i;
}
return val;
}