|
1 | 1 | ---
|
2 | 2 | Title: 'sqrt()'
|
3 |
| -Description: 'Returns square root of the argument.' |
| 3 | +Description: 'Returns the square root of a number in C++.' |
4 | 4 | Subjects:
|
| 5 | + - 'Code Foundations' |
5 | 6 | - 'Computer Science'
|
6 | 7 | Tags:
|
| 8 | + - 'Algorithms' |
7 | 9 | - 'Functions'
|
8 |
| - - 'Arithmetic' |
| 10 | + - 'Math' |
9 | 11 | CatalogContent:
|
10 | 12 | - 'learn-c-plus-plus'
|
11 | 13 | - 'paths/computer-science'
|
12 | 14 | ---
|
13 | 15 |
|
14 |
| -The **`sqrt()`** function returns the square root of the argument. |
| 16 | +C++ **`sqrt()`** function returns the square root of a number. This mathematical function is part of the C++ standard library and is defined in the `<cmath>` header file. |
15 | 17 |
|
16 |
| -## Syntax |
| 18 | +## Syntax of C++ `sqrt()` |
17 | 19 |
|
18 | 20 | ```pseudo
|
19 |
| -sqrt(x) |
| 21 | +sqrt(num) |
20 | 22 | ```
|
21 | 23 |
|
22 |
| -If the parameter `x` is negative (less than 0), then a domain error will occur. |
| 24 | +**Parameters:** |
23 | 25 |
|
24 |
| -## Example |
| 26 | +- `num`: A floating-point number (double, float, or long double) for which the square root is to be calculated |
25 | 27 |
|
26 |
| -The following example uses the `sqrt()` function to find the square root of `4`: |
| 28 | +**Return value:** |
| 29 | + |
| 30 | +The `sqrt()` function returns the square root of the given argument as a floating-point value. Its return type matches the type of the argument (e.g., float, double, or long double). |
| 31 | + |
| 32 | +> **Note:** If a negative argument is passed to `sqrt()`, a domain error occurs and the function returns `NaN` (Not a Number). |
| 33 | +
|
| 34 | +## Example 1: Basic Usage of `sqrt()` function |
| 35 | + |
| 36 | +This example demonstrates the basic usage of the `sqrt()` function with different numerical values: |
| 37 | + |
| 38 | +```cpp |
| 39 | +#include <iostream> |
| 40 | +#include <cmath> |
| 41 | +using namespace std; |
| 42 | + |
| 43 | +int main() { |
| 44 | + // Calculate square root of different numbers |
| 45 | + cout << "Square root of 16: " << sqrt(16) << endl; |
| 46 | + cout << "Square root of 25: " << sqrt(25) << endl; |
| 47 | + cout << "Square root of 2: " << sqrt(2) << endl; |
| 48 | + cout << "Square root of 100.5: " << sqrt(100.5) << endl; |
| 49 | + |
| 50 | + return 0; |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +This example results in the following output: |
| 55 | + |
| 56 | +```shell |
| 57 | +Square root of 16: 4 |
| 58 | +Square root of 25: 5 |
| 59 | +Square root of 2: 1.41421 |
| 60 | +Square root of 100.5: 10.025 |
| 61 | +``` |
| 62 | + |
| 63 | +> **Note:** Output may vary slightly depending on platform-specific floating-point rounding. |
| 64 | +
|
| 65 | +## Example 2: Distance Calculation using C++ `sqrt()` |
| 66 | + |
| 67 | +This example shows how `sqrt()` is used to calculate the distance between two points using the Pythagorean theorem: |
27 | 68 |
|
28 | 69 | ```cpp
|
29 | 70 | #include <iostream>
|
30 | 71 | #include <cmath>
|
| 72 | +using namespace std; |
31 | 73 |
|
32 | 74 | int main() {
|
33 |
| - double x = 4; |
34 |
| - double result; |
| 75 | + // Calculate distance between two points (x1, y1) and (x2, y2) |
| 76 | + double x1 = 1.0, y1 = 2.0; |
| 77 | + double x2 = 4.0, y2 = 6.0; |
| 78 | + |
| 79 | + // Apply distance formula: sqrt((x2-x1)^2 + (y2-y1)^2) |
| 80 | + double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); |
35 | 81 |
|
36 |
| - result = std::sqrt(x); |
| 82 | + cout << "Distance between points (" << x1 << ", " << y1 << ") and (" |
| 83 | + << x2 << ", " << y2 << ") is: " << distance << endl; |
37 | 84 |
|
38 |
| - std::cout << "The square root of " << x << " is " << result << "\n"; |
39 |
| - // Output: The square root of 4 is 2 |
| 85 | + return 0; |
40 | 86 | }
|
41 | 87 | ```
|
42 | 88 |
|
43 |
| -## Codebyte Example |
| 89 | +This example results in the following output: |
44 | 90 |
|
45 |
| -The following example is runnable and returns the square root of `9` with the `sqrt()` function: |
| 91 | +```shell |
| 92 | +Distance between points (1, 2) and (4, 6) is: 5 |
| 93 | +``` |
| 94 | + |
| 95 | +## Codebyte Example: Using `sqrt()` to Solve Quadratic Equation |
| 96 | + |
| 97 | +This example demonstrates using `sqrt()` to solve a quadratic equation using the quadratic formula: |
46 | 98 |
|
47 | 99 | ```codebyte/cpp
|
48 | 100 | #include <iostream>
|
49 | 101 | #include <cmath>
|
| 102 | +using namespace std; |
50 | 103 |
|
51 | 104 | int main() {
|
52 |
| - double x = 9; |
53 |
| - double result; |
| 105 | + // Solve quadratic equation ax^2 + bx + c = 0 |
| 106 | + double a = 1.0, b = -5.0, c = 6.0; |
| 107 | +
|
| 108 | + // Calculate discriminant |
| 109 | + double discriminant = b * b - 4 * a * c; |
54 | 110 |
|
55 |
| - result = std::sqrt(x); |
| 111 | + if (discriminant >= 0) { |
| 112 | + // Calculate roots using quadratic formula |
| 113 | + double root1 = (-b + sqrt(discriminant)) / (2 * a); |
| 114 | + double root2 = (-b - sqrt(discriminant)) / (2 * a); |
56 | 115 |
|
57 |
| - std::cout << "The square root of " << x << " is " << result << "\n"; |
| 116 | + cout << "For equation " << a << "x^2 + " << b << "x + " << c << " = 0" << endl; |
| 117 | + cout << "Root 1: " << root1 << endl; |
| 118 | + cout << "Root 2: " << root2 << endl; |
| 119 | + } else { |
| 120 | + cout << "No real roots exist for this equation." << endl; |
| 121 | + } |
| 122 | +
|
| 123 | + return 0; |
58 | 124 | }
|
59 | 125 | ```
|
| 126 | + |
| 127 | +## Frequently Asked Questions |
| 128 | + |
| 129 | +### 1. What happens if I pass a negative number to `sqrt()`? |
| 130 | + |
| 131 | +Passing a negative number to `sqrt()` results in a domain error and returns `NaN` (Not a Number). |
| 132 | + |
| 133 | +### 2. What header file do I need to include to use `sqrt()`? |
| 134 | + |
| 135 | +You need to include the `<cmath>` header file to use the `sqrt()` function. |
| 136 | + |
| 137 | +### 3. What is the return type of the function `sqrt()` in C++? |
| 138 | + |
| 139 | +The `sqrt()` function returns a floating-point value of the same type as the input parameter. If you pass a float, it returns a float; if you pass a double, it returns a double, and if you pass a long double, it returns a long double. |
0 commit comments