-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.cpp
More file actions
50 lines (37 loc) · 1.94 KB
/
Copy pathmath.cpp
File metadata and controls
50 lines (37 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <cmath> // Required for advanced math functions like sqrt, round, log, pow, etc.
using namespace std;
int main() {
// ==========================================
// 1. BUILT-IN MAX AND MIN
// ==========================================
// max() and min() are available by default in C++ (no <cmath> needed)
cout << "=== 1. MAX AND MIN ===" << endl;
cout << "Highest value (5, 10) : " << max(5, 10) << endl; // Output: 10
cout << "Lowest value (5, 10) : " << min(5, 10) << endl; // Output: 5
// ==========================================
// 2. C++ <cmath> LIBRARY FUNCTIONS
// ==========================================
// The <cmath> library provides advanced mathematical operations
cout << "\n=== 2. CMATH FUNCTIONS ===" << endl;
// Square Root: sqrt(x)
cout << "Square root of 64 : " << sqrt(64) << endl; // Output: 8
// Rounding: round(x) - Rounds to the nearest whole number
cout << "Round 2.6 : " << round(2.6) << endl; // Output: 3
cout << "Round 2.4 : " << round(2.4) << endl; // Output: 2
// Natural Logarithm: log(x) - Base 'e' logarithm
cout << "Natural log of 2 : " << log(2) << endl; // Output: 0.693147
// ==========================================
// 3. OTHER COMMONLY USED CMATH FUNCTIONS
// ==========================================
cout << "\n=== 3. OTHER USEFUL MATH FUNCTIONS ===" << endl;
// Power: pow(base, exponent) -> 2^3
cout << "2 to the power of 3 : " << pow(2, 3) << endl; // Output: 8
// Absolute Value: abs(x) -> Removes negative sign
cout << "Absolute value of -10 : " << abs(-10) << endl; // Output: 10
// Ceiling: ceil(x) -> Always rounds UP
cout << "Ceil of 1.2 : " << ceil(1.2) << endl; // Output: 2
// Floor: floor(x) -> Always rounds DOWN
cout << "Floor of 1.8 : " << floor(1.8) << endl; // Output: 1
return 0;
}