117k views
4 votes
Write a calculator program using a switch statement that: a) Prompts the user to enter two numbers b) Prompts the user to select between the four arithmetic operations using a choice from 1-4; for example, prompt the user with: "Choose 1) for add, 2) for subtract, 3) for multiply or 4) for divide)" c) Calculates the result of performing the selected operation on the numbers; and d) Displays the answer on the screen. (15 pts)

1 Answer

1 vote

Answer:

Following are the code in C language

#include <stdio.h> // header file

int main() // main method

{

float f1; // variable declarartion

float x,y; //variable declarartion

int option;//variable declarations

printf("Enter Two numbers:");

scanf("%f%f",&x,&y);// input two number

printf("Choose : 1: For add 2: For Subtract 3: for multiply 4: for divide");

scanf("%d",&option); //read the choice

switch(option)

{

case 1: // for addition

printf("%4f",x+y);

break;

case 2:// for subtraction

printf("%4f",x-y);

break;

case 3: //for multiply

printf("%4f",x*y);

break;

case 4: // for division

f1=x/y;

printf("%4f",f1);

break;

default: ////for invalid choice

printf("Invalid choice") ;

}

return 0;

}

Step-by-step explanation:

In this program we taking a two input from user of float types after taking input,the switch statement is used case 1 for addition,case 2 for subtraction case 3 for multiplication,case 4 for division and default for invalid choice .

Output

Enter Two numbers:2.5

2.5

Choose : 1: For add 2: For Subtract 3: for multiply 4: for divide:4

1.000000

User Lukas Ruge
by
5.5k points