C++ Language
<cmath>
In C++, the '<cmath>' header (or '<math.h>' in C) provides a set of functions for performing mathematical operations. Here's a simple C++ program that find the square root of a given number:
1 | #include<iostream> |
2 | #include<cmath> |
3 |
|
4 | int main() { |
5 | // Input: Get a number from the user |
6 | double num; |
7 | std::cout << "Enter a number: "; |
8 | std::cin >> num; |
9 |
|
10 | // Calculate square root using std::sqrt function |
11 | if (num >= 0) { |
12 | double squRt = std::sqrt(num); |
13 | std::cout << "Square root of " << num << " is: " << squRt << std::endl; |
14 | } else { |
15 | std::cout << "Cannot calculate of a negative number." << std::endl; |
16 | } |
17 |
|
18 | return 0; |
19 | } |
Enter a number: 4 Square root of 4 is: 2
This program prompts the user to enter a number, then it calculates the square root using the 'std::sqrt' function from the '<cmath>' header.
• Some commonly used functions along with examples:
1 | #include<iostream> |
2 | #include<cmath> |
3 |
|
4 | int main() { |
5 | // Basic arithmetic functions |
6 | double x = 4.0, y = 2.0; |
7 |
|
8 | // Addition |
9 | double sum = x + y; |
10 | std::cout << "Sum: " << sum << std::endl; |
11 |
|
12 | // Subtraction |
13 | double difference = x - y; |
14 | std::cout << "Difference: " << difference << std::endl; |
15 |
|
16 | // Multiplication |
17 | double product = x * y; |
18 | std::cout << "Product: " << product << std::endl; |
19 |
|
20 | // Division |
21 | double quotient = x / y; |
22 | std::cout << "Quotient: " << quotient << std::endl; |
23 |
|
24 | // Exponential and logarithmic functions |
25 | double base = 2.0, exponent = 3.0; |
26 |
|
27 | // Power function: base^exponent |
28 | double powerResult = std::pow(base, exponent); |
29 | std::cout << "Power result: " << powerResult << std::endl; |
30 |
|
31 | // Square root |
32 | double sqrtResult = std::sqrt(x); |
33 | std::cout << "Square root of x: " << sqrtResult << std::endl; |
34 |
|
35 | // Logarithm to the base 10 |
36 | double logResult = std::log10(x); |
37 | std::cout << "Logarithm base 10 of x: " << logResult << std::endl; |
38 |
|
39 | // Trigonometric functions |
40 | double angleInRadians = 45.0; // Angles are typically in radians |
41 |
|
42 | // Sine |
43 | double sinResult = std::sin(angleInRadians); |
44 | std::cout << "Sine of " << angleInRadians << " radians: " << sinResult << std::endl; |
45 |
|
46 | // Cosine |
47 | double cosResult = std::cos(angleInRadians); |
48 | std::cout << "Cosine of " << angleInRadians << " radians: " << cosResult << std::endl; |
49 |
|
50 | // Tangent |
51 | double tanResult = std::tan(angleInRadians); |
52 | std::cout << "Tangent of " << angleInRadians << " radians: " << tanResult << std::endl; |
53 |
|
54 | return 0; |
55 | } |
Sum: 6 Difference: 2 Product: 8 Quotient: 2 Power result: 8 Square root of x: 2 Logarithm base 10 of x: 0.60206 Sine of 45 radians: 0.850904 Cosine of 45 radians: 0.525322 Tangent of 45 radians: 1.61978
This is just a basic example, and there are many more functions available in the '<cmath>' header for various mathematical operations.
What's Next?
We've now entered the finance section on this platform, where you can enhance your financial literacy.