fak.c
changeset 63 5a82f89d607e
parent 9 c3fecc82ade6
child 77 49e0babccb23
equal deleted inserted replaced
62:b7061c0e239f 63:5a82f89d607e
     4  */
     4  */
     5 
     5 
     6 #include <stdio.h>
     6 #include <stdio.h>
     7 #include <stdlib.h>
     7 #include <stdlib.h>
     8 
     8 
     9 long fak (int i)
     9 unsigned long fak (short i)
    10 {
    10 {
    11     if (i == 0)
    11     if (i == 0)
    12         return 1;
    12         return 1;
    13     else if (i > 0)
    13     else if (i > 0)
    14         return (i * fak (i - 1));
    14         return (i * fak (i - 1));
    16         return (i * fak (i + 1));
    16         return (i * fak (i + 1));
    17 }
    17 }
    18 
    18 
    19 int main (int argc, char **argv)
    19 int main (int argc, char **argv)
    20 {
    20 {
    21     int number;
    21     short number;
    22 
    22 
    23     printf ("Enter a number: ");
    23     printf ("Enter a number: ");
    24     if (scanf ("%d", &number) < 0) {
    24     if (scanf ("%hd", &number) != 1) {
    25         printf ("READ ERROR\n");
    25         printf ("READ ERROR\n");
    26         return EXIT_FAILURE;
    26         return EXIT_FAILURE;
    27     }
    27     }
    28 
    28 
    29     printf ("The faktorial of %d is %ld\n", number, fak (number));
    29     if (number < 0) {
       
    30         printf ("NUMBER FORMAT ERROR\n");
       
    31         return EXIT_FAILURE;
       
    32     }
       
    33 
       
    34     printf ("The faktorial of %d is %lu\n", number, fak (number));
    30 
    35 
    31     return EXIT_SUCCESS;
    36     return EXIT_SUCCESS;
    32 }
    37 }