# Java Program to Check if Line Passes Through the Origin

In the previous article, we have seen Java Program to Find Line Passing Through 2 Points

In this article we will discuss about how to find line passing through origin or not using Java programming language.

## Java Program to Check if Line Passes Through the Origin

Before jumping into the program directly, let’s first know how can find line passing through origin or not

Explanation:

Equation of a line passing through two points (a1, b1) and (a2, b2) is given by

b-b1 = ((b2-b1) / (a2-a1))(a-a1) + z

If line is also passing through origin, then z=0, so equation of line becomes

b-b1 = ((b2-b1) / (a2-a1))(a-a1)

Keeping a=0, b=0 in the above equation we get,

a1(b2-b1) = b1(a2-a1)

So above equation must be satisfied if any line passing through two coordinates (a1, b1) and (a2, b2) also passes through origin (0, 0).

### Method-1: Java Program to Check if Line Passes Through the Origin By Using Static Value

Approach :

• Initialize and declare 2 point .
• Find the Boolean value of x1.a * (x2.b - x1.b) == x1.b * (x2.a - x1.a)
• If value is true, then line is passing through origin .
• If not, then line is not passing through origin .

Program :

class Main
{
// Driver method
public static void main(String args[])
{
//points are declared
IntsPoint x1 = new IntsPoint(1,28);
IntsPoint x2 = new IntsPoint(2,56);
boolean a= x1.a * (x2.b - x1.b) == x1.b * (x2.a - x1.a);
if(a==true)
System.out.println("line is passing through origin ");
else
System.out.println("line is  not passing through origin ");
}

static class IntsPoint
{
int a,b;
public IntsPoint(int a, int b)
{
this.a = a;
this.b = b;
}
}

}

Output:

line is passing through origin

### Method-2: Java Program to Check if Line Passes Through the Origin By User Input Value

Approach :

• Take input of values for 2 points.
• Find the Boolean value of x1.a * (x2.b - x1.b) == x1.b * (x2.a - x1.a)
• If value is true, then line is passing through origin .
• If not, then line is not passing through origin .

Program :

import java.util.*;
class Main
{
// Driver method
public static void main(String args[])
{

//Scanner class object created
Scanner s = new Scanner(System.in);
int l,m ;
//taking input of point-1 values
System.out.println("Enter values of point 1:");
l=s.nextInt();
m=s.nextInt();
//taking input of point-2 values
IntsPoint x1 = new IntsPoint(l, m);
System.out.println("Enter values of point 2 :");
l=s.nextInt();
m=s.nextInt();
IntsPoint x2 = new IntsPoint(l, m);
boolean a= x1.a * (x2.b - x1.b) == x1.b * (x2.a - x1.a);
if(a==true)
System.out.println("line is passing through origin ");
else
System.out.println("line is  not passing through origin ");
}
static class IntsPoint
{
int a,b;
public IntsPoint(int a, int b)
{
this.a = a;
this.b = b;
}
}

}


Output:

Enter values of point 1:
1 28
Enter values of point 2 :
2 56
line is passing through origin

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: