C Program To Calculate Volume Of CUBE,CUBOID, CYLINDER, SPHERE ,CONE | TechWithCode.com

yes, Today I will explain to you how to write a c program that calculates the volume of any shapes like CUBE, CUBOID, CYLINDER, SPHERE, CONE.

To understand the code of volume calculation in C, you must know basic mathematics formulas and the Switch Case of C programming.

Before the explanation of the source code of the volume calculation program, I want to share important information that At TechWithCode, we are doing something that will motivate you to do more programming. In this, you have to solve questions [choose from our website or make questions on your own] in any programming language and share your code with us, we will publish your code with your name on our website. For more Details ClickHere

                         ✽Send Solution and Get Credit✽

Let’s come to our main topic, So here is the Source code of the C program to calculate the volume of CUBE, CUBOID, CYLINDER, SPHERE, CONE

#include<stdio.h>
#include<math.h>

 float cube()
 {
  float a;
  float V;
  printf("Enter value of side\n");
  scanf("%f",&a);
  V=a*a*a;
  return V;
 }
 float cuboid()
 {
  float l,b,h;
  float V;
  printf("Enter length,breadth & hight of cuboid\n");
  scanf("%f%f%f",&l,&b,&h);

  V=l*b*h;
  return V;
 }
 float cylender()
 {
  float r,h;
  float V;
  printf("Enter radius & hight of cylender\n");
  scanf("%f%f",&r,&h);
  V=3.14*r*r*h;
  return V;
 }
 float sphere()
 {
  float r;
  float V;
  printf("Enter radius of sphere\n");
 scanf("%f",&r);
  V=(4*3.14*r*r*r)/3;
  return V;
 }
 float cone()
 {
  float r,h;
  float V;
  printf("Enter radius & hight of cone\n");
  scanf("%f%f",&r,&h);
  V=(3.14*r*r*h)/3;
  return V;
 }
 float display(float V)
 {
  printf("Volume is = %f\n",V);
 }

int main()
{
 int ch,q;
 float res;
 
 
 while(1)
 {
  printf("Select the shape for calculate volume\n");
  printf("Press 1 for select CUBE\n");
  printf("Press 2 for select CUBOID\n");
  printf("Press 3 for select CYLINDER\n");
  printf("Press 4 for select SPHERE\n");
  printf("Press 5 for select CONE\n");
  scanf("%d",&ch);
  
   switch(ch)
     {
         case 1: res=cube();break;
         case 2: res=cuboid();break;
         case 3: res=cylender();break;
         case 4: res=sphere();break;
         case 5: res=cone();break;
         default: printf("Wrong input");
          
     }
  if(ch>=1 && ch<=5)
   display(res);
  printf("\n\npress any key to countinue... / press 0 to EXIT\n\n\n");
  q=getch();
  
  if(q==48){
   return 0;
  }
  
 }
    
}

OutPut:-

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top