diff --git a/src/calculatorFactorial.c b/src/calculatorFactorial.c new file mode 100644 index 0000000..e95fd11 --- /dev/null +++ b/src/calculatorFactorial.c @@ -0,0 +1,18 @@ +#include "calculatorFactorial.h" +// Note: +/* This Function may or may not be implemented in actual program, even if it is merged to the main branch. + If it is temporarily not included in the main Program, then this has a role in future Development of the Project */ + + +int calculatorFactorial(int num) //implement recursion. The function calls itself so many times, till the breaking condition is fulfilled. +{ + if (num == 0) //breaking condition + { + return 1; + } + else + { + return num * calculatorFactorial(num - 1); //If its not breaking condition, then multiply the number with the same function implemented on the previous number. Eventually it will reach breaking condition. + } +} + diff --git a/src/calculatorFactorial.h b/src/calculatorFactorial.h new file mode 100644 index 0000000..5dbd323 --- /dev/null +++ b/src/calculatorFactorial.h @@ -0,0 +1,8 @@ +#ifndef CALCULATORFACTORIAL_H +#define CALCULATORFACTORIAL_H + +#include +#include +int calculatorFactorial(int num); + +#endif // CALCULATORFACTORIAL_H diff --git a/src/displayMenuCalculator.c b/src/displayMenuCalculator.c index 9d66ba2..fc26107 100644 --- a/src/displayMenuCalculator.c +++ b/src/displayMenuCalculator.c @@ -5,6 +5,7 @@ #include "calculatorSubtract.c" #include "calculatorMultiply.c" #include "calculatorDivide.c" +#include "calculatorFactorial.c" // Note: /* This Function may or may not be implemented in actual program, even if it is merged to the main branch. If it is temporarily not included in the main Program, then this has a role in future Development of the Project */ @@ -64,11 +65,12 @@ void displayMenuCalculator(char x) //Displays the correct output, only when x i case 5: calculatorGetUserInputFactorial(&num); //Created specially for factorial which gets a number from user. - //NOT YET IMPLEMENTED + answer = calculatorFactorial(num); + printf("Answer: %f\n", answer); break; default: - //NOT YET IMPLEMENTED + printf("Invalid choice\n"); return; } } diff --git a/tests/test_calculatorFactorial.c b/tests/test_calculatorFactorial.c new file mode 100644 index 0000000..624f9fe --- /dev/null +++ b/tests/test_calculatorFactorial.c @@ -0,0 +1,84 @@ +#ifdef TEST + +#include "unity.h" + +#include "calculatorFactorial.h" + +// Note: +/* This Function may or may not be implemented in actual program, even if it is merged to the main branch. + If it is temporarily not included in the main Program, then this has a role in future Development of the Project */ + + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test1_calculatorFactorial(void) +{ + int num, actual, expected; //Arrange + num = 1; + expected = 1; + actual = calculatorFactorial(num); //Act + TEST_ASSERT_EQUAL_INT(expected, actual); //Assert +} + +void test2_calculatorFactorial(void) +{ + int num, actual, expected; //Arrange + num = 0; + expected = 1; + actual = calculatorFactorial(num); //Act + TEST_ASSERT_EQUAL_INT(expected, actual); //Assert +} + +void test3_calculatorFactorial(void) +{ + int num, actual, expected; //Arrange + num = 3; + expected = 6; + actual = calculatorFactorial(num); //Act + TEST_ASSERT_EQUAL_INT(expected, actual); //Assert +} + +void test4_calculatorFactorial(void) +{ + int num, actual, expected; //Arrange + num = 5; + expected = 120; + actual = calculatorFactorial(num); //Act + TEST_ASSERT_EQUAL_INT(expected, actual); //Assert +} + +void test5_calculatorFactorial(void) +{ + int num, actual, expected; //Arrange + num = 8; + expected = 40320; + actual = calculatorFactorial(num); //Act + TEST_ASSERT_EQUAL_INT(expected, actual); //Assert +} + +void test6_calculatorFactorial(void) +{ + int num, actual, expected; //Arrange + num = 11; + expected = 39916800; + actual = calculatorFactorial(num); //Act + TEST_ASSERT_EQUAL_INT(expected, actual); //Assert +} + +void test7_calculatorFactorial(void) +{ + int num, actual, expected; //Arrange + num = 10; + expected = 3628800; + actual = calculatorFactorial(num); //Act + TEST_ASSERT_EQUAL_INT(expected, actual); //Assert +} + + +#endif // TEST