Q:

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

belongs to collection: Python basic programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Find the day of the week for a given date in the p... >>
<< Program to find the execution time of a program in...