Given the intercepts of a straight line on both the axes as **m** and **n**. The task is to find the length of the normal on this straight line from the origin.

**Examples:**

Input:m = 5, n = 3

Output:2.57248

Input:m = 13, n = 9

Output:7.39973

**Approach:** A normal to a line is a line segment drawn from a point perpendicular to the given line.

Let **p** be the length of the normal drawn from the origin to a line, which subtends an angle **Θ** with the positive direction of x-axis as follows.

Then, we have **cos Θ = p / m** and **sin Θ = p / n**

Since, **sin ^{2} Θ + cos^{2} Θ = 1**

So,

**(p / m)**

^{2}+ (p / n)^{2}= 1We get,

**p = m * n / √(m**

^{2}+ n^{2})Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the normal ` `// of the straight line ` `float` `normal(` `float` `m, ` `float` `n) ` `{ ` ` ` `// Length of the normal ` ` ` `float` `N = (fabsf(m) * fabsf(n)) ` ` ` `/ ` `sqrt` `((fabsf(m) * fabsf(m)) ` ` ` `+ (fabsf(n) * fabsf(n))); ` ` ` ` ` `return` `N; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `float` `m = -5, n = 3; ` ` ` `cout << normal(m, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to find the normal ` `// of the straight line ` `static` `float` `normal(` `float` `m, ` `float` `n) ` `{ ` ` ` `// Length of the normal ` ` ` `float` `N = (` `float` `) ((Math.abs(m) * Math.abs(n)) ` ` ` `/ Math.sqrt((Math.abs(m) * Math.abs(m)) ` ` ` `+ (Math.abs(n) * Math.abs(n)))); ` ` ` ` ` `return` `N; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `float` `m = -` `5` `, n = ` `3` `; ` ` ` `System.out.println(normal(m, n)); ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `import` `math; ` ` ` `# Function to find the normal ` `# of the straight line ` `def` `normal(m, n): ` ` ` ` ` `# Length of the normal ` ` ` `N ` `=` `((` `abs` `(m) ` `*` `abs` `(n)) ` `/` ` ` `math.sqrt((` `abs` `(m) ` `*` `abs` `(m)) ` `+` ` ` `(` `abs` `(n) ` `*` `abs` `(n)))); ` ` ` ` ` `return` `N; ` ` ` `# Driver code ` `m ` `=` `-` `5` `; n ` `=` `3` `; ` `print` `(normal(m, n)); ` ` ` `# This code is contributed ` `# by Akanksha Rai ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the normal ` `// of the straight line ` `static` `float` `normal(` `float` `m, ` `float` `n) ` `{ ` ` ` `// Length of the normal ` ` ` `float` `N = (` `float` `)((Math.Abs(m) * Math.Abs(n)) / ` ` ` `Math.Sqrt((Math.Abs(m) * Math.Abs(m)) + ` ` ` `(Math.Abs(n) * Math.Abs(n)))); ` ` ` ` ` `return` `N; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `float` `m = -5, n = 3; ` ` ` `Console.Write(normal(m, n)); ` `} ` `} ` ` ` `// This code is contributed by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to find the normal ` `// of the straight line ` `function` `normal(` `$m` `, ` `$n` `) ` `{ ` ` ` `// Length of the normal ` ` ` `$N` `= (` `abs` `(` `$m` `) * ` `abs` `(` `$n` `)) / ` ` ` `sqrt((` `abs` `(` `$m` `) * ` `abs` `(` `$m` `)) + ` ` ` `(` `abs` `(` `$n` `) * ` `abs` `(` `$n` `))); ` ` ` ` ` `return` `$N` `; ` `} ` ` ` `// Driver code ` `$m` `= -5; ` `$n` `= 3; ` `echo` `normal(` `$m` `, ` `$n` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2.57248

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find X and Y intercepts of a line passing through the given points
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Program to find X, Y and Z intercepts of a plane
- Represent a given set of points by the best possible straight line
- Check if it is possible to draw a straight line with the given direction cosines
- Area of triangle formed by the axes of co-ordinates and a given straight line
- Straight-line Number
- Puzzle | Place numbers 1 to 9 in a Circle such that sum of every triplet in straight line is 15
- Check whether a straight line can be formed using N co-ordinate points
- Check if a line passes through the origin
- Number of jump required of given length to reach a point of form (d, 0) from origin in 2D plane
- Slope of the line parallel to the line with the given slope
- Check if given two straight lines are identical or not
- Count straight lines intersecting at a given point
- Count squares of unique dimensions possible from given Straight Lines parallel to the axes
- Lexicographically Kth smallest way to reach given coordinate from origin
- Find the maximum possible distance from origin using given points
- Minimum distance to visit given K points on X-axis after starting from the origin
- Find normal at a given point on the curve
- Count of different straight lines with total n points with m collinear

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.