fak.c: proper values
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:51:15 +0200
changeset 80 5d7057a1b202
parent 79 1499176750b9
child 81 6cfca66d2f01
fak.c: proper values The normal fak function prints proper values from 0-20 and the bigfak function "guesses" up to 170! committer: Markus Bröker <mbroeker@largo.homelinux.org>
fak.c
--- a/fak.c
+++ b/fak.c
@@ -6,14 +6,20 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-unsigned long fak (short i)
+unsigned long long fak (short i)
 {
     if (i == 0)
         return 1;
-    else if (i > 0)
-        return (i * fak (i - 1));
-    else
-        return (i * fak (i + 1));
+
+    return (i * fak (i - 1));
+}
+
+long double bigfak (short i)
+{
+    if (i == 0)
+        return 1;
+
+    return (i * bigfak (i - 1));
 }
 
 int main (int argc, char **argv)
@@ -26,12 +32,15 @@
         return EXIT_FAILURE;
     }
 
-    if (number < 0) {
-        printf ("NUMBER FORMAT ERROR\n");
+    if (number < 0 || number > 170) {
+        printf ("Range Error: %hd ! {0..170}\n", number);
         return EXIT_FAILURE;
     }
 
-    printf ("The faktorial of %d is %lu\n", number, fak (number));
+    if (number > 20)
+        printf ("The factorial of %2d is %1.5Lg\n", number, bigfak (number));
+    else
+        printf ("The factorial of %2d is %llu\n", number, fak (number));
 
     return EXIT_SUCCESS;
 }