Question

In: Computer Science

Write a program using the switch statement to calculate geometric quantities. Prompt the user to enter...

Write a program using the switch statement to calculate geometric quantities. Prompt the user to enter a radius. Then present a menu of choices for quantities to be calculated from that radius:

                        A         Area of a Circle

                        C         Circumference of a Circle

                        S          Surface Area of a Sphere

                        V         Volume of a Sphere

Prompt the user to enter the character corresponding to the quantity to be calculated. Use a switch statement to handle the calculations. Use the appropriate equation in each case.

Allow for inclusion of both upper case and lower case letters and present an error message for an improper choice. Print the quantity calculated with 2 decimal digits.

When the program is working, modify it using a while or do…while loop so that the program will continue prompting the user to see if another quantity is to be calculated. For example, suppose that a radius is entered and the user specifies that the circumference is to be calculated. After the result is printed, the user will be asked if he or she wants to do another calculation. He might enter Y to continue or N to quit.   Include entry of the radius as well as the switch statement in the loop.

In C++

Solutions

Expert Solution

CODE-

#include <iostream>
#include<cmath>

using namespace std;
//Main block starts
int main() {
//declaring variables
double r, p = M_PI, c = 4 / 3;
char inp, ch;
//do while loop begins
do {
//taking radius input from user
cout << "Enter a radius" << endl;
cin >> r;
//Taking input according to menu
cout << "Enter the choice according to quantities to be calculated from that radius:" << endl;
cout << "A - Area of a Circle" << endl;
cout << "C - Circumference of a Circle" << endl;
cout << "S - Surface Area of a Sphere" << endl;
cout << "V - Volume of a Sphere" << endl;
cin >> ch;
//switch block begins
switch (ch) {
//case to calculate area of circle
case 'A':
case 'a':
printf("%.2f", p * r * r);
break;
//case to calculate circumference of circle
case 'C':
case 'c':
printf("%.2f", 2 * p * r);
break;
//case to calculate surface area of sphere
case 'S':
case 's':
printf("%.2f", 4 * p * r * r);
break;
//case to calculate Volume of sphere
case 'V':
case 'v':
printf("%.2f", c * p * r * r * r);
break;
//case for invalid input
default:
cout << "Not a valid input kindly make an appropiate choice from the given menu" << endl;
break;
}
cout << endl << "If you wish to do any other calculation enter Y else enter N to stop the program" << endl;
cin >> inp;
} while ((inp != 'n') && (inp != 'N'));

return 0;
}

CODE SCREENSHOTS-

OUTPUT-


Related Solutions

Write a program using switch statement that asks user to enter the month number and then...
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till...
Using Java! Write a program that ask prompt the user to enter a dollar amount as...
Using Java! Write a program that ask prompt the user to enter a dollar amount as double. Then, calculate how many quarters, dimes, nickels and pennies are in the dollar amount. For example: $2.56 = 10 quarters, 1 dime, 1 nickel and 1 cent. Print all of the values. Hint: Use Modulus operator and integer division when necessary.
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Write a C program Your program will prompt the user to enter a value for the...
Write a C program Your program will prompt the user to enter a value for the amount of expenses on the credit card. Retrieve the user input using fgets()/sscanf() and save the input value in a variable. The value should be read as type double. The user may or may not enter cents as part of the input. In other words, expect the user to enter values such as 500, 500.00 and 500.10. The program will then prompt the user...
8. write a program using switch-case statements that ask a user to enter a temperature value...
8. write a program using switch-case statements that ask a user to enter a temperature value in degrees Fahrenheit (In your program, use an integer to record the user’s entry. If the temperature falls between 0 and 96 make it print “Temperature below normal”. If the temperature is 97, 98, make it “Temperature normal”. If the temperature is between 100 and 150, print “You have a fever”. If the temperature is outside the ranges given above, display “Are you human”....
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
(b) You will write a program that will do the following: prompt the user enter characters...
(b) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘Q’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘Q’. Example input mJ0*5/]+x1@3qcxQ The ‘Q’ should be included when computing the statistics properties of the input. Since characters are integers...
Write a Python program that has the user enter their name.   Using the user's name calculate...
Write a Python program that has the user enter their name.   Using the user's name calculate the following: 1. How many letters are in the user's name? 2. Print the user's name in REVERSE both in capital letters and lowercase letters 3. Print the ASCII value for each letter of the user's name. 4. Print the SUM of all of the letters of the user's name (add each letter from #3)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT