C Programming - Finding Volume of a Sphere Source Code




  1. Modify the flowchart to introduce a checking mechanism. The modified flowchart
    takes one new input as height, and uses it to calculate the volume (v = π·r2·h) of a cylinder with radius. It is assumed that this cylinder is tall, and therefore height has to be bigger than radius. In addition, it is required that radius cannot be smaller than 1. With the modification, the new flowchart prints out the volume only if the inputs satisfy the conditions. Otherwise, it prints out error messages as shown in the following examples:
    1. A case when radius is less than 1: Enter radius: 0.8
      Radius cannot be smaller than 1.
    2. A case when height is not bigger than radius: Enter radius: 3
      Enter height: 2
      Height has to be bigger than radius.
    3. A normal case with valid inputs: Enter radius: 2
      Enter height: 3
      The volume is 37.70. 

#include <stdio.h>

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define PI 3.141592

int main()

{

float r, h;

printf("Enter radius and height: ");

scanf("%f%f", &r, &h);

float volume = PI*pow(r, 2)*h;

printf("The volume is : %.2f ", volume);

getch();

return 0;

}

Comments