diff --git a/src/fakultaet/fakultaet.c b/src/fakultaet/fakultaet.c new file mode 100644 index 0000000..b05b947 --- /dev/null +++ b/src/fakultaet/fakultaet.c @@ -0,0 +1,15 @@ +#include + +int fakultaet(int n) { + // Checking if the input is either zero or one + if (n == 0 || n == 1) + return 1; + + // The result multiplies with i and i increases every turn until the loop ends + int result = 1; + for (int i = 2; i <= n; i++) { + result *= i; + } + + return result; +} \ No newline at end of file diff --git a/src/fakultaet/fakultaet.h b/src/fakultaet/fakultaet.h new file mode 100644 index 0000000..c2d4a62 --- /dev/null +++ b/src/fakultaet/fakultaet.h @@ -0,0 +1,6 @@ +#ifndef FAKULTAET_H +#define FAKULTAET_H + +int fakultaet(int n); + +#endif \ No newline at end of file diff --git a/src/fakultaet/main.c b/src/fakultaet/main.c new file mode 100644 index 0000000..d0ca0fe --- /dev/null +++ b/src/fakultaet/main.c @@ -0,0 +1,19 @@ +#include "fakultaet.h" +#include "../userinput.h" +#include + +int main() { + // minInput is set to 0 so there won't be any negative numbers + int minInput = 0; + // maxInput is set to 12 because 13 would be too big for int + int maxInput = 12; + + int number = usergetd("Enter a positive number below 13: "), minInput, maxInput); + + int result = fakultaet(number); + // result now has the factorial of the user input + + printf("The factorial of %d is: %d\n", number, result); + + return 0; +} \ No newline at end of file diff --git a/test/fakultaet/test_fakultaet.c b/test/fakultaet/test_fakultaet.c new file mode 100644 index 0000000..4414b23 --- /dev/null +++ b/test/fakultaet/test_fakultaet.c @@ -0,0 +1,20 @@ +#include "unity.h" +#include "fakultaet.h" + +void setUp(){} +void tearDown(){} + +// Test for the input zero +void test_fakultaetOf0() { + TEST_ASSERT_EQUAL_INT(1, fakultaet(0)); +} + +// Test for the input one +void test_fakultaetOf1() { + TEST_ASSERT_EQUAL_INT(1, fakultaet(1)); +} + +// Test for the input five +void test_fakultaetOf5() { + TEST_ASSERT_EQUAL_INT(120, fakultaet(5)); +} \ No newline at end of file