-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathsimple calc.c
More file actions
51 lines (46 loc) · 1.46 KB
/
simple calc.c
File metadata and controls
51 lines (46 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Simple Calculator Program
* This program performs basic arithmetic operations: addition, subtraction,
* multiplication, and division based on user input.
*/
#include <stdio.h>
int main() {
double num1, num2;
char operator;
char choice;
do {
// Ask the user for input
printf("Enter first number: ");
scanf("%lf", &num1);
printf("Enter second number: ");
scanf("%lf", &num2);
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator); // space before %c helps catch any stray newline characters
// Perform the selected operation
switch (operator) {
case '+':
printf("Result: %.2lf\n", num1 + num2);
break;
case '-':
printf("Result: %.2lf\n", num1 - num2);
break;
case '*':
printf("Result: %.2lf\n", num1 * num2);
break;
case '/':
if (num2 != 0)
printf("Result: %.2lf\n", num1 / num2);
else
printf("Error: Division by zero is not allowed.\n");
break;
default:
printf("Invalid operator.\n");
}
// Ask if the user wants to perform another calculation
printf("Do you want to perform another calculation? (y/n): ");
scanf(" %c", &choice);
num1 = num2 = 0; // Reset numbers for next calculation
} while (choice == 'y' || choice == 'Y');
printf("Thank you for using the calculator. Goodbye!\n");
return 0;
}