author | Markus Bröker<broeker.markus@googlemail.com> |
Fri, 20 Oct 2017 06:43:33 +0200 | |
changeset 169 | df7c720bcaa6 |
parent 80 | 5d7057a1b202 |
permissions | -rw-r--r-- |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
1 |
/** |
77 | 2 |
* fak.c |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
3 |
* Copyright (C) 2008 Markus Broeker |
0 | 4 |
*/ |
5 |
||
6 |
#include <stdio.h> |
|
7 |
#include <stdlib.h> |
|
8 |
||
80
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
9 |
unsigned long long fak (short i) |
0 | 10 |
{ |
11 |
if (i == 0) |
|
12 |
return 1; |
|
80
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
13 |
|
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
14 |
return (i * fak (i - 1)); |
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
15 |
} |
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
16 |
|
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
17 |
long double bigfak (short i) |
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
18 |
{ |
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
19 |
if (i == 0) |
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
20 |
return 1; |
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
21 |
|
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
22 |
return (i * bigfak (i - 1)); |
0 | 23 |
} |
24 |
||
25 |
int main (int argc, char **argv) |
|
26 |
{ |
|
63
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
27 |
short number; |
0 | 28 |
|
29 |
printf ("Enter a number: "); |
|
63
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
30 |
if (scanf ("%hd", &number) != 1) { |
0 | 31 |
printf ("READ ERROR\n"); |
32 |
return EXIT_FAILURE; |
|
33 |
} |
|
34 |
||
80
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
35 |
if (number < 0 || number > 170) { |
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
36 |
printf ("Range Error: %hd ! {0..170}\n", number); |
63
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
37 |
return EXIT_FAILURE; |
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
38 |
} |
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
39 |
|
80
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
40 |
if (number > 20) |
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
41 |
printf ("The factorial of %2d is %1.5Lg\n", number, bigfak (number)); |
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
42 |
else |
5d7057a1b202
fak.c: proper values
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
43 |
printf ("The factorial of %2d is %llu\n", number, fak (number)); |
0 | 44 |
|
45 |
return EXIT_SUCCESS; |
|
46 |
} |