Question

In: Computer Science

Write a C++ program to ask the user to enter the shape type: square, sphere or...

Write a C++ program to ask the user to enter the shape type: square, sphere or circle and the appropriate dimensions of the shape.. The program should output the following information about the shape:

a.for a square. it output the area and perimeter.

b. for a sphere, it outputs the area.

c. fir a circle, it outputs the volume.

if the user input anything else, your program should output: "the program does not recognize this shape". Use const whenever it is appropriate (use the nested if)

area of sphere is 4*PI*r^2

area of circle is Pi*r^2 (Pi=3.1416)

Solutions

Expert Solution

code

#include<iostream>
using namespace std;
const float pi=3.14;
int main()
{     
       string type;
    //Declare variables
       float r;int s,perimeter;
       double volume;
       double area;
       cout<<"Enter the shape type: ";
       getline (cin, type);
       if(type=="square"){
           cout<<"Enter Side : ";
    cin>>s;
    //nested if
    if(s>0){
    area = 4*s;
    perimeter = s*s;
    cout<<"Area of Square : "<<area<<endl;
    cout<<"Perimeter of Square : "<<perimeter;
    }
           
       }
       
       if(type=="sphere"){
    
       //Input radius
       cout<<"Input radius: "<<endl;
       cin>>r;
       //Volume of sphere = 4/3(πr3)
       volume=(4/3)*(pi*r*r*r);
       cout<<"Volume of sphere:"<<volume<<endl;
       //Area of sphere = πr2     
       area=pi*r*r;
       cout<<"Area of Circle: "<<area<<endl;
       }
       
       if(type=="circle"){
           cout<<"Enter radius : ";
           cin>>r;
           area = 3.14159*r*r;
        cout<<"area of circle is : "<<area;   
       }
       //remaining condition
       else{
           
           cout<<"The program does not recognize this shape.";
           
       }
       return 0;

      
}

//screenshot

//output:

-------------------------------------------------------------------------------------------------


Related Solutions

Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Using C++ Write a program to ask user to enter a password and validity the password....
Using C++ Write a program to ask user to enter a password and validity the password. The password should be 8 – 20 characters long (8 is included and 20 is included), it should contain at least one uppercase letter and one lower case letter. The password should contain at least one digit, and at least one symbol other than alphabets or numbers. Most importantly, the password may not contain any white space. Functions: You will need at least the...
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that in array. Display that all numbers before and after performing Bubble sort. You must have to create new function with required parameter to perform Bubble sort. Sample Run :- Enter 1 number :- 1 Enter 2 number :- 5 Enter 3 number :- 7 Enter 4 number :- 45 Enter 5 number :- 90 Enter 6 number :- 6 Enter 7 number :- 55...
C++ while loop Exercise Write a program that continues to ask the user to enter any...
C++ while loop Exercise Write a program that continues to ask the user to enter any set of numbers, until the user enters the number -1. Then display the total sum of numbers entered and their average. (note that you need to define a counter that counts how many numbers so the average = (sum/n) where n is your counter total. #include <iostream> using namespace std; int main() { int number, n=0, sum=0; cout << "Enter a number to start...
write a c++ program . Ask the user to enter a number less than 100. Test...
write a c++ program . Ask the user to enter a number less than 100. Test the input to make sure it is correct, and use a while loop to continuously ask the user for correct input value if they don't follow the input rule. After receiving the correct input, test the number to see if it is even or odd. If it is odd, use a while loop to do the following: Output to the monitor all odd numbers...
1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If...
Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If the age is greater than 21 print "welcome," and if the age is less than 21 print "sorry." Use input validation to make sure the birthdate was entered correctly.
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT