C Program for simple calculator to perform addition subtraction multiplication and division based on the symbol using if else ladder statement
Program
#include<stdio.h>
#include<stdlib.h>
void main()
{
float a, b;
char opcode;
printf("Enter two numbers\n");
scanf("%f %f", &a, &b);
printf("Enter\n'+' to add\n'-' to subtract\n'*' to multiply\n'/' to divide\n");
scanf(" %c", &opcode);
if(opcode == '+')
printf("Sum = %f\n", a + b);
else if(opcode == '-')
printf("Difference = %f\n", a - b);
else if(opcode == '*')
printf("Multiplication = %f\n", a * b);
else if(opcode == '/')
{
if(b == 0)
{
printf("Divide By Zero Error\n");
exit(0);
}
else
printf("Division = %f\n", a / b);
}
else
printf("Invalid Operator\n");
}
Output 1
Enter two numbers
9
4
Enter
'+' to add
'-' to subtract
'*' to multiply
'/' to divide
+
Sum = 13.000000
Output 2
Enter two numbers
82
45
Enter
'+' to add
'-' to subtract
'*' to multiply
'/' to divide
-
Difference = 37.000000
Output 3
Enter two numbers
7
3
Enter
'+' to add
'-' to subtract
'*' to multiply
'/' to divide
*
Multiplication = 21.000000
Output 4
Enter two numbers
66
3
Enter
'+' to add
'-' to subtract
'*' to multiply
'/' to divide
/
Division = 22.000000
Output 5
Enter two numbers
8
0
Enter
'+' to add
'-' to subtract
'*' to multiply
'/' to divide
/
Divide By Zero Error