author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 16 Apr 2009 12:50:28 +0200 | |
changeset 66 | 2b4f786d9073 |
parent 63 | 5a82f89d607e |
child 77 | 49e0babccb23 |
permissions | -rw-r--r-- |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
1 |
/** |
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
2 |
* test/demos/fak.c |
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 |
||
63
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
9 |
unsigned long fak (short i) |
0 | 10 |
{ |
11 |
if (i == 0) |
|
12 |
return 1; |
|
13 |
else if (i > 0) |
|
14 |
return (i * fak (i - 1)); |
|
15 |
else |
|
16 |
return (i * fak (i + 1)); |
|
17 |
} |
|
18 |
||
19 |
int main (int argc, char **argv) |
|
20 |
{ |
|
63
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
21 |
short number; |
0 | 22 |
|
23 |
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
|
24 |
if (scanf ("%hd", &number) != 1) { |
0 | 25 |
printf ("READ ERROR\n"); |
26 |
return EXIT_FAILURE; |
|
27 |
} |
|
28 |
||
63
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
29 |
if (number < 0) { |
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
30 |
printf ("NUMBER FORMAT ERROR\n"); |
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
31 |
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
|
32 |
} |
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
33 |
|
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
34 |
printf ("The faktorial of %d is %lu\n", number, fak (number)); |
0 | 35 |
|
36 |
return EXIT_SUCCESS; |
|
37 |
} |