Slope of perpendicular to line
Last Updated :
20 Feb, 2023
You are given the slope of one line (m1) and you have to find the slope of another line which is perpendicular to the given line.
Examples:
Input : 5
Output : Slope of perpendicular line is : -0.20
Input : 4
Output : Slope of perpendicular line is : -0.25
Suppose we are given two perpendicular line segments AB and CD. The slope of AB is m1 and line CD is m2 .
m1 * m2 = -1
From above, we can say
m2 = – 1/( m1 ) .
How does above formula work?
Let slope of line AB be m1 and we need to find slope of line CD. Below diagram gives an idea about working of formula.
C++
#include <bits/stdc++.h>
using namespace std;
double findPCSlope( double m)
{
return -1.0 / m;
}
int main()
{
double m = 2.0;
cout << findPCSlope(m);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static double findPCSlope( double m)
{
return - 1.0 / m;
}
public static void main(String[] args)
{
double m = 2.0 ;
System.out.println(findPCSlope(m));
}
}
|
Python3
def findPCSlope(m):
return - 1.0 / m
m = 2.0
print (findPCSlope(m))
|
C#
using System;
class GFG {
static double findPCSlope( double m)
{
return -1.0 / m;
}
public static void Main()
{
double m = 2.0;
Console.Write(findPCSlope(m));
}
}
|
PHP
<?php
function findPCSlope( $m )
{
return -1.0 / $m ;
}
$m = 2.0;
echo findPCSlope( $m );
?>
|
Javascript
<script>
function findPCSlope(m)
{
return -1.0 / m;
}
let m = 2.0;
document.write(findPCSlope(m));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...