# Cal volume of Cylinder using function

Discussion in 'C' started by Shelly77, Apr 4, 2011.

1. ### Shelly77New Member

Joined:
Apr 4, 2011
Messages:
1
0
Trophy Points:
0
Hi, I need some help on how to use functions, I have only used functions in order to do teh alogrithm calculator. I just dont really know where to start with this question.

Code:
```#include <stdio.h>
#include <stdlib.h>

#define PI 3.142

int main () {

int height;
int volume;
int program;

printf ("Enter 2 to start and 8 to quit\t");
scanf ("%d", &program);

while (program !=9)
{
printf ("Press \n 1 for radius \n 3 for height \n 5 to volume \n 9 to quit:\t");
scanf ("%d", &program);

if (program==1) {
printf ("Enter the value in radius\t");
scanf ("%d", &radius);   }

if (program==3) {
printf ("Enter the value for height:\t");
scanf ("%d", &height);   }

if (program==5) {
volume = PI * radius * radius * height;
printf ("\n VOLUME OF CYLINDER WITH RADIUS %dUNITS: & HEIGHT %dUNITS: IS %d CUBIC UNITS\n", radius, height, volume);
printf ("\n Do you wish to calculate the Volume of another Cylinder?\n");           }
}
system ("pause");
return 0;
}

/* Write a C program that uses a function to calculate the volume of a cylinder.
The height and radius of the cylinder should be entered by the user and passed to the function.  The function should calcuate the volume and return the result to the main program which will ask the user to if it should calculate the volume of another cylinder.  The user should specify when he/she wants to stop.  The results should be given to the user as shown in the following example.
"The volume of a cylinder with radius 2 units and height 6 units is 75 cubic units (to the nearest cubic unit).  Do you wish to ccalculate the volume of another cylinder ?"
Note: Volume of cylinder = PIr(square)H where r=radius and h=height */
```
Thanks

2. ### lionaneeshActive Member

Joined:
Mar 21, 2010
Messages:
848
224
Trophy Points:
43
Occupation:
Student
Location:
India
Important Points :-

1. Always Initialize Your Variables (Make it a habbit)

C does not initialize your variables for you , It simply puts in some garbage value which can ruin your program logic. [Try running your old code provided only with radius ] ​
2. Always Intend your code

Here's the Modified Code with a 1 line function , I hope it solves your problem

Code:
```#include <stdio.h>
#include <stdlib.h>

#define PI 3.142

[COLOR="Red"]int calculateVolume(int radius , int height)
{
return(PI * radius * radius * height);
}
[/COLOR]
int main ()
{
[COLOR="Red"]     int radius = 0, program = 0 , height = 0 , volume = 0;[/COLOR]
printf ("Enter 2 to start and 8 to quit\t");
scanf ("%d", &program);

while (program !=9)
{
printf ("Press \n 1 for radius \n 3 for height \n 5 to volume \n 9 to quit:\t");
scanf ("%d", &program);

if (program==1)
{
printf ("Enter the value in radius\t");
}

if (program==3) {
printf ("Enter the value for height:\t");
scanf ("%d", &height);   }

if (program==5) {
printf ("\nVOLUME OF CYLINDER WITH RADIUS %d UNITS and HEIGHT %d UNITS IS %d CUBIC UNITS\n", radius, height, volume);
printf ("\n Do you wish to calculate the Volume of another Cylinder?\n");           }
}
system ("pause");
return 0;
}

/* Write a C program that uses a function to calculate the volume of a cylinder.
The height and radius of the cylinder should be entered by the user and passed to the function.  The function should calcuate the volume and return the result to the main program which will ask the user to if it should calculate the volume of another cylinder.  The user should specify when he/she wants to stop.  The results should be given to the user as shown in the following example.
"The volume of a cylinder with radius 2 units and height 6 units is 75 cubic units (to the nearest cubic unit).  Do you wish to ccalculate the volume of another cylinder ?"
Note: Volume of cylinder = PIr(square)H where r=radius and h=height */

```