Slope of perpendicular to line

You are given the slope of one line (m1) and you have to find the slope of another lie 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program find slope of perpendicular line
#include <bits/stdc++.h>
using namespace std;
  
// Function to find
// the Slope of other line
double findPCSlope(double m)
{
    return -1.0 / m;
}
  
int main()
{
    double m = 2.0;
    cout << findPCSlope(m);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustarate ...
  
import java.io.*;
import java.util.*;
  
class GFG {
  
    // Function to find
    // the Slope of other line
    static double findPCSlope(double m)
    {
        return -1.0 / m;
    }
  
    public static void main(String[] args)
    {
  
        double m = 2.0;
        System.out.println(findPCSlope(m));
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program find 
# slope of perpendicular line
  
# Function to find
# the Slope of other line
def findPCSlope(m):
  
    return -1.0 / m
  
m = 2.0
print(findPCSlope(m))
  
# This code is contributed
# by Smitha 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find Slope 
// of perpendicular to line
using System;
  
class GFG {
  
    // Function to find
    // the Slope of other line
    static double findPCSlope(double m)
    {
        return -1.0 / m;
    }
  
    // Driver Code
    public static void Main()
    {
  
        double m = 2.0;
        Console.Write(findPCSlope(m));
    }
}
  
// This code is contributed by nitin mittal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program find slope 
// of perpendicular line
  
// Function to find the
// Slope of other line
function findPCSlope($m)
{
    return -1.0 / $m;
}
  
    // Driver Code
    $m = 2.0;
    echo findPCSlope($m);
      
// This code is contributed by anuj_67
?>

chevron_right


Output:

-0.5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.