diff --git a/src/primzahl/main.c b/src/primzahl/main.c new file mode 100644 index 0000000..8480010 --- /dev/null +++ b/src/primzahl/main.c @@ -0,0 +1,19 @@ +#include "primzahl.h" +#include "../userinput.h" +#include + +int main() { + // set the smallest possible input + int minInput = 0; + + //get user input + int num = usergetd("Enter a positive number: ", minInput, NULL); + + //check if its a prime number or not + if (primzahl(num)) + printf("%d is a prime number.\n", num); + else + printf("%d is not a prime number.\n", num); + + return 0; +} \ No newline at end of file diff --git a/src/primzahl/primzahl.c b/src/primzahl/primzahl.c new file mode 100644 index 0000000..e8fd70b --- /dev/null +++ b/src/primzahl/primzahl.c @@ -0,0 +1,15 @@ +#include + +int primzahl(int num) { + //for the case zero or one + if (num <= 1) + return 0; + + //checking if there are any other divider + for (int i = 2; i < num; i++) { + if (num % i == 0) + return 0; + } + //if none were found return true + return 1; +} \ No newline at end of file diff --git a/src/primzahl/primzahl.h b/src/primzahl/primzahl.h new file mode 100644 index 0000000..0f6cfc2 --- /dev/null +++ b/src/primzahl/primzahl.h @@ -0,0 +1,6 @@ +#ifndef PRIMZAHL_H +#define PRIMZAHL_H + +int primzahl(int num); + +#endif \ No newline at end of file diff --git a/test/primzahl/test_primzahl.c b/test/primzahl/test_primzahl.c new file mode 100644 index 0000000..2276ce9 --- /dev/null +++ b/test/primzahl/test_primzahl.c @@ -0,0 +1,17 @@ +#include "unity.h" +#include "primzahl.h" + +void setUP(){} +void tearDown(){} + +void test_primzahl() { + TEST_ASSERT_TRUE(primzahl(2)); // 2 is a prime number + TEST_ASSERT_TRUE(primzahl(3)); // 3 is a prime number + TEST_ASSERT_TRUE(primzahl(5)); // 5 is a prime number + TEST_ASSERT_TRUE(primzahl(7)); // 7 is a prime number + TEST_ASSERT_FALSE(primzahl(1)); // 1 is not a prime number + TEST_ASSERT_FALSE(primzahl(4)); // 4 is not a prime number + TEST_ASSERT_FALSE(primzahl(6)); // 6 is not a prime number + TEST_ASSERT_FALSE(primzahl(9)); // 9 is not a prime number + +} \ No newline at end of file