forked from ALX-SE-Algorithmia/Demo-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86d648f
commit bb17077
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |