Arithmetic Operations using Functions in C

This “Arithmetic Operations using user defined Functions” page explains clearly about how to write the program using user functions to perform arithmetic operations.


 

#include<stdio.h>
float add(float,float);
float substract(float,float);
float multiply(float,float);
float divide(float,float);
int main()
{
int ch;
float a,b,c;
while(1)
{
printf(“1.Add \n”);
printf(“2.Substract \n”);
printf(“3.Mutiply \n”);
printf(“4.Divide \n”);
printf(“5.Quit \n”);
printf(“Enter the choice : “);
scanf(“%d”,&ch);
if(ch>=0 && ch<=3)
{
printf(“Enter two numbers :”);
scanf(“%f%f”,&a,&b);
}
switch(ch)
{
case 1    :    c = add(a,b);
printf(“Result : %f\n\n”,c);
break;
case 2    :    c = substract(a,b);
printf(“Result : %f\n\n”,c);
break;

case 3    :    c = multiply(a,b);
printf(“Result : %f\n\n”,c);
break;

case 4    :    c = divide(a,b);
printf(“Result : %f\n\n”,c);
break;

case 5    :    exit(1);
default    :    printf(“Invalid choice\n\n”);
}
}
}
float add(float x,float y)
{
return x+y;
}
float substract(float x,float y)
{
return x-y;
}
float multiply(float x,float y)
{
return x*y;
}
float divide(float x,float y)
{
return x/y;
}

Share this