C Program To Calculate Volume of Sphere

Given the value for radius of a Sphere, write a C program to calculate volume of the Sphere.

Formula To Calculate Volume of Sphere

volume = ( 4 * PI * radius3 ) / 3.0;

OR

volume = ( 1.333 * PI * radius3 );

volume of sphere

where PI is approximately equal to 3.14;

Related Read:
Basic Arithmetic Operations In C

Expected Output for the Input

User Input:
Enter Radius of the Sphere
5

Output:
Volume of Sphere is 523.333374

Logic To Calculate Volume of Sphere

We ask the user to enter value for radius of the Sphere. If user enters 5 inche. Then we use the formula to calculate the Volume of Sphere:

volume = ( 4 * PI * radius3 ) / 3.0;

User input:
radius = 5 inches;

PI value is approximately equal to 3.14;

volume = ( 4 * PI * radius3 ) / 3.0
volume = ( 4 * 3.24 * (5 inches)3) / 3.0
volume = ( ‭12.56‬ * 125 inches3 ) / 3.0
volume = ( ‭12.56‬ * 125 inches3 ) / 3.0
volume = ‭1,570‬ inches3 / 3.0
volume = ‭523.33 inches3

Video Tutorial: C Program To Calculate Volume of Sphere


[youtube https://www.youtube.com/watch?v=rpQzrFSWj6c]

YouTube Link: https://www.youtube.com/watch?v=rpQzrFSWj6c [Watch the Video In Full Screen.]

Source Code: C Program To Calculate Volume of Sphere

#include<stdio.h>

int main()
{
    const float PI = 3.14;
          float r, volume;

    printf("Enter Radius of the Sphere\n");
    scanf("%f", &r);

    volume = ( 4 * PI * r * r * r ) / 3.0;

    printf("Volume of Sphere is %f\n", volume);

    return 0;
}

Output 1:
Enter Radius of the Sphere
3
Volume of Sphere is 113.040001

Output 2:
Enter Radius of the Sphere
14
Volume of Sphere is 11488.213867

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Calculate Circumference of Circle

Given the radius of a Circle, write a C program to calculate circumference of the Circle.

Formula To Calculate Circumference of Circle

circumference = 2 * PI * radius;
Where PI is approximately equal to 3.14

Related Read:
Basic Arithmetic Operations In C

Expected Output for the Input

User Input:
Enter Radius of the Circle
5

Output:
Circumference of the Circle is 31.400002

Video Tutorial: C Program To Calculate Circumference of Circle


[youtube https://www.youtube.com/watch?v=o2r2bDOuCX4]

YouTube Link: https://www.youtube.com/watch?v=o2r2bDOuCX4 [Watch the Video In Full Screen.]

Source Code: C Program To Calculate Circumference of Circle

#include<stdio.h>

int main()
{
    const float PI = 3.14;
          float r, c;

    printf("Enter Radius of the Circle\n");
    scanf("%f", &r);

    c = 2 * PI * r;

    printf("Circumference of the Circle is %f\n", c);

    return 0;
}

Output 1:
Enter Radius of the Circle
10
Circumference of the Circle is 62.800003

Output 2:
Enter Radius of the Circle
41
Circumference of the Circle is 257.480011

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Calculate Volume of Cylinder

Given the values for radius and height of a cylinder, write a C program to calculate volume of the Cylinder.

Formula To Calculate Volume of Cylinder

volume = Base_Area x height;

Base of the Cylinder is a Circle. Area of Circle is PI x Radius2

So lets replace Base_Area with Area of Circle: PI x Radius2.

Therefore, volume = (PI x Radius2) x height;

volume of Cylinder

where PI is 3.14159265359;

Related Read:
Basic Arithmetic Operations In C

Expected Output for the Input

User Input:
Enter Radius and Height of the Cylinder
2
5

Output:
Volume of Cylinder is 62.831856

Logic To Calculate Volume of Cylinder

We ask the user to enter values for radius and height of the Cylinder. If user enters 2m and 5m. Then we use the formula to calculate the Volume of Cylinder:

volume = (PI x Radius2) x height;

User input:
radius = 2m;
height = 5m;

PI value is 3.14159265359;

volume = PI x Radius2 x height;
volume = 3.14 x (2m)2 x (5m);
volume = 3.14 x 4(m)2 x 5(m);
volume = 3.14 x 20(m)3;
volume = ‭62.8‬ m3;

So volume of cylinder is 62.8 cubic meter.

Video Tutorial: C Program To Calculate Volume of Cylinder


[youtube https://www.youtube.com/watch?v=WZMPJEGs0cI]

YouTube Link: https://www.youtube.com/watch?v=WZMPJEGs0cI [Watch the Video In Full Screen.]

Source Code: C Program To Calculate Volume of Cylinder

#include<stdio.h>

int main()
{
    const float PI = 3.14159265359;
          float r, h, volume;

    printf("Enter Radius and Height of the Cylinder\n");
    scanf("%f%f", &r, &h);

    volume = PI * r * r * h;

    printf("Volume of Cylinder is %f\n", volume);

    return 0;
}

Output 1:
Enter Radius and Height of the Cylinder
2
5
Volume of Cylinder is 62.831856

Output 2:
Enter Radius and Height of the Cylinder
2
3
Volume of Cylinder is 37.699112

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Calculate Distance in Nautical Miles: C Program

Write a C program to receive values of latitude(L1, L2) and longitude(G1, G2), in degrees, of two places on the earth and output the distance (D) between them in nautical miles.

Formula For Distance in Nautical Miles

D = 3963 * acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 – lon1) );

where lat1, lat2 are latitudes. lon1, lon2 are longitude.

Formula To Convert Degree To Radian

radian = degree * (PI/180.0);
where PI is 3.141592

Logic To Find Distance In Nautical Miles

User input the values of latitude(lat1, lat2) and longitude(lon1, lon2 ), in degrees. We convert it to radians. Next we use the formula below:
D = 3963 * acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 – lon1) );
and out put the result on to the console.

