Question
Please do this in C language. Thank you
1. Write the following functions that compute the volume and surface of a sphere with radius r; a cylinder with circular base
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14285714286

// Function to calculate and return volume of a sphere
float sphereVolume(float r)
{
// Calculates and returns volume of a sphere
return ((4.0f/3.0f) * PI *(r * r *r ));
}// End of function

// Function to calculate and return surface of a sphere
float sphereSurface(float r)
{
// Calculates and returns surface area of a sphere
return (4.0f * PI * r * r );
}// End of function

// Function to calculate and return volume of a cylinder
float cylinderVolume(float r, float h)
{
// Calculates and returns volume of a cylinder
return (PI * r * r * h);
}// End of function

// Function to calculate and return surface of a cylinder
float cylinderSurface(float r, float h)
{
// Calculates and returns surface area of a cylinder
return (2.0f * PI * r * (r + h));
}// End of function

// Function to calculate and return volume of a cone
float coneVolume(float r, float h)
{
// Calculates and returns volume of a cone
return (1.0/3.0) * PI * r * r * h;
}// End of function

// Function to calculate and return surface of a cone
float coneSurface(float r, float h)
{
// Calculates and returns surface area of a cone
return PI * r * (r + sqrt(r * r + h * h));
}// End of function

// main function definition
int main()
{
// To store radius and height entered by the user
float r, h;
// To store user choice
int choice;

// Loops till user choice is not 7
do
{
// Displays menu
printf("\n\n **************** MENU **************** ");
printf("\n\t 1 - Sphere Volume \n\t 2 - Sphere Surface \n\t 3 - Cylinder Volume \
\n\t 4 - Cylinder Surface \n\t 5 - Cone Volume \n\t 6 - Cone Surface \n\t 7 - Exit \
\n\t\t What is your choice? ");
// Accepts user choice
scanf("%d", &choice);

// Checks user choice and calls appropriate function and displays result
switch(choice)
{
case 1:
printf("\n Enter radius: ");
scanf("%f", &r);
printf("\n\n Sphere Volume: %.2f", sphereVolume(r));
break;
case 2:
printf("\n Enter radius: ");
scanf("%f", &r);
printf("\n\n Sphere Surface area: %.2f", sphereSurface(r));
break;
case 3:
printf("\n Enter radius: ");
scanf("%f", &r);
printf("\n Enter height: ");
scanf("%f", &h);
printf("\n\n Cylinder Volume: %.2f", cylinderVolume(r, h));
break;
case 4:
printf("\n Enter radius: ");
scanf("%f", &r);
printf("\n Enter height: ");
scanf("%f", &h);
printf("\n\n Cylinder Surface area: %.2f", cylinderSurface(r, h));
break;
case 5:
printf("\n Enter radius: ");
scanf("%f", &r);
printf("\n Enter height: ");
scanf("%f", &h);
printf("\n\n Cone Volume: %.2f", coneVolume(r, h));
break;
case 6:
printf("\n Enter radius: ");
scanf("%f", &r);
printf("\n Enter height: ");
scanf("%f", &h);
printf("\n\n Cone Surface area: %.2f", coneSurface(r, h));
break;
case 7:
printf("\n\n\t\t Thanks.");
exit(0);
default:
printf("\n\n\t\t Invalid Choice!!");
}// End of switch - case
}while(1);// End of do - while loop
}// End of main function

Sample Output:

**************** MENU ****************
1 - Sphere Volume
2 - Sphere Surface
3 - Cylinder Volume
4 - Cylinder Surface
5 - Cone Volume
6 - Cone Surface
7 - Exit
What is your choice? 1

Enter radius: 4


Sphere Volume: 268.19

**************** MENU ****************
1 - Sphere Volume
2 - Sphere Surface
3 - Cylinder Volume
4 - Cylinder Surface
5 - Cone Volume
6 - Cone Surface
7 - Exit
What is your choice? 2

Enter radius: 2


Sphere Surface area: 50.29

**************** MENU ****************
1 - Sphere Volume
2 - Sphere Surface
3 - Cylinder Volume
4 - Cylinder Surface
5 - Cone Volume
6 - Cone Surface
7 - Exit
What is your choice? 3

Enter radius: 4

Enter height: 6


Cylinder Volume: 301.71

**************** MENU ****************
1 - Sphere Volume
2 - Sphere Surface
3 - Cylinder Volume
4 - Cylinder Surface
5 - Cone Volume
6 - Cone Surface
7 - Exit
What is your choice? 4

Enter radius: 5

Enter height: 1


Cylinder Surface area: 188.57

**************** MENU ****************
1 - Sphere Volume
2 - Sphere Surface
3 - Cylinder Volume
4 - Cylinder Surface
5 - Cone Volume
6 - Cone Surface
7 - Exit
What is your choice? 6

Enter radius: 3

Enter height: 2


Cone Surface area: 62.28

**************** MENU ****************
1 - Sphere Volume
2 - Sphere Surface
3 - Cylinder Volume
4 - Cylinder Surface
5 - Cone Volume
6 - Cone Surface
7 - Exit
What is your choice? 9


