Domain For Sale!

www.techbaz.org

This domain, along with all its contents, is available for purchase.

Contact: tb.0x2e@gmail.com

Mathematical Calculations

Mathematical operations in C++.

<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:

cpp Copy Code
1#include<iostream>
2#include<cmath>
3
4int main() {
5// Input: Get a number from the user
6double num;
7std::cout << "Enter a number: ";
8std::cin >> num;
9
10// Calculate square root using std::sqrt function
11if (num >= 0) {
12double squRt = std::sqrt(num);
13std::cout << "Square root of " << num << " is: " << squRt << std::endl;
14} else {
15std::cout << "Cannot calculate of a negative number." << std::endl;
16}
17
18return 0;
19}
Output:
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:

cpp Copy Code
1#include<iostream>
2#include<cmath>
3
4int main() {
5// Basic arithmetic functions
6double x = 4.0, y = 2.0;
7
8// Addition
9double sum = x + y;
10std::cout << "Sum: " << sum << std::endl;
11
12// Subtraction
13double difference = x - y;
14std::cout << "Difference: " << difference << std::endl;
15
16// Multiplication
17double product = x * y;
18std::cout << "Product: " << product << std::endl;
19
20// Division
21double quotient = x / y;
22std::cout << "Quotient: " << quotient << std::endl;
23
24// Exponential and logarithmic functions
25double base = 2.0, exponent = 3.0;
26
27// Power function: base^exponent
28double powerResult = std::pow(base, exponent);
29std::cout << "Power result: " << powerResult << std::endl;
30
31// Square root
32double sqrtResult = std::sqrt(x);
33std::cout << "Square root of x: " << sqrtResult << std::endl;
34
35// Logarithm to the base 10
36double logResult = std::log10(x);
37std::cout << "Logarithm base 10 of x: " << logResult << std::endl;
38
39// Trigonometric functions
40double angleInRadians = 45.0; // Angles are typically in radians
41
42// Sine
43double sinResult = std::sin(angleInRadians);
44std::cout << "Sine of " << angleInRadians << " radians: " << sinResult << std::endl;
45
46// Cosine
47double cosResult = std::cos(angleInRadians);
48std::cout << "Cosine of " << angleInRadians << " radians: " << cosResult << std::endl;
49
50// Tangent
51double tanResult = std::tan(angleInRadians);
52std::cout << "Tangent of " << angleInRadians << " radians: " << tanResult << std::endl;
53
54return 0;
55}
Output:
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.