How to create a simple calculator in C Programming Languge ?
/* C programme to creat simple calculator */
#include <stdio.h>
float add(float a,float b);
float substract(float a,float b);
float multiply(float a,float b);
float divide(float a,float b);
int main(void)
{
float num1,num2;
float w,result=6;
char ch,c;
printf("Enters two numbers to add,substract,multiply or divide\n");
scanf("%f %f",&num1,&num2);
printf("enter a to add, s to substract ,m to multiply,d to divide\n");
scanf("%c %c",&c,&ch);
if(ch=='a'||ch=='A')
{
result=add(num1,num2);
printf("%f\n",result);
}
else if(ch=='s'||ch=='S')
{
result=substract(num1,num2);
printf("%f\n",result);
}
else if(ch=='m'||ch=='M')
{
result=multiply(num1,num2);
printf("%f\n",result);
}
else if(ch=='d'||ch=='D')
{
result=divide(num1,num2);
printf("%f\n",divide(num1,num2));
}
else
{
printf("please input the correct character\n");
}
return 0;
}
float add(float a,float b)
{
float add;
add=a+b;
return add;
}
float substract(float a,float b)
{
float substract;
substract=a-b;
return substract;
}
float multiply(float a,float b)
{
float multiply;
multiply=a*b;
return multiply;
}
float divide(float a,float b)
{
float divide;
divide=(float)a/(float)b;
return divide;
}