Write a C program for Calculator

Write a C program for Calculator

#include<stdio.h>
main()
{
char choice;
float num1,num2,result;
int flag=1;

printf(“Enter +,-,/,* for knowing the resultn”);
scanf(“%c”,&choice);

printf(“Enter the number 1n”);
scanf(“%f”,&num1);

printf(“Enter number 2n);
scanf(“%f”,&num2);

switch(choice)
{
case ‘+’:
result=num1+num2;

break;
case ‘-‘:

result=num1-num2;
break;

case ‘/’:

{
if(num==2)
{
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 = undefinedn”,num1,choice,num2);
}
}

OUTPUT

Enter +,-,/,* for knowing the result

*

Enter number 1

1234

Enter number 2

1234

1234.00000*1234.00000= 1522756.00000

Leave a Comment

error: Content is protected !!