Browse Source

Merge branch 'primzahl' into 'master'

Merge branch 'primzahl' into 'master'

See merge request fdai8031/cstools101!11
master
fdai7595 11 months ago
parent
commit
b7fd605b8b
  1. 19
      src/primzahl/main.c
  2. 15
      src/primzahl/primzahl.c
  3. 6
      src/primzahl/primzahl.h
  4. 17
      test/primzahl/test_primzahl.c

19
src/primzahl/main.c

@ -0,0 +1,19 @@
#include "primzahl.h"
#include "../userinput.h"
#include <stdio.h>
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;
}

15
src/primzahl/primzahl.c

@ -0,0 +1,15 @@
#include <stdio.h>
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;
}

6
src/primzahl/primzahl.h

@ -0,0 +1,6 @@
#ifndef PRIMZAHL_H
#define PRIMZAHL_H
int primzahl(int num);
#endif

17
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
}
Loading…
Cancel
Save