Program to make calculator using C
#include <stdio.h>
int main()
{
char choice;
float num1,num2,result;
int flag=1;
printf("Enter +,-,/,* for knowing the result
");
scanf("%c",&choice);
printf("Enter first number
");
scanf("%f",&num1);
printf("Enter second number
");
scanf("%f",&num2);
switch(choice)
{
case '+':result=num1+num2;
break;
case '-':result=num1-num2;
break;
case '/':
{
if(num2==0)
{
flag=0;
}
else
{
result=num1/num2;
}
break;
}
case '*': result=num1*num2;
break;
default:printf("Error");
break;
}
if(flag==1)
{
printf("%f %c %f = %f",num1,choice,num2,result);
}
else
{
printf("%f %c %f = undefined
",num1,choice,num2);
}
}
Output:
Output of simple addition
Output when num2=0
![]() |
Description: In this program,we ask the user to input two numbers along with the operator which they want to do.And using the switch case function, we calculate the value as user wants. |


