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
8098a19
commit 2e2bd83
Showing
4 changed files
with
124 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,31 @@ | ||
#include "main.h" | ||
|
||
/* Function to check if the operator is valid*/ | ||
int isValidOperator(char operator) | ||
{ | ||
return operator == '+' || operator == '-' || operator == '*' || operator == '/'; | ||
} | ||
|
||
/*Function to add two numbers */ | ||
float addition(float num1, float num2) { | ||
return num1 + num2; | ||
} | ||
|
||
/*Function to subtract two numbers*/ | ||
float subtraction(float num1, float num2) { | ||
return num1 - num2; | ||
} | ||
|
||
/*Function to multiply two numbers */ | ||
float multiplication(float num1, float num2) { | ||
return num1 * num2; | ||
} | ||
|
||
/*Function to divide two numbers */ | ||
float division(float num1, float num2) { | ||
if (num2 == 0) { | ||
printf("Error: Cannot divide by zero!\n"); | ||
return 0; | ||
} | ||
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,25 @@ | ||
# collaborating on Calculator Demo project in c programming | ||
The Demo will run like a calculator which will handle 4 operation | ||
.Addition | ||
.Substraction | ||
.Multiplication | ||
.Division | ||
|
||
#Program contain 4 functions | ||
.Addition function | ||
.Multiplication function | ||
.Substraction function | ||
.Division function | ||
|
||
#Calculator is interactive | ||
.It takes in two numbers from the user | ||
.It takes in an arithmetic operator for the four math operation | ||
.Perform arthmetic operationbased on user choice | ||
.Then it's prints result when user entered valid operator | ||
.When user enter wrong arthmetic operation other than division, addition,multiplication and addition | ||
.program warn user then print an error message. | ||
|
||
#The program allow user to use calculator many time | ||
.After performing one operation user will be asked if he/she want to contiue | ||
.if user select y(for yes) or Y(for capital Yes)user can keep using the calculator | ||
.if user select other character or typed anything rather than Y or y the program will end. |
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,56 @@ | ||
#include <stdio.h> | ||
#include "main.h" | ||
|
||
int main() | ||
{ | ||
char operator; | ||
float num1, num2, result; | ||
char choice; | ||
|
||
printf("Welcome to the Four Basic Operations Calculator!\n"); | ||
|
||
do | ||
{ | ||
printf("Enter the first number: "); | ||
scanf("%f", &num1); | ||
|
||
printf("Enter an arithmetic operator (+, -, *, /): "); | ||
scanf(" %c", &operator); | ||
|
||
printf("Enter the second number: "); | ||
scanf("%f", &num2); | ||
|
||
if (isValidOperator(operator)) | ||
{ | ||
switch (operator) | ||
{ | ||
case '+': | ||
result = addition(num1, num2); | ||
break; | ||
case '-': | ||
result = subtraction(num1, num2); | ||
break; | ||
case '*': | ||
result = multiplication(num1, num2); | ||
break; | ||
case '/': | ||
result = division(num1, num2); | ||
break; | ||
} | ||
|
||
printf("Result: %.2f\n", result); | ||
} | ||
else | ||
{ | ||
printf("Error: Invalid arithmetic operator!\n"); | ||
} | ||
|
||
printf("Do you wish to perform another operation? (y/n): "); | ||
scanf(" %c", &choice); | ||
} | ||
while (choice == 'y' || choice == 'Y'); | ||
|
||
printf("Thank you for using the calculator. Goodbye!\n"); | ||
|
||
return 0; | ||
} |
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,12 @@ | ||
#ifndef MAIN_H | ||
#define MAIN_H | ||
|
||
#include <stdio.h> | ||
|
||
// Function prototypes | ||
float addition(float num1, float num2); | ||
float subtraction(float num1, float num2); | ||
float multiplication(float num1, float num2); | ||
float division(float num1, float num2); | ||
int isValidOperator(char operator); | ||
#endif // MAIN_H |