-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCalculator.c
54 lines (44 loc) · 1.42 KB
/
Calculator.c
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
52
53
54
#include <stdio.h>
#include <stdlib.h>
int main()
{
float num1, num2;
char operator, retry;
do
{
// clears the terminal screen
system("cls");
printf("Enter the operator (+ - * /) : ");
scanf(" %c", &operator);
// clears input buffers in console
fflush(stdin);
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
// switch case for getting result/output
switch (operator)
{
case '+':
printf("\nSum of %.0f & %.0f is: %.0f \n", num1, num2, (num1 + num2));
break;
case '-':
printf("\nSubtraction of %.0f & %.0f is: %.0f \n", num1, num2, (num1 - num2));
break;
case '*':
printf("\nProduct of %.0f & %.0f is: %.0f \n", num1, num2, (num1 * num2));
break;
case '/':
printf("\nDivision of %.0f & %.0f is: %.0f \n", num1, num2, (num1 / num2));
break;
default:
printf("\nERROR: Invalid Input. \n");
}
// clears input buffers in console
fflush(stdin);
// checks if user wants to use calculator again
printf("\nDo you waana try again [y/N]: ");
scanf(" %c", &retry);
} while (retry == 'y' || retry == 'Y');
return 0;
}