Home » C++ programming language

hypot() function with example in C++

C++ hypot() function: Here, we are going to learn about the hypot() function with example of cmath header in C++ programming language?
Submitted by IncludeHelp, on April 26, 2019

C++ hypot() function

hypot() function is a library function of cmath header, it is used to find the hypotenuse of the given numbers, it accepts two numbers and returns the calculated result of hypotenuse i.e. sqrt(x*x + y*y).

Syntax of hypot() function:

    hypot(x, y);

Parameter(s): x, y – numbers to be calculated hypotenuse (or sqrt(x*x + y*y))

Return value: double – it returns double value that is the result of the expression sqrt(x*x + y*y).

Example:

    Input:
    float x = 10.23;
    float y = 2.3;

    Function call:
    hypot(x, y);

    Output:
    10.4854

C++ code to demonstrate the example of hypot() function

// C++ code to demonstrate the example of 
// hypot() function

#include <iostream>
#include <cmath>
using namespace std;

// main code section
int main()
{
    float x = 10.23;
    float y = 2.3;

    // calculate the hypotenuse 
    float result = hypot(x, y);
    cout<<"hypotenuse of "<<x<<" and "<<y<<" is = "<<result;
    cout<<endl;
    
    //using sqrt() function
    result = sqrt((x*x + y*y));
    cout<<"hypotenuse of "<<x<<" and "<<y<<" is = "<<result;
    cout<<endl;
    
    return 0;
}

Output

hypotenuse of 10.23 and 2.3 is = 10.4854
hypotenuse of 10.23 and 2.3 is = 10.4854


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.