Browse Source

Merge branch 'fakultaet' into 'master'

Merge branch 'fakultaet'

See merge request fdai8031/cstools101!8
master
fdai7595 11 months ago
parent
commit
0e2837a9bc
  1. 15
      src/fakultaet/fakultaet.c
  2. 6
      src/fakultaet/fakultaet.h
  3. 19
      src/fakultaet/main.c
  4. 20
      test/fakultaet/test_fakultaet.c

15
src/fakultaet/fakultaet.c

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

6
src/fakultaet/fakultaet.h

@ -0,0 +1,6 @@
#ifndef FAKULTAET_H
#define FAKULTAET_H
int fakultaet(int n);
#endif

19
src/fakultaet/main.c

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

20
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));
}
Loading…
Cancel
Save