Header Ads Widget

length of hypotenuse | HCL coding question

Problem:-

  You are given two integers A and B. A represents a coordinate on the X axis (0, A) and B represents a coordinate on the Y-axis (B,0) These are two co-ordinate points of a right-angled triangle, the third point being the origin(0,0). You will be given N such triangles in the input. Find out and print the length of hypotenuse of all the triangles.

Note:

The formula of the length of a hypotenuse = root(a² + b²) where a and b represent the length of the other two sides of the triangle.

If the length of the hypotenuse is in decimal, round it to the next greater integer.

Input Format:

The input consist is given in the following format:

  • The first line contains an integer N denoting the number of triangles.
  • The next N lines contain two space-separated integers representing A and B respectively.

The input will be read from the STDIN by the candidate

Output Format:

The output consists of N lines:

  • Each line representing the length of the hypotenuse of the triangle. The output will be matched to the candidate's output printed on the STDOUT

Constraints:

  • 1<=A,B<=10^9

Example:

Input:

2

20 21

8 15

Output:

29

19


 C++ Code :-

#include <bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int a,b;
cin>>a>>b;
// sqrt((x2-x1)^2 +(y2-y1)^2)
cout<<ceil(sqrt(pow(a,2)+pow(b,2)))<<endl;
}

return 0;
}

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-


C-tutorial:-

See more:-