Skip to main content

How to Code a Menu Driven Volume Finder Of Different Shapes

How to find volume of different shapes using a menu



Now, let's talk about a little bit about this code. You have to write a program which will let you choose what shape's volume you want to find out & after this it will execute for that particular shape to find out it's volume. 
So to code this our main target is to build a menu. So how can we do that? It's really simple. We just use a switch case to do this program.


BASIC STEPS TO CODE THIS PROGRAM

1. First you should take the integer variable so that we can choose option from this like, Press 1 to find volume of cone, Press 2 to find volume of cylinder & so on. After that we should take float variables to store height, length, radius, width, edge length etc.

2. Next we use a switch case to execute the option driven code.

3. Now we will separately consider each case & make proper formula to find out the correct answer for that specific option selected shape's volume. That's all. 

Isn't it so simple???? Ok. Let's see how to code this & also the example of input & output format for that program.

 C code Snippet

//program to execute a menu driven volume finder
#include <stdio.h>
int main(){
    int n;
    float ans,h,r,l,w,a;
    printf("Press 1 to find volume of Cone\n");
    printf("Press 2 to find volume of Cylinder\n");
    printf("Press 3 to find volume of Pyramid\n");
    printf("Press 4 to find volume of Cube\n");
    printf("Press 5 to find volume of Sphere\n");
    printf("Choose an option: ");
    scanf("%d",&n);
    switch (n)
    {
    case 1:
        printf("Enter the height of the cone: ");
        scanf("%f",&h);
        printf("Enter the radius of the base of the cone: ");
        scanf("%f",&r);
        ans = (3.14*r*r*h)/3;
        printf("The volume of this cone is = %f", ans);
        break;
    case 2:
        printf("Enter the height of the cylinder: ");
        scanf("%f",&h);
        printf("Enter the radius of the base of the cylinder: ");
        scanf("%f",&r);
        ans = (3.14*r*r*h);
        printf("The volume of this cylinder is = %f", ans);
        break;
    case 3:
        printf("Enter the height of the pyramid: ");
        scanf("%f",&h);
        printf("Enter the base length of the pyramid: ");
        scanf("%f",&l);
        printf("Enter the base width of the pyramid: ");
        scanf("%f",&w);
        ans = (l*w*h)/3;
        printf("The volume of this pyramid is = %f", ans);
        break;
    case 4:
        printf("Enter the edge length of the cube: ");
        scanf("%f",&a);
        ans = (a*a*a);
        printf("The volume of this pyramid is = %f", ans);
        break;
    case 5:
        printf("Enter the radius of the sphere: ");
        scanf("%f",&r);
        ans = (4*3.14*r*r*r)/3;
        printf("The volume of this sphere is = %f", ans);
        break;
   
    default:
        printf("Error: Please enter a valid input. ");
        printf("Try to choose between 1 to 5");
        break;
    }
    return 0;
}

Input



Output





Comments

Popular posts from this blog

C code to calculate product of even digits of a given number

 C code to calculate product of even digits of a given number Code Steps: 1. First input the number 2. Now initialize a variable =1, that stores the multiplication  3. Now start a while loop until number = 0 4. Inside while loop just find out all the digits by finding the remainder & check it if it is divisible by 2  5. If the number is divisible by 2 then multiply that number with the variable that was initialize = 1 6. At last return the multiplication storing variable.  C Code Snippet #include <stdio.h> int main (){     int num ;     int product = 1 ;     printf ( "Enter the number: " );     scanf ( " %d " , & num );     int temp = num ;     while ( num != 0 ){                 int digit = num % 10 ;         num = num / 10 ;         if ( digit % 2 == 0 ){         ...

10 Tips for Writing Faster and More Efficient Code

 10 Tips for Writing Faster and More Efficient Code Writing efficient code may seem like an impossible task when you’re just starting out with your programming journey, but the truth is that there are several steps you can take to make your code run faster and use fewer resources. You don’t have to be some sort of genius or even have years of experience to achieve this – here are 10 simple tips for writing more efficient code! 1) Use Type Alias When you need to define your own data type, consider using a type alias. This saves you from having to repeatedly reference more verbose names like String or NSNumber. Using a type alias also helps reduce pollution of existing types, which can cause problems later on. For example, adding an int property on top of NSObject will break a lot of code that uses that class without changing its definition. 2) Use Option Auto or Type Keywords Most developers have different preferences, but it is important to use Option Auto or Type Keywords. This wi...