-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic Calculator
87 lines (75 loc) · 1.53 KB
/
basic Calculator
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Humza Khawar
Calculator through functions
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int sum(int n1, int n2); //Declaration
int minus(int n1, int n2);
int multiply(int n1, int n2);
float divide(int n1, int n2);
int main()
{
char operator = 'A'; //initialization
int num1, num2;
printf("\n Enter First Number : ");
scanf_s("%d", &num1);
printf("\n Enter Second Number : ");
scanf_s("%d", &num2);
printf("\nEnter Operator or press q to exit : ");
while ((operator!='q') && (operator!='Q') && (operator!='+') && (operator!='-') && (operator!='/') && (operator!='*'))//keeps asking for operator
{
scanf(" %c", &operator);
}
if ((operator!='q') && (operator!='Q'))
{
switch (operator)//switch statement for all operator cases
{
case '+':
{
printf("\n %d + %d = %d\n", num1, num2, sum(num1, num2));
break;
}
case '-':
{
printf("\n %d - %d = %d\n", num1, num2, minus(num1, num2));
break;
}
case '*':
{
printf("\n %d * %d = %d\n", num1, num2, multiply(num1, num2));
break;
}
case '/':
{
if (num2 != 0)
{
printf("\n %d / %d = %.2f\n", num1, num2, divide(num1, num2));
}
else
printf("\n Zero division error\n");
break;
}
default:
{
printf("\nInvalid Input. Try Again!");
break;
}
return 0;
}
int sum(n1, n2)//sum function
{
return (n1 + n2);
}
int minus(n1, n2)//minus function
{
return (n1 - n2);
}
int multiply(n1, n2)//multiplication function
{
return (n1 * n2);
}
float divide(n1, n2)//division function
{
return ((float)n1 / n2);
}