C Program to check if the points are parallel to X axis or Y axis

Given n number of points we have to check that whether the point is parallel to x-axis or y-axis or no axis according to a graph. A graph is a figure which is used to show a relationship between two variables each measured along with axis at right angles. Parallel are the same lines which are having same distance at all points, like railway tracks are parallel to each other.

So, we have to find whether the points are parallel to x-axis or y-axis means the distance between the coordinates and the axis are same at all points.

What is an axis

The graph is measured along with two axis' x-axis and y-axis, both of the axis start from a point value 0 and extend according to their particular variable value. Both of the axis combined form a figure like right angle triangle.

Let's understand it clearly with help of an simple diagrammatic representation −

X-axis Y-axis 0 Parallel to Y-axis Parallel to X-axis

The approach used below is as follows

  • Firstly we take the coordinates of a graph in way of (x, y) coordinates.
  • Then check whether they are parallel to which axis.
  • If all y coordinates are same, then the graph is parallel to x-axis.
  • Else if the x coordinates are same, then the graph is parallel to y-axis.
  • Else the graph is not parallel to either of the axis.

Syntax

void parallel(int n, int a[][2]);

Algorithm

Start
In function void parallel (int n, int a[][2])
   Step 1-> Declare and initialize i and j
   Step 2-> Declare bool x = true, y = true
   Step 3-> Loop For i = 0 and i < n - 1 and i++
   Loop For j = 0 and j < 2 and j++
      If a[i][0] != a[i + 1][0] then,
         Set x as false
      If a[i][1] != a[i + 1][1] then,
         Set y as false
      End loop
   End loop
   Step 4-> If x then,
      Print "parallel to X Axis<br>"
   Step 5-> Else if y
      Print "parallel to Y Axis<br>"
   Step 6-> Else
      Print "parallel to X and Y Axis<br>"
In function int main()
   Step 1-> Declare an array "a[][2]"
   Step 2-> Declare and Initialize n as sizeof(a) / sizeof(a[0])
   Step 3-> Call function parallel(n, a)

Example

#include <stdio.h>
#include <stdbool.h>

// To check the line is parallel or not
void parallel(int n, int a[][2]) {
    int i, j;
    bool x = true, y = true;
    
    // checking for parallel to X and Y axis condition
    for (i = 0; i < n - 1; i++) {
        if (a[i][0] != a[i + 1][0])
            x = false;
        if (a[i][1] != a[i + 1][1])
            y = false;
    }
    
    // To display the output
    if (x)
        printf("parallel to Y Axis<br>");
    else if (y)
        printf("parallel to X Axis<br>");
    else
        printf("not parallel to any axis<br>");
}

int main() {
    int a[][2] = { { 2, 1 },
                   { 3, 1 },
                   { 4, 1 },
                   { 0, 1 } };
    int n = sizeof(a) / sizeof(a[0]);
    
    printf("Points: ");
    for (int i = 0; i < n; i++) {
        printf("(%d,%d) ", a[i][0], a[i][1]);
    }
    printf("<br>");
    
    parallel(n, a);
    return 0;
}

Output

Points: (2,1) (3,1) (4,1) (0,1) 
parallel to X Axis

Example 2: Points Parallel to Y-axis

#include <stdio.h>
#include <stdbool.h>

void parallel(int n, int a[][2]) {
    int i;
    bool x = true, y = true;
    
    for (i = 0; i < n - 1; i++) {
        if (a[i][0] != a[i + 1][0])
            x = false;
        if (a[i][1] != a[i + 1][1])
            y = false;
    }
    
    if (x)
        printf("parallel to Y Axis<br>");
    else if (y)
        printf("parallel to X Axis<br>");
    else
        printf("not parallel to any axis<br>");
}

int main() {
    int a[][2] = { { 5, 1 },
                   { 5, 3 },
                   { 5, 7 },
                   { 5, 9 } };
    int n = sizeof(a) / sizeof(a[0]);
    
    printf("Points: ");
    for (int i = 0; i < n; i++) {
        printf("(%d,%d) ", a[i][0], a[i][1]);
    }
    printf("<br>");
    
    parallel(n, a);
    return 0;
}
Points: (5,1) (5,3) (5,7) (5,9) 
parallel to Y Axis

Key Points

  • Points are parallel to X-axis when all y-coordinates are the same.
  • Points are parallel to Y-axis when all x-coordinates are the same.
  • The inner loop in the original algorithm is unnecessary since we only need to compare coordinates.
  • Include <stdbool.h> for the bool data type.

Conclusion

This program efficiently checks if given points form a line parallel to either axis by comparing coordinates. The logic is simple: same y-coordinates mean parallel to X-axis, same x-coordinates mean parallel to Y-axis.

Updated on: 2026-03-15T12:19:21+05:30

597 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements