203k views
3 votes
Write a program in c or c++ to perform different arithmeticoperation using switch statement .the program will take two inputintegers from the user and then an arithmetic operator from theuser to perform the specific operation the given twointegers?

User Lbris
by
8.4k points

1 Answer

5 votes

Answer:

C code :

#include<stdio.h>

int main()

{

int j;

float k,l, x; //taking float to give the real results.

printf("Enter your two operands: "); //entering the numbers on which //the operation to be performed.

scanf("%f %f", &k, &l);

printf("\\ Now enter the operation you want to do: ");

printf("1 for Addition, 2 for Subtraction, 3 for Multiplication, 4 for Division ");

scanf("%d", &j); //j takes the input for opearation.

switch(j)

{

case 1:

x=k+l;

printf("%.2f+%.2f=%.2f",k,l,x); //we write %.2f to get the result //upto 2 decimal point.

break;

case 2:

x=k-l;

printf("%.2f-%.2f=%.2f",k,l,x);

break;

case 3:

x=k*l;

printf("%.2f*%.2f=%.2f",k,l,x);

break;

case 4:

if(l!=0) //division is not possible if the denominator is 0.

{

x=k/l;

printf("%.2f/%.2f=%.2f",k,l,x);

}

else

printf("Division result is undefined");

default:

printf("\\ invalid operation");

}

}

Output is in image.

Step-by-step explanation:

At first we take two numbers.

Then we take integers from 1 to 4 for Addition, subtraction, multiplication, division respectively.

Then accordingly the case is followed and the operation is performed.

Write a program in c or c++ to perform different arithmeticoperation using switch-example-1
User Veetaha
by
8.6k points