Home » Python » Python programs

# 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?**

Submitted by Bipin Kumar, on November 09, 2019

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

- Take the coordinate of the two-point by the user from which the line will pass.
- Find the slope of the line by using the formula
**m = (y2-y1)//(x2-x1)**. - Now, write the equation of the line by using the mathematical formula
**y = m*x+c**where**c**is constant. - 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**. - Here, to know the
**x-intercept**just put the value of**y**is zero in the equation of the line. - Also to find the
**y-intercept**just put the value of**x**is zero in the expression of the line. - 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.

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

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.