Find the height of a right-angled triangle whose area is X times its base
Last Updated :
23 Apr, 2022
Given that the area of a right-angled triangle is X times its base b. The task is to find the height of the given triangle.
Examples:
Input: X = 40
Output: 80
Input: X = 100
Output: 200
Approach: We know that the area of a right-angled triangle, Area = (base * height) / 2 and it is given that this area is X times the base i.e. base * X = (base * height) / 2.
Solving for height, we get height = (2 * base * X) / base = 2 * X.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getHeight( int X)
{
return (2 * X);
}
int main()
{
int X = 35;
cout << getHeight(X);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Gfg
{
static int getHeight( int X)
{
return ( 2 * X);
}
public static void main (String[] args) throws java.lang.Exception
{
int X = 35 ;
System.out.println(getHeight(X)) ;
}
}
|
Python3
def getHeight(X):
return ( 2 * X)
if __name__ = = '__main__' :
X = 35
print (getHeight(X))
|
C#
using System;
class Gfg
{
static int getHeight( int X)
{
return (2 * X);
}
public static void Main ()
{
int X = 35;
Console.WriteLine(getHeight(X)) ;
}
}
|
Javascript
<script>
function getHeight(X)
{
return (2 * X);
}
var X = 35;
document.write(getHeight(X)) ;
</script>
|
Time Complexity: O(1), as we are doing only arithmetic operation.
Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...