Practical 4
Practical 4
PRACTICAL-4
Theory:
In this task, we need to create a C++ function power that raises a number
m to the power of n. The function should take a double value for m (the
base) and an int value for n (the exponent). To simplify the calculation of
squares, we'll make n a default argument with a value of 2.
Explanation
Code Implementation
#include <iostream>
int main() {
// Test cases
double base;
int exponent;
std::cout << "Square of " << base << " is: " << power(base) <<
std::endl;
std::cout << base << " raised to the power " << exponent << " is: "
<< power(base,
return 0;
Key Points
2. The <cmath> library is used for the pow function, which efficiently
computes powers.
Example Output
Square of 4 is: 16
Enter a number (base): 2.5