.TH sqrt() "" "" "Mathematics Function (libm)"
.PC "Compute square root"
.B "#include <math.h>"
\fBdouble sqrt(\fIz\^\fB) double \fIz\^\fB;\fR
.PP
\fBsqrt()\fR returns the square root of \fIz\fR.
.SH Example
The following program prints all prime numbers between one and a
positive integer that the user enters.
.II "Young, Michael B."
It was written by Michael B. Young (myoung@mcs.csuhayward.edu).
.DM
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
.DE
.DM
int main()
{
	int i, userinput;
.DE
.DM
	/* get user input */
	fprintf(stderr, "Enter an integer value greater than 2:  ");
	scanf("%d", &userinput);
.DE
.DM
	if (userinput < 3) {
		fprintf(stderr, "Error:  enter a positive integer > 2\en");
		exit(EXIT_FAILURE);
	}
.DE
.DM
	/* test for all numbers between one and "userinput". */
	/* for efficiency's sake, even numbers are not tested. */
	/* two is the only even prime number */
.DE
.DM
	printf("%d\en", 2);
	for (i = 3; i < userinput; i += 2)
		if (prime(i))
			printf("%d\en", i);
.DE
.DM
	exit(EXIT_SUCCESS);
}
.DE
.DM
/*
 *  function prime() - tests the passed integer testvalue for "prime-ness"
 *  by testing whether each integer between 1 and the square root of
 *  testvalue divides evenly into testvalue.  Returns 1 if prime, 0 if not.
 */
int prime(testvalue)
int testvalue;
{
	int end, j, result;
.DE
.DM
	end = (int) sqrt ( (double) testvalue );
	for (j = 2, result = 1; result == 1 && j <= end; j++) {
		if ((testvalue % j) == 0)
			result = 0;
	}
	return result;
}
.DE
.SH "See Also"
.Xr "cos()," cos
.Xr "cosh()," cosh
.Xr "libm," libm
.Xr "sin()" sin
.br
\*(AS, \(sc7.5.5.2
.br
\*(PX Standard, \(sc8.1
.SH Diagnostics
When a domain error occurs (i.e., when \fIz\fP is negative),
.B sqrt()
sets \fBerrno\fR to \fBEDOM\fR and returns zero.
