Home » Archimedes archive » Acorn User » AU 1995-04.adf » !C_C » c/prime

c/prime

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-04.adf » !C_C
Filename: c/prime
Read OK:
File size: 02D7 bytes
Load address: 0000
Exec address: 0000
File contents
/* Program demonstrating nested 'for' loops to print out prime numbers */
/* Acorn User 'C for yourself' demonstration program - April 1995 */

#include <stdio.h>

#define MAXNUM 100
main()
{
   int x,y,flag,result;
   printf("Finding all prime numbers between 3 and %d\n",MAXNUM);

   for (x = 3; x <= MAXNUM; x++) {
      flag = 0;
      for (y = 2; y < x/2; y++) {
         result = x % y;
         if (result == 0) {
            flag = 1;
            break;
         }

         /* If the number is divisible by one below it, then a flag
         is set - the number is only printed out if this flag remains
         unset - that is, the number is prime. */

      }
      if (flag == 0) printf("%d is prime.\n",x);
   }
}
00000000  2f 2a 20 50 72 6f 67 72  61 6d 20 64 65 6d 6f 6e  |/* Program demon|
00000010  73 74 72 61 74 69 6e 67  20 6e 65 73 74 65 64 20  |strating nested |
00000020  27 66 6f 72 27 20 6c 6f  6f 70 73 20 74 6f 20 70  |'for' loops to p|
00000030  72 69 6e 74 20 6f 75 74  20 70 72 69 6d 65 20 6e  |rint out prime n|
00000040  75 6d 62 65 72 73 20 2a  2f 0a 2f 2a 20 41 63 6f  |umbers */./* Aco|
00000050  72 6e 20 55 73 65 72 20  27 43 20 66 6f 72 20 79  |rn User 'C for y|
00000060  6f 75 72 73 65 6c 66 27  20 64 65 6d 6f 6e 73 74  |ourself' demonst|
00000070  72 61 74 69 6f 6e 20 70  72 6f 67 72 61 6d 20 2d  |ration program -|
00000080  20 41 70 72 69 6c 20 31  39 39 35 20 2a 2f 0a 0a  | April 1995 */..|
00000090  23 69 6e 63 6c 75 64 65  20 3c 73 74 64 69 6f 2e  |#include <stdio.|
000000a0  68 3e 0a 0a 23 64 65 66  69 6e 65 20 4d 41 58 4e  |h>..#define MAXN|
000000b0  55 4d 20 31 30 30 0a 6d  61 69 6e 28 29 0a 7b 0a  |UM 100.main().{.|
000000c0  20 20 20 69 6e 74 20 78  2c 79 2c 66 6c 61 67 2c  |   int x,y,flag,|
000000d0  72 65 73 75 6c 74 3b 0a  20 20 20 70 72 69 6e 74  |result;.   print|
000000e0  66 28 22 46 69 6e 64 69  6e 67 20 61 6c 6c 20 70  |f("Finding all p|
000000f0  72 69 6d 65 20 6e 75 6d  62 65 72 73 20 62 65 74  |rime numbers bet|
00000100  77 65 65 6e 20 33 20 61  6e 64 20 25 64 5c 6e 22  |ween 3 and %d\n"|
00000110  2c 4d 41 58 4e 55 4d 29  3b 0a 0a 20 20 20 66 6f  |,MAXNUM);..   fo|
00000120  72 20 28 78 20 3d 20 33  3b 20 78 20 3c 3d 20 4d  |r (x = 3; x <= M|
00000130  41 58 4e 55 4d 3b 20 78  2b 2b 29 20 7b 0a 20 20  |AXNUM; x++) {.  |
00000140  20 20 20 20 66 6c 61 67  20 3d 20 30 3b 0a 20 20  |    flag = 0;.  |
00000150  20 20 20 20 66 6f 72 20  28 79 20 3d 20 32 3b 20  |    for (y = 2; |
00000160  79 20 3c 20 78 2f 32 3b  20 79 2b 2b 29 20 7b 0a  |y < x/2; y++) {.|
00000170  20 20 20 20 20 20 20 20  20 72 65 73 75 6c 74 20  |         result |
00000180  3d 20 78 20 25 20 79 3b  0a 20 20 20 20 20 20 20  |= x % y;.       |
00000190  20 20 69 66 20 28 72 65  73 75 6c 74 20 3d 3d 20  |  if (result == |
000001a0  30 29 20 7b 0a 20 20 20  20 20 20 20 20 20 20 20  |0) {.           |
000001b0  20 66 6c 61 67 20 3d 20  31 3b 0a 20 20 20 20 20  | flag = 1;.     |
000001c0  20 20 20 20 20 20 20 62  72 65 61 6b 3b 0a 20 20  |       break;.  |
000001d0  20 20 20 20 20 20 20 7d  0a 0a 20 20 20 20 20 20  |       }..      |
000001e0  20 20 20 2f 2a 20 49 66  20 74 68 65 20 6e 75 6d  |   /* If the num|
000001f0  62 65 72 20 69 73 20 64  69 76 69 73 69 62 6c 65  |ber is divisible|
00000200  20 62 79 20 6f 6e 65 20  62 65 6c 6f 77 20 69 74  | by one below it|
00000210  2c 20 74 68 65 6e 20 61  20 66 6c 61 67 0a 20 20  |, then a flag.  |
00000220  20 20 20 20 20 20 20 69  73 20 73 65 74 20 2d 20  |       is set - |
00000230  74 68 65 20 6e 75 6d 62  65 72 20 69 73 20 6f 6e  |the number is on|
00000240  6c 79 20 70 72 69 6e 74  65 64 20 6f 75 74 20 69  |ly printed out i|
00000250  66 20 74 68 69 73 20 66  6c 61 67 20 72 65 6d 61  |f this flag rema|
00000260  69 6e 73 0a 20 20 20 20  20 20 20 20 20 75 6e 73  |ins.         uns|
00000270  65 74 20 2d 20 74 68 61  74 20 69 73 2c 20 74 68  |et - that is, th|
00000280  65 20 6e 75 6d 62 65 72  20 69 73 20 70 72 69 6d  |e number is prim|
00000290  65 2e 20 2a 2f 0a 0a 20  20 20 20 20 20 7d 0a 20  |e. */..      }. |
000002a0  20 20 20 20 20 69 66 20  28 66 6c 61 67 20 3d 3d  |     if (flag ==|
000002b0  20 30 29 20 70 72 69 6e  74 66 28 22 25 64 20 69  | 0) printf("%d i|
000002c0  73 20 70 72 69 6d 65 2e  5c 6e 22 2c 78 29 3b 0a  |s prime.\n",x);.|
000002d0  20 20 20 7d 0a 7d 0a                              |   }.}.|
000002d7