C Program To Convert Degree To Radian

Lets write a C program to convert angle from Degree to Radian.

Formula To Convert Degree To Radian

radian = degree x (M_PI / 180.0);

where M_PI is a constant present in header file / library file math.h and its approximately equal to 3.14;

Note: Trigonometric Ratios like Sin, Cos, Tan etc take radian value as input and not degrees. So its important to learn how to convert angles from degree to radian.

Related Read:
Keywords, Constants, Variables: C
Basic Arithmetic Operations In C
Print All Trigonometric Ratios: C Program
C Program To Convert Radian To Degree

Video Tutorial: C Program To Convert Degree To Radian


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

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

Source Code: C Program To Convert Degree To Radian

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

int main()
{
    float degree, radian;

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

    radian = degree * ( M_PI / 180.0 );

    printf("Angle in Radian is %f\n", radian);

    return 0;
}

Output 1:
Enter angle in Degrees
90
Angle in Radian is 1.570796

Output 2:
Enter angle in Degrees
360
Angle in Radian is 6.283185

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 Surface Area of Cone

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

Formula To Calculate Surface Area of Cone

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

Note: sqrt() is a builtin method present in math.h header file.

surface area of cone

Related Read:
Basic Arithmetic Operations In C

Expected Output for the Input

User Input:
Enter radius and height of the cone
2
5

Output:
Surface Area of Cone is 46.378838

Video Tutorial: C Program To Calculate Surface Area of Cone


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

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

Source Code: C Program To Calculate Surface Area of Cone

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

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

    printf("Enter radius and height of the cone\n");
    scanf("%f%f", &r, &h);

    s_area = PI * r * ( r + sqrt(h * h + r * r) );

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

    return 0;
}

Output 1:
Enter radius and height of the cone
8
18
Surface Area of Cone is 695.766663

Output 2:
Enter radius and height of the cone
5
14
Surface Area of Cone is 311.897278

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

C Program To Check If Point Lies Inside, Outside or On The Circle

Given the coordinates(cx, cy) of center of a circle and its radius, write a C program that will determine whether a point(x, y) lies inside the Circle, on the Circle or outside the Circle. (Hint: Use sqrt() and pow() functions)

Note:
Center Point – (cx, cy);
We need to find the position of point (x, y);

Logic To Check whether Point Lies Inside, Outside or On The Circle

First we need to calculate the distance of the point(x, y) from the center(cx, cy) of the circle. Next we need to compare the distance with the radius of the Circle.

Conditions To Determine The Position of the Point(x, y)
1. Distance is greater than radius: point is outside the Circle.
2. Distance is less than radius : point is inside the Circle.
3. Distance is equal to the radius: point is on the Circle.

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

Expected Output for the Input

User Input:
Enter the center point(cx, cy)
0
0
Enter radius of the circle
6
Enter the point(x, y) to check its position
3
3

Output:
Point (3.00, 3.00) is inside the Circle

Formula To Calculate Distance from point(x, y) To Center Point (cx, cy)

distance = sqrt( pow( (x – cx), 2 ) + pow( (y – cy), 2) );

Note: sqrt() and pow() are builtin method present in library file or header file math.h

Video Tutorial: C Program To Check If Point Lies Inside, Outside or On The Circle


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

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

Source Code: C Program To Check If Point Lies Inside, Outside or On The Circle

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

int main()
{
    float cx, cy, radius, x, y, distance;

    printf("Enter the center point(cx, cy)\n");
    scanf("%f%f", &cx, &cy);

    printf("Enter radius of the circle\n");
    scanf("%f", &radius);

    printf("Enter the point(x, y) to check its position\n");
    scanf("%f%f", &x, &y);

    distance = sqrt( pow( (x - cx), 2 ) + pow( (y - cy), 2 ) );

    if(distance < radius)
    {
        printf("Point (%0.2f, %0.2f) is inside the Circle\n", x, y);
    }
    else if(distance > radius)
    {
        printf("Point (%0.2f, %0.2f) is outside the Circle\n", x, y);
    }
    else
    {
        printf("Point (%0.2f, %0.2f) is on the Circle\n", x, y);
    }

    return 0;
}

Output 1:
Enter the center point(cx, cy)
0
0
Enter radius of the circle
6
Enter the point(x, y) to check its position
2
2
Point (2.00, 2.00) is inside the Circle

Output 2:
Enter the center point(cx, cy)
0
0
Enter radius of the circle
6
Enter the point(x, y) to check its position
12
6
Point (12.00, 6.00) is outside the Circle

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 Find First and Last Digit of a Number

Write a C program to find first and last digit of the user input number, without using looping.

Related Read:
Basic Arithmetic Operations In C

Source Code: C Program to Find First and Last Digit of a Number

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

int main()
{
    int num, first, last, count;

    printf("Enter an integer number\n");
    scanf("%d", &num);

    count = log10(num);

    first = num / pow(10, count);
    last  = num % 10;

    printf("First Digit = %d\nLast Digit = %d\n", first, last);

    return 0;
}

Output 1:
Enter an integer number
123
First Digit = 1
Last Digit = 3

Output 2:
Enter an integer number
123456
First Digit = 1
Last Digit = 6

Output 3:
Enter an integer number
15937
First Digit = 1
Last Digit = 7

Output 4:
Enter an integer number
5986
First Digit = 5
Last Digit = 6

Output 5:
Enter an integer number
964801
First Digit = 9
Last Digit = 1

C Program to Find First and Last Digit of a Number


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

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


Logic To Find First and Last Digits of a Number

If user enters a number 123. i.e., num = 123; Then num % 10 would give the last digit of the number i.e., 3. To get first digit, we need to know the number of digits present in the number. To get that we make use of a built-in method called log10(). log10() returns the number of digits present in the number minus 1. That is because, it starts the count from 0. So if num = 123, log10(num) will return 2 and not 3. We store the number of digits inside variable count.

Now we use pow() method and calculate 10 to the power of count(value present in variable count) i.e., pow(10, count); We divide the user entered number by pow(10, count); to get the first digit of the number.
i.e., First_Digit = num / pow(10, count);

Note: Both pow() and log10() are built-in methods present in header file math.h So we include that library file at the beginning of our C program source code.

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