1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
emmasim [6.3K]
3 years ago
13

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)
Computers and Technology
1 answer:
dlinn [17]3 years ago
5 0

Answer:

// calculator program in C++.

#include <bits/stdc++.h>

using namespace std;

// main function

int main()

{

// variables

int x,y;

int ch;

   cout<<"Enter 1st number:";

   // read first number

   cin>>x;

   cout<<"Enter 2nd number:";

   // read second number

   cin>>y;

   cout<<"Choose 1) for add, 2) for subtract, 3) for multiply or 4) for divide):";

   // read choice

   cin>>ch;

   switch(ch)

   {

       // if choice is 1, find Addition

       case 1:

       cout<<"Addition of two numbers is:"<<(x+y)<<endl;

       break;

       // if choice is 2, find subtraction

       case 2:

       cout<<"Subtract the second from the first is:"<<(x-y)<<endl;

       break;

       // if choice is 3, find multiplication

       case 3:

       cout<<"Multiplication of two numbers is:"<<(x*y)<<endl;

       break;

       // if choice is 4, find division

       case 4:

       cout<<"Division the first by the second is:"<<(x/y)<<endl;

       break;

       // if choice is other

       default:

       cout<<"Wrong choice:"<<endl;

       break;

   }

return 0;

}

Explanation:

Read two numbers from user.Then read the choice of operation from user.If choice is 1, then print the Addition of both numbers.If choice is 2, then print the subtraction. If the choice is 3, then print the multiplication.If choice is 4, then print the  division.If choice is other than these, then print wrong choice.

Output:

Enter 1st number:10                                                                                                      

Enter 2nd number:2                                                                                                      

Choose 1) for add, 2) for subtract, 3) for multiply or 4) for divide):3                                                    

Multiplication of two numbers is:20

You might be interested in
It is a good idea to include your teacher’s name on a title page.
LenaWriter [7]

Answer: I think it all depends on the teacher because I know one of my teachers like when we put their name on our assignments. So I pick true

Explanation:

6 0
3 years ago
Read 2 more answers
This uses the gps methodology to predict the likely location of an offender's base of operation:
iVinArrow [24]
Crime pattern analysis <span>uses the gps methodology to predict the likely location of an offender's base of operation.</span>
6 0
3 years ago
Git pull is a combination of which 2 other git commands?.
Kobotan [32]

Answer:

git fetch followed by git merge

Explanation:

8 0
2 years ago
Presentations are used to communicate______ or to help persuade someone of a new idea. A good presentation will contain a mix of
kvv77 [185]
Just quizlet it believe me it will help 100%
7 0
2 years ago
Create a dictionary that will hold AT LEAST 3 categories for food with at least 3 foods for each category. E.g. Fruits --&gt; Ap
RSB [31]

Answer:

  1. food_category = {
  2.    "Fruits": ["Apple", "Strawberries", "Orange"],
  3.    "Vegetables": ["Carrots", "Peas", "Onions"],
  4.    "Meats": ["Chicken", "Beef", "Mutton"]
  5. }
  6. for cat in food_category:
  7.    output = cat + ": "
  8.    for food in food_category[cat]:
  9.        output += food + ", "
  10.    print(output)
  11. answer = input("Do you want to add category or food (Y - Yes  N - No): ")
  12. while(answer == "Y"):
  13.    choice = input("New Category (N) or New Food (nf): ")
  14.    
  15.    if(choice == "N"):
  16.        new_cat = input("Enter new category name: ")
  17.        if(new_cat not in food_category):
  18.            food_category[new_cat] = ""
  19.    elif(choice == "nf"):
  20.        cat = input("Enter category: ")
  21.        new_food = input("Enter new food name: ")  
  22.        food_category[cat].append(new_food)
  23.    
  24.    answer = input("Do you want to add new category or food (Y - Yes N - No): ")
  25. print("All food categories: ")
  26. for cat in food_category:
  27.    print(cat)
  28. selected_cat = input("Select a category to view the food list: ")
  29. output = ""
  30. for food in food_category[selected_cat]:
  31.    output += food + ", "
  32. print(output)

Explanation:

The solution code is written in Python 3.

Firstly, create a food category dictionary data structure (Line 1 -5). Next, display all the food category and the food in each category (Line 7 - 11)

Next prompt user to feedback if they wish to add new category or new food (Line 13).

While the answer is yes prompt user to enter their choice, either category or food and use if else if statements to handle the choice made by the user. If the choice is new category, prompt user to enter new category name and check if the new category is already exist in the dictionary. If not, add that category to dictionary (Line 18 - 21).

If the choice is food, nf, prompt user to input food category and enter the new food name and add it to the food_category list (Line 22 - 25).

After that prompt user to enter if they wish to continue to add new data (Line 27).

After collecting all info from user, write a for loop to display all the categories (Line 29 - 31).

At last, prompt user to select a category and display all the food of the selected category (Line 33 - 37).

8 0
2 years ago
Other questions:
  • An array of strings , names , has been declared and initialized . write the statements needed to determine whether any of the th
    14·1 answer
  • This was not a "true" operating system, but rather an operating environment.
    5·1 answer
  • Type the correct answer in the box spell all words correctly
    8·1 answer
  • 5. Tricks of the language commercials use are called Rhetorical Devices?
    9·1 answer
  • In order for you to make a wireless connection it must first find an availble
    8·1 answer
  • "A(n) ___________ is a radio transceiver that plays the same role as a hub or switch in a wired network and connects the WLAN to
    14·1 answer
  • A security utility program that scans the system for small programs that interfere with how a computer functions are _____ utili
    13·1 answer
  • The sameNumber method checks to see if the number of words in the phrase, numwords, is the same number of words contained in the
    5·1 answer
  • Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon or liters/kmâ
    5·1 answer
  • We can improve our knowledge and skills on animals and fish raising with the use of internet​
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!