-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simple_calculator.c
65 lines (65 loc) · 1.53 KB
/
Simple_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
55
56
57
58
59
60
61
62
63
64
65
/*
@Author: Abhinav Pawar
@Created on: 07-05-2022
*/
#include<stdio.h>
#include<math.h>
int fact(int num)
{
int fact=1;
if (num==0 || num==1)
{
fact=1;
}
else
{
for (int i=2;i<=num;i++)
{
fact=fact*i;
}
}
return fact;
}
void main()
{
int n1,n2,flag=0;
float ans;
char operate;
printf ("\t\tWelcome to the SIMPLE CALCULATOR!\n");
printf ("\nEnter your calculation: \n");
scanf("%d%c%d",&n1,&operate,&n2);
switch (operate)
{
case '+': ans=n1+n2;
break;
case '-': ans=n1-n2;
break;
case '*': ans=n1*n2;
break;
case '/': ans=n1/n2;
break;
case '^': ans=pow(n1,n2);
break;
case '%': ans=n1%n2;
break;
case 'p':
case 'P': if (n1>=0 && n2>=0 && n1>n2)
ans=fact(n1)/fact(n1-n2);
else
flag=2;
break;
case 'c':
case 'C': if (n1>=0 && n2>=0 && n1>n2)
ans=fact(n1)/((fact(n2)*fact(n1-n2)));
else
flag=2;
break;
default: flag=1;
}
if (flag==1)
printf ("\nFail! Enter correct operator\n");
else if (flag==2)
printf ("\nFail! Enter correct operands\n");
else
printf ("\n\t%d%c%d=%0.2f\n\n", n1,operate,n2,ans);
}