Home » Archimedes archive » Acorn User » AU 1995-10.adf » !Regulars » Regulars/C/c/Factorial

Regulars/C/c/Factorial

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Acorn User » AU 1995-10.adf » !Regulars
Filename: Regulars/C/c/Factorial
Read OK:
File size: 0292 bytes
Load address: 0000
Exec address: 0000
File contents
/* C for yourself example program - October 1995
   by Steve Mumford */


#include <stdio.h>

int     main(void);
int     fact(int);

int
main(void)
{
        int x, answer;

        printf("Please enter a number to give the factorial function:\n");
        printf("(between 1 and 15)\n\n");
        scanf("%d", &x);

        if (x > 0 && x <= 15) {
                answer = fact(x);
        } else {
                printf("Sorry - number out of range.\n");
        }

        printf("The answer is: %d\n", answer);
}

int
fact(int z)
{
        if (z == 1) {
                return (1);
        } else {
                return ( z * fact(z-1));
        }
}
00000000  2f 2a 20 43 20 66 6f 72  20 79 6f 75 72 73 65 6c  |/* C for yoursel|
00000010  66 20 65 78 61 6d 70 6c  65 20 70 72 6f 67 72 61  |f example progra|
00000020  6d 20 2d 20 4f 63 74 6f  62 65 72 20 31 39 39 35  |m - October 1995|
00000030  0a 20 20 20 62 79 20 53  74 65 76 65 20 4d 75 6d  |.   by Steve Mum|
00000040  66 6f 72 64 20 2a 2f 0a  0a 0a 23 69 6e 63 6c 75  |ford */...#inclu|
00000050  64 65 20 3c 73 74 64 69  6f 2e 68 3e 0a 0a 69 6e  |de <stdio.h>..in|
00000060  74 20 20 20 20 20 6d 61  69 6e 28 76 6f 69 64 29  |t     main(void)|
00000070  3b 0a 69 6e 74 20 20 20  20 20 66 61 63 74 28 69  |;.int     fact(i|
00000080  6e 74 29 3b 0a 0a 69 6e  74 0a 6d 61 69 6e 28 76  |nt);..int.main(v|
00000090  6f 69 64 29 0a 7b 0a 20  20 20 20 20 20 20 20 69  |oid).{.        i|
000000a0  6e 74 20 78 2c 20 61 6e  73 77 65 72 3b 0a 0a 20  |nt x, answer;.. |
000000b0  20 20 20 20 20 20 20 70  72 69 6e 74 66 28 22 50  |       printf("P|
000000c0  6c 65 61 73 65 20 65 6e  74 65 72 20 61 20 6e 75  |lease enter a nu|
000000d0  6d 62 65 72 20 74 6f 20  67 69 76 65 20 74 68 65  |mber to give the|
000000e0  20 66 61 63 74 6f 72 69  61 6c 20 66 75 6e 63 74  | factorial funct|
000000f0  69 6f 6e 3a 5c 6e 22 29  3b 0a 20 20 20 20 20 20  |ion:\n");.      |
00000100  20 20 70 72 69 6e 74 66  28 22 28 62 65 74 77 65  |  printf("(betwe|
00000110  65 6e 20 31 20 61 6e 64  20 31 35 29 5c 6e 5c 6e  |en 1 and 15)\n\n|
00000120  22 29 3b 0a 20 20 20 20  20 20 20 20 73 63 61 6e  |");.        scan|
00000130  66 28 22 25 64 22 2c 20  26 78 29 3b 0a 0a 20 20  |f("%d", &x);..  |
00000140  20 20 20 20 20 20 69 66  20 28 78 20 3e 20 30 20  |      if (x > 0 |
00000150  26 26 20 78 20 3c 3d 20  31 35 29 20 7b 0a 20 20  |&& x <= 15) {.  |
00000160  20 20 20 20 20 20 20 20  20 20 20 20 20 20 61 6e  |              an|
00000170  73 77 65 72 20 3d 20 66  61 63 74 28 78 29 3b 0a  |swer = fact(x);.|
00000180  20 20 20 20 20 20 20 20  7d 20 65 6c 73 65 20 7b  |        } else {|
00000190  0a 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |.               |
000001a0  20 70 72 69 6e 74 66 28  22 53 6f 72 72 79 20 2d  | printf("Sorry -|
000001b0  20 6e 75 6d 62 65 72 20  6f 75 74 20 6f 66 20 72  | number out of r|
000001c0  61 6e 67 65 2e 5c 6e 22  29 3b 0a 20 20 20 20 20  |ange.\n");.     |
000001d0  20 20 20 7d 0a 0a 20 20  20 20 20 20 20 20 70 72  |   }..        pr|
000001e0  69 6e 74 66 28 22 54 68  65 20 61 6e 73 77 65 72  |intf("The answer|
000001f0  20 69 73 3a 20 25 64 5c  6e 22 2c 20 61 6e 73 77  | is: %d\n", answ|
00000200  65 72 29 3b 0a 7d 0a 0a  69 6e 74 0a 66 61 63 74  |er);.}..int.fact|
00000210  28 69 6e 74 20 7a 29 0a  7b 0a 20 20 20 20 20 20  |(int z).{.      |
00000220  20 20 69 66 20 28 7a 20  3d 3d 20 31 29 20 7b 0a  |  if (z == 1) {.|
00000230  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000240  72 65 74 75 72 6e 20 28  31 29 3b 0a 20 20 20 20  |return (1);.    |
00000250  20 20 20 20 7d 20 65 6c  73 65 20 7b 0a 20 20 20  |    } else {.   |
00000260  20 20 20 20 20 20 20 20  20 20 20 20 20 72 65 74  |             ret|
00000270  75 72 6e 20 28 20 7a 20  2a 20 66 61 63 74 28 7a  |urn ( z * fact(z|
00000280  2d 31 29 29 3b 0a 20 20  20 20 20 20 20 20 7d 0a  |-1));.        }.|
00000290  7d 0a                                             |}.|
00000292