Search code examples
csqrt

Why does the `sqrt()` print the desired output when I use `float`s instead of `double`s?


#include <stdio.h>
#include <math.h>

double hypotenuse( double side1, double side2 );

int main( void )
{
    double a, b;

    printf( "Enter the values of the two sides: " );
    scanf( "%f %f", &a, &b );
    printf( "\nThe length of hypotenuse is: %f", hypotenuse( a, b ) );   

    getchar();
    getchar();
    return 0;
}

double hypotenuse( double side1, double side2 )
{
       double c;

       c = (side1 * side1) + (side2 * side2);
       return sqrt( c );
}

The above program works when I use float a, b; instead of double a,b;. Why?


Solution

  • In your code, you use

    scanf( "%f %f", &a, &b );
    

    But %f is used for a float ,not a double. Try %lf for a double.