-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.c
More file actions
34 lines (31 loc) · 1.12 KB
/
Copy pathcalculator.c
File metadata and controls
34 lines (31 loc) · 1.12 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
#include <stdio.h> /* needed for printf and scanf*/
int main(){
printf("***************************\n");
printf(" A SIMPLE CALCULATOR IN C \n");
printf("ENTER INPUTS WITHOUT SPACES\n");
printf("***************************\n\n\n");
while(1){ /* no booleans (1 == true) */
/* variables must be declared at a block's head */
char option;
double num_1, num_2, total = 0;
printf("Enter an operation (+, -, *, /) | 'q' to quit: ");
scanf(" %c", &option); /* scan in the char input (%c) into the address of option */
if(option == 'q' || option == 'Q') break;
printf("Enter number 1 (double): ");
scanf(" %lf", &num_1); /* scan in the double input (%lf) into the address of num_1 */
printf("Enter number 2 (double): ");
scanf(" %lf", &num_2); /* scan in the double input (%lf) into the address of num_2 */
switch(option){
case '+': total = num_1 + num_2;
break;
case '-': total = num_1 - num_2;
break;
case '*': total = num_1 * num_2;
break;
case '/': total = num_1 / num_2;
break;
}
printf("The result is: %lf\n", total);
}
return 0;
}