Calculator program in C: To implement basic calculator functionality programming. We include all the basic functions that calculator performs.

How to implement a calculator program in c
We know the simple mathematical operation. Using a switch case we can implement a calculator. This means whenever a user wants to calculate any operation its searches for a case with the operation and performs the mathematical operation.

Page Contents

## Algorithm to implement calculator

1. Write different cases with basic operation as a choice.
2. Take the input operation. operator
3. Take the input numbers. no1 and no2
Check choice. switch(operator)
If matches
perform a specific operation and break

## Calculator program in C

```#include<stdio.h>

main()

{

char choice;

float num1,num2,result;

int flag=1;

printf("Enter +,-,/,* for knowing the result\n");

scanf("%c",&choice);

printf("Enter number 1\n");

scanf("%f",&num1);

printf("Enter number 2\n");

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\n",num1,choice,num2);

}

}```