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 for checking a number if it's a Armstrong number or not

 Important Coding Questions (With C Language + Code Snippet) Source: Click This Link Definition of Armstrong Number:  What is an Armstrong Number? A number is thought of as an Armstrong number if the sum of its own digits raised to the power number of digits gives the number itself.  For example, 153, 370, 371, 407 are three-digit Armstrong numbers  and, 1634, 8208, 9474 are  four-digit  Armstrong numbers and there are many more. Input: 1634 Output: 1634 is an Armstrong number. Input: 152 Output: 152  is not an Armstrong number. Code Snippet: #include <stdio.h> int main () {     int num , originalNum , remainder , result = 0 ;     printf ( "Enter a three-digit integer: " );     scanf ( " %d " , & num );     originalNum = num ;     while ( originalNum != 0 ) {         remainder = originalNum % 10 ;         result += remainder * remainder * ...

What is the Difference Between Web Development and Android Development?

 What is the Difference Between Web Development and Android Development? While many people believe that web development and Android development are the same thing, this couldn’t be further from the truth. While both fields involve code and computers, the two actually have significantly different processes, tools, and skillsets required to complete their work. In this article, we’ll explore some of the key differences between web development and Android development in order to help you decide which profession may be better suited to your skill level and interests. Web Development vs Android Development The Ultimate Android vs Web Developers Showdown : There’s a massive amount of discussion around which type of development, web or android, is better. Both sides have passionate proponents who make compelling arguments. We don’t want to get into that argument. Instead, we’re going to look at both types of development and point out some strengths and weaknesses that make each different ...

How to code if two numbers are co-prime or not?

 C code for two numbers to check if they are co-prime or not Question: How can we understand if two numbers are coprime or not? Ans:  A Prime Number is defined as a Number which has no factor other than 1 and itself. But, Co-prime Numbers are Considered in pairs and two Numbers are  Co-prime  if they have a Common factor as 1 only . Their HCF is 1. Now come to the main point that is how to code it in C language so that we can get the correct output. C code snippet: #include <stdio.h> int main (){     int num1 , num2 , hcf ;     printf ( "Enter the first number: " );     scanf ( " %d " , & num1 );     printf ( "Enter the second number: " );     scanf ( " %d " , & num2 );     for ( int i = 1 ; i <= num1 ; i ++ )      {       if ( num1 % i == 0 && num2 % i == 0 )       {         hcf = i ;       ...