Slope of the line parallel to the line with the given slope
Last Updated :
08 Dec, 2022
Given an integer m which is the slope of a line, the task is to find the slope of the line which is parallel to the given line.
Examples:
Input: m = 2
Output: 2
Input: m = -3
Output: -3
Approach:
Let P and Q be two parallel lines with equations y = m1x + b1, and y = m2x + b2 respectively. Here m1 and m2 are the slopes of the lines respectively. Now as the lines are parallel, they don’t have any intersecting point, and hence there will be no system of solutions for the lines. So, let us try to solve the equations,
For y, m1x + b1 = m2x + b2
m1x – m2x = b2 – b1
x(m1 – m2) = b2 – b1
The only way there can be no solution for x is for m1 – m2 to be equal to zero.
m1 – m2 = 0
This gives us m1 = m2 and the slopes are equal.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double getSlope( double m)
{
return m;
}
int main()
{
double m = 2;
cout << getSlope(m);
return 0;
}
|
Java
import java.io.*;
public class GfG {
static double getSlope( double m) { return m; }
public static void main(String[] args)
{
double m = 2 ;
System.out.println(getSlope(m));
}
}
|
Python3
def getSlope(m):
return m;
m = 2 ;
print (getSlope(m));
|
C#
class GFG
{
static double getSlope( double m)
{
return m;
}
static void Main()
{
double m = 2;
System.Console.Write(getSlope(m));
}
}
|
PHP
<?php
function getSlope( $m )
{
return $m ;
}
$m = 2;
echo getSlope( $m );
?>
|
Javascript
<script>
function getSlope(m)
{
return m;
}
var m = 2;
document.write(getSlope(m));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...