Skip to content

Commit

Permalink
Merge pull request ALX-SE-Algorithmia#42 from rodgersxy/basic-calc
Browse files Browse the repository at this point in the history
simple calculator in C programming
  • Loading branch information
MartyOfMCA authored Jul 14, 2023
2 parents fbecfa0 + dad99dd commit 89bf0fa
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
10 changes: 10 additions & 0 deletions codescience/README.md
Original file line number Diff line number Diff line change
@@ -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
92 changes: 92 additions & 0 deletions codescience/calcul.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <stdio.h>
#include <math.h>
#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);
}
15 changes: 15 additions & 0 deletions codescience/calcul.h
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions codescience/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <stdio.h>
#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;
}

0 comments on commit 89bf0fa

Please sign in to comment.