Created
April 2, 2025 19:47
-
-
Save bonface221/2d407a2ac468476e465d0f385ac499fe to your computer and use it in GitHub Desktop.
Simple Addition, Division and multiplication Calculator in C
This file contains hidden or 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
#include <stdio.h> | |
double operate(char opr, double num1, double num2) | |
{ | |
if (opr == 'a') | |
return num1 + num2; | |
if (opr == 's') | |
return num1 - num2; | |
if (opr == 'm') | |
return num1 * num2; | |
printf("Invalid operation.\n"); | |
return 0.0; | |
} | |
int main() | |
{ | |
double num1, num2; | |
char operation; | |
printf("Enter the First number:\n"); | |
scanf("%lf", &num1); | |
// Clear the newline left by previous scanf | |
getchar(); | |
printf("Enter Operator \n a: ADD \n s: Subtract \n m: Multiply\n"); | |
scanf("%c", &operation); | |
printf("Enter the second number:\n"); | |
scanf("%lf", &num2); | |
double result = operate(operation, num1, num2); | |
printf("RESULT: %lf\n", result); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment