Question





1) [5 pts] Build a simple calculator using switch statements which can perform the four operations +, -, *,/. Write a program
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Answer 1-

using System;

public class Test1

{   

public static void Main()

{

double c;

Console.Write("Enter an Operator(+,-,*,/):");

string op=Console.ReadLine();

Console.Write("Enter two Operands : ");

double n1= Convert.ToDouble(Console.ReadLine());

double n2= Convert.ToDouble(Console.ReadLine());

switch (op)

{

case "+":

c=n1+n2;

Console.WriteLine(" {0} + {1} = {2} ", n1,n2,c);

break;

case "-":

c=n1-n2;

Console.WriteLine(" {0} - {1} = {2} ", n1,n2,c);

break;

case "*":

c=n1*n2;

Console.WriteLine(" {0} * {1} = {2} ", n1,n2,c);

break;

case "/":

c=n1/n2;

Console.WriteLine(" {0} / {1} = {2} ", n1,n2,c);

break;

default:

Console.WriteLine("Default case");

break;

}   

  

}

}

Screenshot-

save un share repl talk Sign up , run Ce, and host your programs and apps online for free. D saved Mono C# compiler version 4

Answer 2-

using System;

public class Test2

{   

public static void Main()

{

double c;

Console.Write("Enter the number of bars:");

int val=Convert.ToInt32(Console.ReadLine());

if(val<0)

{

Console.WriteLine(" Number of bars can't be negative");

}

else

{

switch (val)

{

case 1:

c=0.75;

Console.WriteLine(" Cost of {0} bars is $ {1} ", val,c);

break;

case 2:

c=1.25;

Console.WriteLine(" Cost of {0} bars is $ {1} ", val,c);

break;

case 3:

c=1.65;

Console.WriteLine(" Cost of {0} bars is $ {1} ", val,c);

break;

default:

c=1.65+(val-3)*0.30;

Console.WriteLine(" Cost of {0} bars is $ {1} ", val,c);

break;

}

}   

  

}

}

Screenshot-

# compiler, Online C# IDE, and online C# RE. le Ca, run Cs, and host your programs and apps online for free. un share 요요 rep

Add a comment
Know the answer?
Add Answer to:
1) [5 pts] Build a simple calculator using switch statements which can perform the four operations...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In-Class Assignment 4 (15 pts) Structured Programming, if Statements 1. Write a program that prompts the...

    In-Class Assignment 4 (15 pts) Structured Programming, if Statements 1. Write a program that prompts the user to enter a value. Print a message stating whether the input value is less than zero or greater than zero or equal to zero. Submit a screenshot of output for each case. See below for sample output Microsoft Visual Studio Debug Console Microsoft Visual Studio Debug Console Microsoft Visual Studio Debug Console Enter a value 5 Enter a value you entered zero. Enter...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • Problem 3 (20 pts) It is required to write a Matrix Calculator Program using C Programming...

    Problem 3 (20 pts) It is required to write a Matrix Calculator Program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should prompt the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • Write a Java program which allows the user to perform simple tasks on a calculator. A...

    Write a Java program which allows the user to perform simple tasks on a calculator. A series of methods allows the user to select an operation to perform and then enter operands. The first method displays a menu, giving the user the choice of typing in any one of the following: +, -, *, /, or % representing the usual arithmetic operators (Each operation should be done on numbers negative(-) to positive(+), positive(+) to negative(-), negative(-) to negative(-), and positive(+)...

  • with the correct answer please 5. Write a program in C++ that will perform the operation...

    with the correct answer please 5. Write a program in C++ that will perform the operation of a simple CALCULATOR. But its output will be as shown in the output Screen-short. Three(3) Marks Guideline for developing the application. Input Output 1. Start program with asking The output will be question whether user is willing 1. The chosen Operator to continue or not. 2. Calculated result 2. If answer is Yes, Like:- System will ask for c\Users\Alamgir Documents\Visual Studio 2008 Proje....

  • Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the...

    Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the console (inclusive). The identified prime numbers must be stored in an array . The inputted number should be between 0 and 1000 (inclusive). array is large enough to o Make sure your hold all the prim e numbers. Dynamic memory allocation is not required for this assignment, so you can have unused space in your array Make sure you can handle the cases of...

  • c++ please Exercise 2: A scout team consists of five children. There are four duties with...

    c++ please Exercise 2: A scout team consists of five children. There are four duties with a day off to be rotated among team members. Duties are: fetching water, collecting firewood, cooking, and cleaning up. You are to write a program that prompts the user for the desired number of working days and prints a schedule of daily duties for the team. Use two separate list objects to store the team member names and the duties, respectively. The program must...

  • Using Python, Can someone please assist in the following: These are the hints: Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the...

    Using Python, Can someone please assist in the following: These are the hints: Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the user for the size of the multiplication table (from 2x2 to 10x10). Use a validation loop to display a warning if the number is less than 2 or greater than 10 and prompt the user to enter the data again until they enter a valid number Put a...

  • Need some help I am not understanding this programming class at all. We are using Microsoft...

    Need some help I am not understanding this programming class at all. We are using Microsoft visual studio with python in console mode to complete these labs. Double-click to hide white space CIS115 Week 4 Lab Overview Title of Lab: Multiplication Table in Python Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the user for the size of the multiplication table (from 2x2 to 10x10). Use a validation loop to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT