diff --git a/codescience/README.md b/codescience/README.md new file mode 100644 index 0000000..2d2eb8b --- /dev/null +++ b/codescience/README.md @@ -0,0 +1,10 @@ +# Basic Calculator +-------------------------------------------------------- +Enter number one: 6 +Enter number two: 7 +Enter an operator (+, -, *, /, ^, r, s, c): + +Result: 13.00 + +# To run the calculator +* Navigate to the codescience and run: +* gcc calcul.c main.c -lm \ No newline at end of file diff --git a/codescience/calcul.c b/codescience/calcul.c new file mode 100644 index 0000000..7ed5733 --- /dev/null +++ b/codescience/calcul.c @@ -0,0 +1,92 @@ +#include +#include +#include "calcul.h" + +/** + * Function that adds two numbers + * @num1: the first number + * @num2: the second number + * + * Return: the sum of num1 and num2 + */ +float add(float num1, float num2) { + return num1 + num2; +} + +/** + * Function that subtracts two numbers + * @num1: the first number + * @num2: the second number + * + * Return: the difference of num1 and num2 + */ +float subtract(float num1, float num2) { + return num1 - num2; +} + +/** + * Function that multiplies two numbers + * @num1: the first number + * @num2: the second number + * + * Return: the product of num1 and num2 + */ +float multiply(float num1, float num2) { + return num1 * num2; +} + +/** + * Function that divides two numbers + * @num1: the first number + * @num2: the second number + * + * Return: the quotient of num1 and num2, or 0 if num2 is 0 + */ +float divide(float num1, float num2) { + if (num2 != 0) { + return num1 / num2; + } else { + return 0; + } +} + +/** + * Function that calculates the power of a base raised to an exponent + * @base: the base number + * @exponent: the exponent + * + * Return: the result of base raised to the exponent + */ +float power(float base, float exponent) { + return pow(base, exponent); +} + +/** + * Function that calculates the square root of a number + * @num: the number + * + * Return: the square root of num + */ +float squareRoot(float num) { + return sqrt(num); +} + +/** + * Function that calculates the sine of an angle + * @angle: the angle + * + * Return: the sine of the angle + */ +float sine(float angle) { + return sin(angle); +} + +/** + * Function that calculates the cosine of an angle + * @angle: the angle + * + * Return: the cosine of the angle + */ +float cosine(float angle) { + return cos(angle); +} diff --git a/codescience/calcul.h b/codescience/calcul.h new file mode 100644 index 0000000..6382df5 --- /dev/null +++ b/codescience/calcul.h @@ -0,0 +1,15 @@ +#ifndef CALCUL_H +#define CALCUL_H + + +/**prototypes*/ +float add(float num1, float num2); +float subtract(float num1, float num2); +float multiply(float num1, float num2); +float divide(float num1, float num2); +float power(float base, float exponent); +float squareRoot(float num); +float sine(float angle); +float cosine(float angle); + +#endif \ No newline at end of file diff --git a/codescience/main.c b/codescience/main.c new file mode 100644 index 0000000..9d2bdc2 --- /dev/null +++ b/codescience/main.c @@ -0,0 +1,51 @@ +#include +#include "calcul.h" + +int main(void) { + float num1, num2; + char operator; + + printf("Enter number one: "); + scanf("%f", &num1); + + printf("Enter number two: "); + scanf("%f", &num2); + + printf("Enter an operator (+, -, *, /, ^, r, s, c): "); + scanf(" %c", &operator); + + float result; + switch (operator) { + case '+': + result = add(num1, num2); + break; + case '-': + result = subtract(num1, num2); + break; + case '*': + result = multiply(num1, num2); + break; + case '/': + result = divide(num1, num2); + break; + case '^': + result = power(num1, num2); + break; + case 'r': + result = squareRoot(num1); + break; + case 's': + result = sine(num1); + break; + case 'c': + result = cosine(num1); + break; + default: + printf("Invalid operator\n"); + return 1; + } + + printf("Result: %.2f\n", result); + + return 0; +}