Freelancer

Volume of a cylinder

Volume of a cylinder - C Programming Example

A cylinder with radius r units and height h units has a volume of V cubic units given by 
C programming example

V=πr2h

C Programming Code

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
    float r, h, volume,pi;
    pi=3.1416;
    printf("Input the radius\n");
    scanf("%f",&r);
    printf("Input the height\n");
    scanf("%f",&h);
    volume=2*pi*r*r*h;
    printf("The volume of the cylinder is = %f", volume);
    getch();
}

Output of the C Program

C programming example
Output

EXPLANATION of C Program

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

Firstly the three headers have to be included, firstly <stdio.h>, as it is a mathematical problem the header <math.h> is crucial. <conio.h> has been included as a precaution so that the program doesn’t turn off before showing the result. As <conio.h> has been included, instead of return0 at the end of the code getch(); has to be written.
main()
 Then main()has been included as we are doing this solution as a main function. Then { is used to start the code and in the end } used to end the code. Basically the code has to be written within these two second brackets {}.
    float r, h, volume,pi;
    pi=3.1416;
 Coming to the code and problem itself, as the area, length etc. aren’t always in integers that’s why for proper solution of the problem float has been used. To save the inputs r, h, volume, pi has been chosen as random representatives of the numbers or in this case floats.
Then we have declared what pi stands for
Pi=3.1416
    printf("Input the radius\n");
    scanf("%f",&r);

After that, printf needs to be written so that the program shows the verse/verses within the (“ ”) after printf.
(“Input the radius\n”) is written so that the user inputs the length radius and \n is added in the end so the length is shown is the next line. Here \n is added just for adding finesse to the program.
Scanf is written, because when the input is given the computer has to scan the input in order to show that input in the program. Within (“ ”) ,%f is written as it is the command to scan floats in computer and also we are using floats to solve the problem. And to save the input &is used before the random character r, so that the computer scans and save the number in r, where is r is its representative in the computer.
The same process has been used for the height of the cylinder, with h as random character.
    volume=2*pi*r*r*h;
We know that the volume of a cylinder is:
(pi x (radius of the cylinder)^2 x height of the cylinder )
(radius)^2 can also be showed as radius x radius.
For solving this problem we used the formula, and hence, volume has been declared like this:
volume= pi*r*r*h, so here volume is used as the formula for figuring out the volume of the cylinder.
Again printf is added to show the result of the problem
Printf(“The volume of the cylinder is = %.2f”, volume)
 %.2fand volume is added here because the program will show the value of volumeusing the previously given data inputs, save the data there scan it and show it instead of %.2f when the program is run. Here also %f is used to scan the numbers as we are using floats. In short fstands for float. And .2 is added so that the end result only shows only 2 numbers after the decimal point, such as 0.00.
 getch()
Finally getch();  is included to end the program.
Note: make sure there is an ; at the end of each line, or that (   )and “    ’’ are properly placed, most importantly check and make sure that the formulas are correct. Moreover analyze the problem and solve it step by step properly.

You may also like this C programs







No comments

Powered by Blogger.