code:
#include<stdio.h>
double add(double a,double b)
{
double sum;
sum = a+b;
return sum;
}
double subtract(double a,double b)
{
double sub;
sub = a-b;
return sub;
}
double multiply(double a,double b)
{
double mul;
mul = a*b;
return mul;
}
double divide(double a,double b)
{
double div;
div = a/b;
return div;
}
int main()
{
int n;
double a,b,total;
while(1!=EOF)
{
printf("What you want to do ???\n");
printf("1. Add\n2. Subtract\n3. Multiply\n4. Divide\n");
printf("Type value:");
scanf("%d",&n);
if(n==1)
{
printf("Enter two of the values:");
scanf("%lf %lf",&a,&b);
total=add(a,b);
printf("\nAnswer: %.2lf\n\n",total);
}
if(n==2)
{
printf("Enter two of the values:");
scanf("%lf %lf",&a,&b);
total=subtract(a,b);
printf("\nAnswer: %.2lf\n\n",total);
}
if(n==3)
{
printf("Enter two of the values:");
scanf("%lf %lf",&a,&b);
total=multiply(a,b);
printf("\nAnswer: %.2lf\n\n",total);
}
if(n==4)
{
printf("Enter two of the values:");
scanf("%lf %lf",&a,&b);
total=divide(a,b);
printf("\nAnswer: %.2lf\n\n",total);
}
}
return 0;
}
With Switch Case:
#include<stdio.h>
double add(double a,double b)
{
double sum;
sum = a+b;
return sum;
}
double subtract(double a,double b)
{
double sub;
sub = a-b;
return sub;
}
double multiply(double a,double b)
{
double mul;
mul = a*b;
return mul;
}
double divide(double a,double b)
{
double div;
div = a/b;
return div;
}
int main()
{
int n;
double a,b,total;
while(1!=EOF)
{
printf("What you want to do ???\n");
printf("1. Add\n2. Subtract\n3. Multiply\n4. Divide\n");
printf("Type value:");
scanf("%d",&n);
switch(n)
{
case 1:
printf("Enter two of the values:");
scanf("%lf %lf",&a,&b);
total=add(a,b);
printf("\nAnswer: %.2lf\n\n",total);
break;
case 2:
printf("Enter two of the values:");
scanf("%lf %lf",&a,&b);
total=subtract(a,b);
printf("\nAnswer: %.2lf\n\n",total);
break;
case 3:
printf("Enter two of the values:");
scanf("%lf %lf",&a,&b);
total=multiply(a,b);
printf("\nAnswer: %.2lf\n\n",total);
break;
case 4:
printf("Enter two of the values:");
scanf("%lf %lf",&a,&b);
total=divide(a,b);
printf("\nAnswer: %.2lf\n\n",total);
break;
default:
// printf("You didn't press any function.");
exit(420);
}
}
return 0;
}
Another easy code:
#include<stdio.h>
double add(double a,double b)
{
double sum;
sum = a+b;
return sum;
}
double subtract(double a,double b)
{
double sub;
sub = a-b;
return sub;
}
double multiply(double a,double b)
{
double mul;
mul = a*b;
return mul;
}
double divide(double a,double b)
{
double div;
div = a/b;
return div;
}
int main()
{
int n;
double a,b,total_add,total_sub,total_mul,total_div;
while(1!=EOF)
{
printf("Enter two of the values:");
scanf("%lf %lf",&a,&b);
total_add=add(a,b);
total_sub=subtract(a,b);
total_mul=multiply(a,b);
total_div=divide(a,b);
printf("Add Answer: %.2lf\n",total_add);
printf("Subtract Answer: %.2lf\n",total_sub);
printf("Multiply Answer: %.2lf\n",total_mul);
printf("Division Answer: %.2lf\n",total_div);
}
return 0;
}