Invalid Choice!!

**************** MENU ****************
1 - Sphere Volume
2 - Sphere Surface
3 - Cylinder Volume
4 - Cylinder Surface
5 - Cone Volume
6 - Cone Surface
7 - Exit
What is your choice? 7


Thanks.

Add a comment
Know the answer?
Add Answer to:
Please do this in C language. Thank you 1. Write the following functions that compute the...
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
  • This is for Pycharm a python course. Can I get some help doing this and understand...

    This is for Pycharm a python course. Can I get some help doing this and understand it? ⑤ : ib tps://canvas.pasadena.edu/courses/1032754/assi a ☆ 西%N m Crowfall. Th O computing, with-c- O PHP Tutorial | SoloL ontoja. on tojav.. Write functions . def main) . def sphereVolume(r) . def sphereSurfacelr) def cylinderVolumetr. h) . def cylinderSurface(r, h) . def coneVolumefr, h) . def coneSurface(r, h) ences rations main() # call amin function at teh end In the main display, a menu...

  • Write a C program named assignment04.cpp that will ask for and read in a float for...

    Write a C program named assignment04.cpp that will ask for and read in a float for a radius ask for and read in a float for the height each of the following functions will calculate some property of a shape (area, volume, circumference, etc) The functions will accept one or two parameters (radius and/or height) and return the proper calculation. There is no input/output (cin or cout) in the function – just the calculation write the following functions float areaCircle(float...

  • Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions...

    Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions you created. Only use one value of r and h for all the function. Ask the user for the r and h in the main() as an input, and h for all the functions. You should put the functions for areas in a separate file. ​ For example, firstDigtec7ze Write a function e digits n) (returning the number of digits in the argument returning...

  • in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base cla...

    in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base classes. Circle int r; void area(); void perimeter(); void volume(); //No volume for circle Sphere int r; void area();//Sphere surface area= 4 × pi × radius2 void perimeter(); //No perimeter for Sphere void volume();//Sphere volume= 4/3 × pi × radius2 Cylinder int r, height; void area();// Cylinder surface area=perimeter of...

  • Identify the errors in this C program (struct - calculating volume, area of cylinder). Explain. #include...

    Identify the errors in this C program (struct - calculating volume, area of cylinder). Explain. #include <stdio.h> #define PI 3.14 typedef struct cylinder{    float r,h;    float areacircle=2*PI*r;    float areacylinder;    float volumecylinder;    areacylinder=2*PI*r*h+2*PI*r*r;    volumecylinder=PI*r*r*h; }; int main() {    float start,numcyls;    printf("How many cylinders do you want? Enter a positive integer.\n");    scanf("%f",&numcyls);    if(numcyls<1||ceilf(numcyls)!=numcyls)    while()    {printf("The number you have entered is not a positive integer. Please try again.\n");        printf("Remember,...

  • C++ Pr ogramming (CSC-115) Functions (pass by Reference) Programming project Using FUNCTIONS, write a C++ program...

    C++ Pr ogramming (CSC-115) Functions (pass by Reference) Programming project Using FUNCTIONS, write a C++ program that calculates the Area of a cirele, a square and a rectangle. Your program must prompt the user to select which area is to be calculated. Document your program. Apply the do while loop to repeat the program Apply the while loop for input validation. Apply the switch statements or the if/ else if statement to prompt the user for the area's selection. Based...

  • Need in C++ Assignment #1: Write a program to ask the user for a sphere radius...

    Need in C++ Assignment #1: Write a program to ask the user for a sphere radius r as a double. Then compute and display the sphere surface area using S-4*PI'r*r as double. Then compute and display the sphere volume using V-(1.33)*PIr'r*r as double. Use: const double PI-3.14;

  • PLEASE TYPE OUT IN TEXT (please no pdf or writing) C++ CODE Consider the following program...

    PLEASE TYPE OUT IN TEXT (please no pdf or writing) C++ CODE Consider the following program in which the statements are in the incorrect order. Rearrange the statements in the following order so that the program prompts the user to input: The height of the base of a cylinder The radius of the base of a cylinder The program then outputs (in order): The volume of the cylinder. The surface area of the cylinder Format the output to two decimal...

  • Write a C++ program to compute the total volume of N cones, where N is a...

    Write a C++ program to compute the total volume of N cones, where N is a number entered by the user. Repeat the request for N until the user enters a positive number. For each cone, ask for the radius and height, compute the volume, and add that volume to the total. Each time you ask the user to enter either the height or the radius, if the user does not enter a positive number, display an error message and...

  • Problem: Write a short C++ program that gets the side of a cube and the radius...

    Problem: Write a short C++ program that gets the side of a cube and the radius of a sphere from the keyboard and writes to a file the surfaces of these shapes.             ------------------------------------------------------------------------------------------------------------------------ Your task: implement in C++ the algorithm solution shown below. ------------------------------------------------------------------------------------------------------------------------ Part A (79 points) Algorithm solution (in pseudocode): To accomplish this task, your program will have to take the following steps: 1. Declare a variable named outFile that represents an output stream. 2. Declare a...

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