C Program To Calculate Volume Of All the shapes in just one C program.
yes, Today I will explain to you that how to write a c program which calculates the volume of any shapes like CUBE, CUBOID, CYLINDER, SPHERE, CONE.
To understand the code of volume calculation in C, you must have knowledge about basic mathematics formulas and the Switch Case of C programming.
Before the explanation of the source code of volume calculation program, I want to share important information that At TechWithCode, we are doing something which will motivate you to do more programming. In his, you have to solve questions[chose 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
Let's come to our main topic, So here is the Source code of C program to calculate the volume of CUBE, CUBOID, CYLINDER, SPHERE, CONE
- Source Code:-
#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:-
Explanation of above code:-
In this Code, I have use Switch case, function, and many concepts of c programming. First, I ask to select the option from the user that the volume of which shapes the user wants to calculate then I pass the user choice in the switch case and call the appropriate function. that functions return the calculated value of volume in the main function after that I pass that value in the Display function.
I put all my code in a while loop which is always true so it will repeatedly asking for calculations the volume one after other.
To stop the program you have to enter ZERO ("0").
Thanks You..@author Deepak Kumar.Do you want to become a good programmer, Click Here
✽Send Solution and Get Credit✽
Must Read:-
4. Java Programming
5. C programs With source code
Final Word:-
In this article, We have explained to you how to write a C program that will calculate the volume of any shapes.We hope you like it
5. C programs With source code
Final Word:-
In this article, We have explained to you how to write a C program that will calculate the volume of any shapes.We hope you like it
If This post helps you a little bit then please share this post with your friends. For more Computer and programming Related Tips and tricks, please
Don't forget to share how you like this Content in the comment below. If you have any doubt or suggestion then please write in the comment section below, we will love to hear from you.
thank you sir\mam
ReplyDelete