fak.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:50:39 +0200
changeset 72 4103c76d5bf2
parent 63 5a82f89d607e
child 77 49e0babccb23
permissions -rw-r--r--
sort.c: bubblesort fixed * the bubblesort algorithm has an abort-condition * the linearsort algorithm has not an abort-condition committer: Markus Bröker <mbroeker@largo.homelinux.org>
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
/**
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
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
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
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;
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
    else if (i > 0)
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
        return (i * fak (i - 1));
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    else
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
        return (i * fak (i + 1));
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
}
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
int main (int argc, char **argv)
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
        printf ("READ ERROR\n");
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
        return EXIT_FAILURE;
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    }
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
    return EXIT_SUCCESS;
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
}