You are here

sqrt in C

sqrt in C

Function sqrt in C returns the square root of a number. To use it, include the "math.h" header file in the program.

Declaration: double sqrt(double);

Square root in C using sqrt function

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

int main()
{
  double n, result;

  printf("Enter a number to calculate its square root\n");
  scanf("%lf", &n);

  result = sqrt(n);

  printf("Square root of %.2lf = %.2lf\n", n, result);

  return 0;
}

We use "%.2lf" in printf function to print two decimal digits.

Output of program:
sqrt in C

C program to find square root of numbers from 1 to 100

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

int main()
{
  int c;

  // The function sqrt expects a double data type argument so we convert int to a double.

  for (c = 1; c <= 100; c++)
    printf("√%d = %lf\n", c, sqrt((double)c));

  return 0;
}

When we run it, we get the output:

1 = 1.000000
2 = 1.414214
3 = 1.732051
4 = 2.000000
5 = 2.236068
6 = 2.449490
7 = 2.645751
8 = 2.828427
9 = 3.000000
10 = 3.162278
11 = 3.316625
12 = 3.464102
13 = 3.605551
14 = 3.741657
15 = 3.872983
16 = 4.000000
17 = 4.123106
18 = 4.242641
19 = 4.358899
20 = 4.472136
21 = 4.582576
22 = 4.690416
23 = 4.795832
24 = 4.898979
25 = 5.000000
26 = 5.099020
27 = 5.196152
28 = 5.291503
29 = 5.385165
30 = 5.477226
31 = 5.567764
32 = 5.656854
33 = 5.744563
34 = 5.830952
35 = 5.916080
36 = 6.000000
37 = 6.082763
38 = 6.164414
39 = 6.244998
40 = 6.324555
41 = 6.403124
42 = 6.480741
43 = 6.557439
44 = 6.633250
45 = 6.708204
46 = 6.782330
47 = 6.855655
48 = 6.928203
49 = 7.000000
50 = 7.071068
51 = 7.141428
52 = 7.211103
53 = 7.280110
54 = 7.348469
55 = 7.416198
56 = 7.483315
57 = 7.549834
58 = 7.615773
59 = 7.681146
60 = 7.745967
61 = 7.810250
62 = 7.874008
63 = 7.937254
64 = 8.000000
65 = 8.062258
66 = 8.124038
67 = 8.185353
68 = 8.246211
69 = 8.306624
70 = 8.366600
71 = 8.426150
72 = 8.485281
73 = 8.544004
74 = 8.602325
75 = 8.660254
76 = 8.717798
77 = 8.774964
78 = 8.831761
79 = 8.888194
80 = 8.944272
81 = 9.000000
82 = 9.055385
83 = 9.110434
84 = 9.165151
85 = 9.219544
86 = 9.273618
87 = 9.327379
88 = 9.380832
89 = 9.433981
90 = 9.486833
91 = 9.539392
92 = 9.591663
93 = 9.643651
94 = 9.695360
95 = 9.746794
96 = 9.797959
97 = 9.848858
98 = 9.899495
99 = 9.949874
100 = 10.000000