Source Code: Calculate Distance in Nautical Miles: C Program

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

int main()
{
    float lat1, lat2, lon1, lon2, D;
    const float PI = 3.141592;

    printf("Enter latitude(L1, L2)\n");
    scanf("%f%f", &lat1, &lat2);

    printf("Enter longitude(G1, G2)\n");
    scanf("%f%f", &lon1, &lon2);
  
    /* Convert Degrees To Radian */    lat1 = lat1 * ( PI / 180.0 );
    lat2 = lat2 * ( PI / 180.0 );
    lon1 = lon1 * ( PI / 180.0 );
    lon2 = lon2 * ( PI / 180.0 );

    D = 3963 * acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1) );

    printf("Distance in nautical miles is %f\n", D);

    return 0;
}

Output:
Enter latitude(L1, L2)
52.3
43.2
Enter longitude(G1, G2)
12.3
15.6
Distance in nautical miles is 647.682434

Calculate Distance in Nautical Miles: C Program


[youtube https://www.youtube.com/watch?v=d88pWBloIOE]

YouTube Link: https://www.youtube.com/watch?v=d88pWBloIOE [Watch the Video In Full Screen.]


For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Find Area of a Triangle Using Its Sides: C Program

If lengths of three sides of a Triangle are input through the keyboard, write a program to find the area of the Triangle.

Related Read:
Basic Arithmetic Operations In C
Triangle Valid or Not based On Sides: C Program

Note:A Triangle is valid if the sum of 2 sides of the Triangle is greater than the largest of the 3 sides.

Important: In this program we assume that the user has entered valid lengths of the Triangle.

Formula To Calculate Area and Semi-perimeter of a Triangle

sp = (a + b + c) / 2.0;
area = sqrt( sp * (sp-a) * (sp-b) * (sp-c) );

where a, b and c are lengths of sides of the Triangle.
sp – Semi-perimeter.

Note: In geometry, above formula to calculate area of Triangle is called Heron’s formula (sometimes called Hero’s formula).

Find Area of a Triangle Using Its Sides: C Program


[youtube https://www.youtube.com/watch?v=whGefMyrmVM]

YouTube Link: https://www.youtube.com/watch?v=whGefMyrmVM [Watch the Video In Full Screen.]


Find Area of a Triangle Using Its Sides: C Program

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

int main()
{
    float a, b, c, sp, area;

    printf("Enter values of a, b and c\n");
    scanf("%f%f%f", &a, &b, &c);

    sp = (a+b+c)/2.0;

    area = sqrt(sp*(sp-a)*(sp-b)*(sp-c));

    printf("Area of triangle is %f\n", area);

    return 0;
}

Output:
Enter values of a, b and c
4
5
6
Area of triangle is 9.921567

Note: While calculating Semi-perimeter, make sure to divide by 2.0 and not by 2. Because division by integer number returns only the integer part of the result. So for certain inputs the result might be wrong. So always use the floating/double value while dividing in C Programs.

Note: Since we are using sqrt() method, we need to include math.h library file (header file) without fail.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert