C Program To Check If Three Points Are On One Straight Line

Given three points (x1, y1), (x2, y2) and (x3, y3), write a C program to check if all the three points fall on one straight line.

Note: (x1, y1), (x2, y2) and (x3, y3) are called co-ordinates of x and y axis.

Related Read:
Basic Arithmetic Operations In C

Expected Output for the Input

User Input:
Enter points (x1, y1)
1
2
Enter points (x2, y2)
3
4
Enter points (x3, y3)
5
6

Output:
All 3 points lie on the same straight line.

Formula To Calculate Slope of 2 points

Slope of points (x1, y1) and (x2, y2) = m;

m = (y2 – y1) / (x2 – x1);

Slope of points (x2, y2) and (x3, y3) = n;

n = (y3 – y2) / (x3 – x2);

x and y axis

Logic To Check If Three Points Are On One Straight Line

We ask the user to enter all 3 points (x1, y1), (x2, y2) and (x3, y3). Next we calculate slope of (x1, y1), (x2, y2) and (x2, y2) (x3, y3). If slopes of both these points are equal, then all these 3 points lie on same straight line.

Video Tutorial: C Program To Check If Three Points Are On Same Straight Line


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

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

Source Code: C Program To Check If Three Points Are On One Straight Line

#include < stdio.h >

int main()
{
    float x1, y1, x2, y2, x3, y3, m, n;

    printf("Enter points (x1, y1)\n");
    scanf("%f%f", &x1, &y1);

    printf("Enter points (x2, y2)\n");
    scanf("%f%f", &x2, &y2);

    printf("Enter points (x3, y3)\n");
    scanf("%f%f", &x3, &y3);

    m = (y2 - y1) / (x2 - x1);
    n = (y3 - y2) / (x3 - x2);

    if( m == n)
    {
        printf("All 3 points lie on the same line\n");
    }
    else
    {
        printf("All 3 points do not lie on the same line\n");
    }

    return 0;
}

Output 1:
Enter points (x1, y1)
-2
2
Enter points (x2, y2)
2
5
Enter points (x3, y3)
6
8
All 3 points lie on the same straight line.

Output 2:
Enter points (x1, y1)
1
2
Enter points (x2, y2)
3
4
Enter points (x3, y3)
-10
15
All 3 points do not lie on the same straight line.

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 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