C/Caulculator2.c
2024-05-13 11:09:46 -05:00

66 lines
1.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
int main() {
/* Display a message before the program initiates */
printf("Preform an operation on two numbers.\n\n");
float n1, n2, r; /* Number One, Number Two, and Result */
char o; /* Operator */
/* Prompt the user for the numbers */
printf("Enter the first number: ");
scanf("%f", &n1);
printf("Enter the second number: ");
scanf("%f", &n2);
/* Prompt the user for the operator */
printf("Enter the operator (+|-|*|/): ");
scanf("\n%c", &o);
/* Preform the operation */
switch (o) {
case '+':
r = n1 + n2;
break;
case '-':
r = n1 - n2;
break;
case '*':
r = n1 * n2;
break;
case '/':
/* Sanity Check
*
* If n2, the second number, equals 0,
* exit the program.
*/
if (n2 == 0) {
printf("Error: Cannot devide by 0\n");
exit(1);
}
r = n1 / n2;
break;
/* If the provided value of the operator
* is not (+|-|*|/), exit the program.
*/
default:
printf("Error: Invalid argument\n");
exit(1);
}
/* Print the result */
printf("Result: %.2f", r);
return 0;
}