This blog is under construction

Monday 8 July 2013

C program to calculate volume of a cylinder and sphere

Write a C program to calculate volume of a cylinder and sphere.


  #include <stdio.h>
  int main() {
        float svol, cvol, sradius, cradius, height, vsa, sa;

        /* get the input for sphere */
        printf("Enter the radius of the sphere:");
        scanf("%f", &sradius);

        /* get the input for cylinder */
        printf("Enter the volume and radius of the cylinder:");
        scanf("%f%f", &cradius, &height);

        /* volume and surface area calculation for cylinder & sphere */
        svol = (4 * 3.14 * sradius * sradius * sradius) / 3;
        sa = 4 * 3.14 * sradius * sradius;
        cvol = 3.14 * cradius * cradius * height;
        vsa = 2 * 3.14 * cradius * (cradius + height);

        /* print the results */
        printf("Volume of the cylinder: %.2f\n", cvol);
        printf("Surface area of cylinder : %.2f\n", vsa);
        printf("Volume of the sphere: %.2f\n", svol);
        printf("Surface area of sphere: %.2f\n", sa);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the radius of the sphere:10
  Enter the volume and radius of the cylinder:10  10

  Volume of the cylinder: 3140.00
  Surface area of cylinder : 1256.00
  Volume of the sphere: 4186.67
  Surface area of sphere: 1256.00






See Also:

No comments:

Post a Comment