Write a C program using switch case to find addition, subtraction, multiplication and division of two numbers
Answers
Answer:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
return 0;
}
Explanation:
Hope it helps.
Answer:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
float a,b,ans;
printf("Enter any two value: ");
scanf("%f%f",&a,&b); //Input two values
printf("\nPress 1 to find sum of that two numbers");
printf("\nPress 2 to find subtract of that two numbers");
printf("\nPress 3 to find multiply of that two numbers");
printf("\nPress 4 to find divide of that two numbers");
printf("\nEnter your choice: ");
scanf("%d",&x); //Input 1-4 as per user choice
switch(x)
{
case 1: //If entered 1 by user
ans=a+b; //Sum of two numbers
break;
case 2: //If entered 2 by user
ans=a-b; //Multiply of two numbers
break;
case 3: //If entered 3 by user
ans=a*b; //Subtract of two numbers
break;
case 4: //If entered 4 by user
ans=a/b; //Divide of two numbers
break;
default: //If user entered choice except 1-4
printf("\nYou have entered a wrong choice");
exit(0);
}
printf("\nThe Answer will be = %.2f",ans); //Print the answer as per user choice
return 0;
}
Explanation:
This program takes an arithmetic operator ‘+’, ‘-’, ‘*’, ‘/’ and two operands from the user. Then, it performs the calculation on the two operands depending upon the operator entered by the user.