summaryrefslogtreecommitdiff
path: root/cbook/one_two.c
diff options
context:
space:
mode:
authorCody Hiar <cody@hiar.ca>2020-03-10 13:38:39 -0600
committerCody Hiar <cody@hiar.ca>2020-03-10 13:38:39 -0600
commit2cd866e3fa0cbec2b53a3b5851792da320fc1c71 (patch)
treea446b7fdaa648aea51dc6569caf77a8b4a599eed /cbook/one_two.c
parent0bcd56cb0e42761903431081ddcf00790627b505 (diff)
Running two examples from cbook
Diffstat (limited to 'cbook/one_two.c')
-rw-r--r--cbook/one_two.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/cbook/one_two.c b/cbook/one_two.c
new file mode 100644
index 0000000..a5bf221
--- /dev/null
+++ b/cbook/one_two.c
@@ -0,0 +1,36 @@
+/*
+*
+* Dumb program that generates prime numbers.
+*/
+#include <stdio.h>
+#include <stdlib.h>
+
+
+int is_prime(int x) {
+ for (int i=2; i<x; i++) {
+ if (x % i == 0 && i != x) return 0;
+ }
+ return 1;
+}
+
+
+
+
+int main(){
+ int this_number, prime, next_prime, next_number;
+
+ this_number = 3;
+
+ while(this_number < 10000){
+ prime = is_prime(this_number);
+ if (prime == 1) {
+ next_number = this_number + 2;
+ next_prime = is_prime(next_number);
+ if (next_prime == 1)
+ printf("%d and %d are prime pair\n", this_number, next_number);
+ }
+ this_number++;
+
+ }
+ exit(EXIT_SUCCESS);
+}