More functions from the
<math.h> or <cmath> header files. Today it’s
about the sqrt function.
The sqrt
function is straightforward. Given values for x, the function returns y, the
square root.
From C11 standard.
Test Code.
I tested the functions in Visual C++ 2010 as an console
application.
// The
standard library includes the system function.
#include <cstdlib>
// C++
standard I/O library.
#include <cstdio>
// C++
math library.
#include <cmath>
int main()
{
// Header.
printf("The
sqrt Function in C & C++\n\n");
printf("Given
x, the function returns y equal to the square root of x.\n\n");
// Counter for
loop.
int i;
// Argument.
double n;
// Result.
double y;
printf(" y
x y =
x\n\n");
for (i = 1;
i < 10; ++i)
{
n = 12.3456 / i;
y = sqrt(n);
printf ("%5.4f x %5.4f = %6.4f\n", y, y, n);
}
// Keep console
window open.
system("pause");
// Return some
value.
return 0;
} // end main
Output.
No comments:
Post a Comment