Program to find the x-intercept and y-intercept of a line passing through the given point in Python

In this tutorial, we will learn how to find the x-intercept and y-intercept of the line passing through the given two-point in the Python programming language? By Bipin Kumar Last updated : January 05, 2024

The x-intercept and y-intercept of a line passing through the given point

The x-intercept is the point where the line cut the x-axis and the y-intercept of the line is a point where the line will cut the y-axis. As we all have learned in the coordinate geometry that how we find the x-intercept and y-intercept of the given line and also in this tutorial we will use the same concept that we have learned in the coordinate geometry. Here, the coordinate of two-points will be given by the user by which the line passes. To solve this problem, the idea is very simple that initially find the equation of the line by using the mathematical formula y = m*x+c where m is the slope of the line and c is constant. After this to know the x-intercept of the line just put the value of y is zero and the corresponding value of x is x-intercept and similarly for y-intercept just put the value of x is zero and the corresponding value of y is y-intercept. Before going to solve this problem, we will the algorithm and try to understand the approach.

Algorithm

Algorithm to solve this problem:

  1. Take the coordinate of the two-point by the user from which the line will pass.
  2. Find the slope of the line by using the formula m = (y2-y1)//(x2-x1).
  3. Now, write the equation of the line by using the mathematical formula y = m*x+c where c is constant.
  4. To find the value of constant c just put the given one point coordinate in the expression of the line i.e y = m*x+c.
  5. Here, to know the x-intercept just put the value of y is zero in the equation of the line.
  6. Also to find the y-intercept just put the value of x is zero in the expression of the line.
  7. Print the value of x-intercept and y-intercept of the line.

Now, we will write the Python program by implementing the above algorithm in a simple way.

Python program to find the x-intercept and y-intercept of a line passing through the given point

a,b,p,q=map(int,input('Enter the coordinates of the points:').split())

m=(q-b)/(p-a)
y=b
x=a
c=y-(m*x)

#to find x-intercept.
y=0
x=(y-c)/m
print('x-intercept of the line:',x)

#to find y-intercept.
x=0
y=(m*x)+c
print('y-intercept of the line:',y)

Output

Enter the coordinates of the points: 5 2 2 7
The x-intercept of the line: 6.2
The y-intercept of the line: 10.333333333333334

To understand the above program, you should have the basic knowledge of the following Python topics:

Python Basic Programs »


Related Programs

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.