fak.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 27 Dec 2013 16:00:09 +0100
changeset 167 7c6b221900bf
parent 80 5d7057a1b202
permissions -rw-r--r--
Proper Datatypes to leave the 80's
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
     1
/**
77
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
     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
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     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
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
{
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
    if (i == 0)
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
}
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
int main (int argc, char **argv)
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
        printf ("READ ERROR\n");
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
        return EXIT_FAILURE;
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    }
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
    return EXIT_SUCCESS;
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
}