C Program To Calculate Surface Area of Cylinder

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

Formula To Calculate Surface Area of Cylinder

Surface_Area = (2 * PI * radius * height) + (2 * PI * radius * radius);
where PI is approximately equal to 3.14

surface area of Cylinder

Related Read:
Basic Arithmetic Operations In C
Area of Rectangle: C Program
Calculate Area of a Circle without using math.h library: C
C Program To Calculate Circumference of Circle

Logic of Surface Area of Cylinder Formula


A solid Cylinder has 2 circles. One at the top and one at the bottom. Area of a Circle is PI * radius2. Since there are 2 Circles in the Cylinder, we multiply it by 2.

i.e., 2 (PI * radius2)

Next lets cut the cylinder at one end and open it. It forms a rectangle. Now height of the rectangle and height of the Cylinder are same. Width of the rectangle is equal to circumference of the circle, since top and bottom of the Cylinder are circles.

So, area of rectangle = (2 * PI * radius) * height

Therefore, Surface Area of Cylinder is:

SA = ( (2 * PI * radius) * height ) + ( 2 (PI * radius2) );

Expected Output for the Input

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

Output:
Surface Area of Cylinder is 87.920006

Video Tutorial: C Program To Calculate Surface Area of Cylinder


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

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

Source Code: C Program To Calculate Surface Area of Cylinder

#include<stdio.h>

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

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

    Area = (2 * PI * r * h) + (2 * PI * r * r);

    printf("Surface Area of Cylinder is %f\n", Area);

    return 0;
}

Output 1:
Enter Radius and Height of the Cylinder
5
10
Surface Area of Cylinder is 471.000031

Output 2:
Enter Radius and Height of the Cylinder
2
13
Surface Area of Cylinder is 188.400009

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

C Program To Check If Sum of Square of Sine and Cosine of an Angle is 1

Write a C program to receive value of an angle in degrees and check whether sum of squares of sine and cosine of this angle is equal to 1.

Related Read:
Basic Arithmetic Operations In C
Calculate Power of a Number using pow(): C Program

Formula To Convert Angle From Degree To Radian

radian = degree x ( PI / 180.0)
where PI is 3.14159265359

Note: In C programming Language, we’ve a builtin constant M_PI which has value of PI.

Expected Output for the Input

User Input:
Enter angle in Degree
45

Output:
Sum of square of sin(45.00) and cos(45.00) is 1

Logic To Check If Sum of Square of Sine and Cosine of an Angle is 1

User enters the angle in degree. We convert angle from degree to radian.
radian = degree x ( PI / 180.0)
Next we use pow() method present in math.h library to sqaure the values of sin(angle) and cos(angle). We add the square of sin(angle) and cos(angle) and store it inside the variable sum, and display appropriate message to the user.

Video Tutorial: C Program To Check If Sum of Square of Sine and Cosine of an Angle is 1


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

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

Source Code: C Program To Check If Sum of Square of Sine and Cosine of an Angle is 1

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

int main()
{
    float sum = 0, angle, temp;

    printf("Enter angle in Degree\n");
    scanf("%f", &angle);

    temp   = angle;

    angle  = angle * ( M_PI / 180.0 );

    sum    = (  pow(sin(angle), 2) + pow(cos(angle), 2)  );

    if(sum == 1)
    {
        printf("Sum of square of sin(%0.2f) and cos(%0.2f) is 1\n", temp, temp);
    }
    else
    {
        printf("Sum of square of sin(%0.2f) and cos(%0.2f) is not 1\n", temp, temp);
    }

    return 0;
}

Output 1:
Enter angle in Degree
30
Sum of square of sin(30.00) and cos(30.00) is 1

Output 2:
Enter angle in Degree
45
Sum of square of sin(45.00) and cos(45.00) is 1

Output 3:
Enter angle in Degree
60
Sum of square of sin(60.00) and cos(60.00) is 1

Output 4:
Enter angle in Degree
75
Sum of square of sin(75.00) and cos(75.00) is 1

Output 5:
Enter angle in Degree
90
Sum of square of sin(90.00) and cos(90.00) is 1

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

Draw Arcs/Circle with Canvas: HTML5

Today lets learn how to use arcs and paths to draw circle.

Arcs are curves that are portions of a circle. A full circle is a 360 degree arc!
A Path is a collection of some lines(or shapes) that once defined can be either filled or stroked.

index.html and myStyle.css files are kept same as illustrated in previous video tutorial: Canvas State: HTML5

Syntax
arc(x, y, r, sA, eA, TF);

arc method takes 6 parameters. x and y are the x-axis and y-axis, which is the center of the circle.
i.e., The x and y parameters determine where the center of the circle will be located on your canvas.

r is the radius of the circle, in pixels.
i.e., the distance of the curve from the center of the circle.

sA is start angle of the circle.
eA is the end angle of the circle.

TF is the direction – which can take 2 values i.e., True or False.

True: negative degrees, counter-clockwise direction.
False: positive degrees, clockwise direction.

Javascript file
myScript.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
window.onload = canvas;
 
function canvas()
{
var myCanvas = document.getElementById("myCanvas");
 
if( myCanvas && myCanvas.getContext("2d") ) 
{
var context         = myCanvas.getContext("2d");
 
context.lineWidth= 6;
context.beginPath();
 
var degree = 270;
var radian = ( Math.PI / 180 ) * degree;
 
context.arc(100, 100, 50, 0, radian, false);
context.strokeStyle= "red";
context.fillStyle= "blue";
context.stroke();
context.fill();
context.closePath();
 
}
}

Degree to Radian

radians-and-degrees

We convert degrees to radians using a simple mathematical formula:
Radians = (PI / 180 ) * degree;

Draw Arcs/Circle with Canvas: HTML5


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

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



Note:
1. By default, direction of drawing the arc is clockwise/positive degrees(i.e., the value of last parameter is false).
2. PI value is 180.
3. 2 * PI is 360!
4. arc method should be passed with radian values and not degrees.