C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

C Program: Volume of a Cylinder

C Program: Volume of a Cylinder

Introduction (Cylinder Volume)

A cylinder is a 3D solid shape with two parallel circular bases connected by a curved surface.
Examples in real life include water tanks, cans, and pipes.

The volume of a cylinder represents the total space it encloses.
The formula is:

Volume = π r2 h

where

  • r = radius of the circular base
  • h = height of the cylinder
  • π (pi) ≈ 3.14159

 

C Program: Volume of a Cylinder

C

#include <stdio.h>

#define PI 3.14159   // Define constant PI

 

int main() {

    float radius, height, volume;

 

    // Input radius and height

    printf("Enter the radius of the cylinder: ");

    scanf("%f", &radius);

 

    printf("Enter the height of the cylinder: ");

    scanf("%f", &height);

 

    // Calculate volume

    volume = PI * radius * radius * height;

 

    // Display result

    printf("Volume of Cylinder = %.2f\n", volume);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter the radius of the cylinder: 3
Enter the height of the cylinder: 10
Volume of Cylinder = 282.74

OUTPUT 2 :
Enter the radius of the cylinder: 4.5
Enter the height of the cylinder: 12
Volume of Cylinder = 763.41


Explanation

  1. The program asks for radius and height of the cylinder.
  2. Formula applied: π × r² × h.
  3. PI is defined using #define.
  4. The calculated volume is displayed with two decimal places.