Advertisement Remove all ads

Write a Program to Implement Calculator with Following Operations Using Switch Case 1. Add Two Numbers 2. Subtract Two Numbers - Structured Programming Approach

Write a program to implement calculator with following operations using switch case
1. add two numbers
2. subtract two numbers
3. multiply two numbers
4. divide two numbers 

Advertisement Remove all ads

Solution

#include <stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
float result;
char ch;
clrscr();
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
printf("Choose operation to perform (+,-,*,/): ");
scanf(" %c",&ch);
result=0;
switch(ch)
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=(float)num1/(float)num2;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
getch();
}

 

Output: Enter first number: 10
Enter second number: 3
Choose operation to perform (+,-,*,/,%): /
Result: 10 / 3 = 3.333333
Concept: Control Structures
  Is there an error in this question or solution?
Advertisement Remove all ads
Advertisement Remove all ads
Advertisement Remove all ads
Share
Notifications

View all notifications


      Forgot password?
View in app×