In: Computer Science
Write a program that acts as a basic calculator
please use #include studio not ios.stream
int main(int argc, char* argv[])
{
calculator();
return 0;
}
/* Sample run of the program - Input and Output */
0 - Exit
1 - Add
2 - Subtract
3 - Multiply
4 - Divide
Please select an option : 1
Please enter the value : 5
Result = 5.000000
0 - Exit
1 - Add
2 - Subtract
3 - Multiply
4 - Divide
Please select an option : 2
Please enter the value : 1
Result = 4.000000
0 - Exit
1 - Add
2 - Subtract
3 - Multiply
4 - Divide
Please select an option : 3
Please enter the value : 3
Result = 12.000000
0 - Exit
1 - Add
2 - Subtract
3 - Multiply
4 - Divide
Please select an option : 4
Please enter the value : 2
Result = 6.000000
0 - Exit
1 - Add
2 - Subtract
3 - Multiply
4 - Divide
Please select an option : 0
#include <stdio.h>
float add(float a,float b){return a+b;}
float sub(float a,float b){return a-b;}
void mul(float *a,float b){*a=(*a)*b;}
void divi(float *a,float b){*a =( *a)/b;}
void claculator()
{ float result=0;
float inp=0;
int opt=-1;
while(1)
{
printf("0-Exit\n1-Add\n2-Substarct\n3-Multiply\n4-Divide\n");
printf("Please select an option :");
scanf("%d",&opt);
if(opt==0)
break;
printf("Please enter the value : ");
scanf("%f",&inp);
if(opt==1)
result=add(result,inp);
if(opt==2)
result=sub(result,inp);
if(opt==3)
mul(&result,inp);
if(opt==4)
divi(&result,inp);
printf("result =%f\n",result);
}
}
int main()
{
claculator();
return 0;
}