C Program To Convert Polar To Cartesian Co-ordinates

Write a C program to receive Polar co-ordinates(r, θ) and convert them into Cartesian co-ordinates(x, y). Ask the user to enter theta(θ) value in degrees.

Related Read:
C Program To Convert Cartesian To Polar Co-ordinates

Formula To Convert Polar To Cartesian Co-ordinates

x = r * cos(θ);

y = r * sin(θ);

where x and y are Cartesian co-ordinates. r and θ are polar co-ordinates.

Formula To Convert Degree To Radian

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

Source Code: Convert Polar To Cartesian Co-ordinates: C Program

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

int main()
{
    float x, y, r, theta;
    const float PI = 3.141592;

    printf("Enter Polar Co-ordinates(r, theta)\n");
    scanf("%f%f", &r, &theta);

    /* Convert angle from Degree To Radian */    theta = theta * (PI / 180.0); 

    x = r * cos(theta);
    y = r * sin(theta);

    printf("Cartesian Co-ordinates (x, y) = (%f, %f)\n", x, y);

    return 0;
}

Output:
Enter Polar Co-ordinates(r, theta)
5.01
53.131
Cartesian Co-ordinates (x, y) = (3.005938, 4.008047)

Convert Polar To Cartesian Co-ordinates: C Program


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

YouTube Link: https://www.youtube.com/watch?v=XCz3CDUsC9o [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

C Program To Convert Cartesian To Polar Co-ordinates

Write a C program to receive Cartesian co-ordinates(x, y) of a point and convert them into Polar co-ordinates(r, θ).

Related Read:
C Program To Convert Polar To Cartesian Co-ordinates

Formula To Convert Cartesian To Polar Co-ordinates

r = sqrt(x*x + y*y);
OR
r = sqrt( pow(x, 2) + pow(y, 2) );

theta(θ) = atan(y/x);

where x and y are Cartesian co-ordinates.
atan() is the method in math.h library for calculating tan inverse.

Formula To Convert Radian To Degree

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

Source Code: Convert Cartesian To Polar Co-ordinates: C Program

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

int main()
{
    float x, y, r, theta;
    const float PI = 3.141592;

    printf("Enter Cartesian Co-ordinates(x, y)\n");
    scanf("%f%f", &x, &y);

    r     = sqrt(x*x + y*y);
    theta = atan(y/x);       // Radian

    theta = theta * (180.0 / PI); //Radian To Degree Conversion

    printf("Polar Co-ordinates: (r, theta) = (%f, %f)\n", r, theta);

    return 0;
}

Output:
Enter Cartesian Co-ordinates(x, y)
3
4
Polar Co-ordinates: (r, theta) = (5.000000, 53.130112)

Convert Cartesian To Polar Co-ordinates: C Program


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

YouTube Link: https://www.youtube.com/watch?v=3ebGfJ_GOD0 [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

Print All Trigonometric Ratios: C Program

If value of an angle is input through the keyboard, write a c program to print all its Trigonometric Ratios.

Formula To Convert Degree To Radian

radian = degree * (PI / 180.0);
where PI = 3.14159; (constant value)

Logic to Print all Trigonometric Ratios

First we ask the user to input angle of a Triangle in degrees. Next we convert angel from degrees to radian.

Next we include math.h header file and use methods present in it to get:
sin()
cos()
tan()

To get cosec() = 1 / sin();
To get sec() = 1 / cos();
To get cot() = 1 / tan();

tan() value can also be obtained using sin() / cos();
cot() value can also be obtained using cos() / sin();

Source Code: Print All Trigonometric Ratios: C Program

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

int main()
{
    float degree, radian;
    const float PI = 3.14159;

    printf("Enter angle in degree\n");
    scanf("%f", °ree);

    radian = degree * (PI / 180.0);

    printf("Sin(%f) = %f\n", degree, sin(radian));
    printf("Cos(%f) = %f\n", degree, cos(radian));
    printf("Tan(%f) = %f\n", degree, tan(radian));
    printf("Cosec(%f) = %f\n", degree, 1/sin(radian));
    printf("Sec(%f) = %f\n", degree, 1/cos(radian));
    printf("Cot(%f) = %f\n", degree, 1/tan(radian));

    return 0;
}

Output 1
Enter angle in degree
30
Sin(30.000000) = 0.500000
Cos(30.000000) = 0.866026
Tan(30.000000) = 0.577350
Cosec(30.000000) = 2.000001
Sec(30.000000) = 1.154700
Cot(30.000000) = 1.732052

Output 2
Enter angle in degree
45
Sin(45.000000) = 0.707106
Cos(45.000000) = 0.707107
Tan(45.000000) = 0.999999
Cosec(45.000000) = 1.414214
Sec(45.000000) = 1.414213
Cot(45.000000) = 1.000001

Print All Trigonometric Ratios: C Program


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

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


Tan and Cot

 
    printf("Tan(%f) = %f\n", degree, sin(radian)/cos(radian));
    printf("Cot(%f) = %f\n", degree, cos(radian)/sin(radian));

OR

 
    printf("Tan(%f) = %f\n", degree, tan(radian));
    printf("Cot(%f) = %f\n", degree, 1/tan(radian));

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 Area of a Circle using math.h library: C

We can calculate area of circle if we know the value of its radius. So using that, we shall write the logic in c program to calculate the area of a circle, and we will us math.h library functions in this program.

Related Read:
Basic Arithmetic Operations In C

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

int main()
{
    float radius;

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

    printf("Area of circle is %f \n", (M_PI * pow(radius, 2)));

    return 0;
}

Output:
Enter radius of circle
2.0
Area of circle is 12.566371

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Calculate Area of a Circle using math.h library: C


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

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


math.h library file has a constant M_PI which has the value of PI. Since it’s a constant variable we can not change its value in our program. We’re also using pow() method/function present in math.h header file.

Formula for calculating Area of a circle


area of circle

Formula for calculating Area of a circle is PI * radius * radius. We take value of radius from user and using above formula calculate area of the circle and output result to the console window. Since we’re using math.h library file we use M_PI and pow() methods for the expression.

i.e., area = M_PI * pow(radius, 2);

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