Skip to content

Commit

Permalink
Move files to base of directory
Browse files Browse the repository at this point in the history
  • Loading branch information
dohoudaniel committed Aug 8, 2023
1 parent 86d648f commit bb17077
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ekerejosh/calculator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "calculator.h"

// Function for addition
float addition(float num1, float num2)
{
return (num1 + num2);
}

// Function for subtraction
float subtraction(float num1, float num2)
{
return (num1 - num2);
}

// Function for multiplication
float multiplication(float num1, float num2)
{
return (num1 * num2);
}

// Function for division
float division(float num1, float num2)
{
return (num1 / num2);
}
10 changes: 10 additions & 0 deletions ekerejosh/calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef CALCULATOR_H
#define CALCULATOR_H

// Function declarations for basic operations
float addition(float a, float b);
float subtraction(float a, float b);
float multiplication(float a, float b);
float division(float a, float b);

#endif
64 changes: 64 additions & 0 deletions ekerejosh/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <stdio.h>
#include "calculator.h"
/**
* main - This progam performs basic arithmetic operations on a pair of numbers
* Return: (0) success, (1) fail
*/
int main()
{
float num1, num2, result;
char operator;

printf("Enter a simple arithmetic expression. Example: 2 * 2\n");
printf("(allowed operators: +, -, *, or /)\n");
printf("\n");

//Take numbers and operators from the user
printf("Enter the first number: ");
if (scanf("%f", &num1) != 1)
{
printf("Invalid input for the first number.\n");
return (1);
}

printf("Enter the operator (+, -, *, /): ");
scanf(" %c", &operator);

printf("Enter the second number: ");
if (scanf("%f", &num2) != 1)
{
printf("Invalid input for the first number.\n");
return (1);
}

//Perform the appropriate arithmetic operation based on the operator
switch (operator)
{
case '+':
result = addition(num1, num2);
break;
case '-':
result = subtraction(num1, num2);
break;
case '*':
result = multiplication(num1, num2);
break;
case '/':
// Check for division by zero
if (num2 == 0)
{
printf("Error: Cannot divide by zero.\n");
return (1);
}
result = division(num1, num2);
break;
default:
printf("Invalid operator. Please use any of '+', '-', '*', '/'. \n");
}

// Output the result
printf("Result: %.2f\n", result);

return (0);

}

0 comments on commit bb17077

Please sign in to comment.