0% found this document useful (0 votes)
3 views

Practical 4

The document outlines a C++ program that defines a function to raise a number m to the power of n, with n defaulting to 2 for calculating squares. It explains the use of default arguments and demonstrates the function through test cases. The program utilizes the <cmath> library for efficient power calculations.

Uploaded by

hardikdeotale7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Practical 4

The document outlines a C++ program that defines a function to raise a number m to the power of n, with n defaulting to 2 for calculating squares. It explains the use of default arguments and demonstrates the function through test cases. The program utilizes the <cmath> library for efficient power calculations.

Uploaded by

hardikdeotale7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PRINCIPAL OF OBJECT ORIENTED PROGRAMINING

PRACTICAL-4

AIM: write a program in C++ for function power to raise a number m to


power n. The function takes a double value for m and int value for n. Use
default value for n to make the function to calculate squares when this
argument is omitted.

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

1. Default Argument: In C++, a default argument is a value provided


in the function declaration, used if no explicit value is passed for
that parameter when the function is called.

2. Function Definition: We'll use a loop or the pow function from


<cmath> to compute the power.

3. Usage: If n is not provided, the function computes the square of m


by default.

Code Implementation

#include <iostream>

#include <cmath> // For the pow function

// Function to compute m raised to the power n

double power(double m, int n = 2)

return pow(m, n); // Using the pow function from <cmath>

int main() {

// Test cases

double base;
int exponent;

// Example 1: Calculate square (default argument used)

std::cout << "Enter a number to calculate its square: ";

std::cin >> base;

std::cout << "Square of " << base << " is: " << power(base) <<
std::endl;

// Example 2: Calculate m raised to n

std::cout << "\nEnter a number (base): ";

std::cin >> base;

std::cout << "Enter an integer (exponent): ";

std::cin >> exponent;

std::cout << base << " raised to the power " << exponent << " is: "
<< power(base,

exponent) << std::endl;

return 0;

Key Points

1. The default value of n is set to 2, so calling power(m) without n


calculates the square of m.

2. The <cmath> library is used for the pow function, which efficiently
computes powers.

3. The program demonstrates both default and explicit arguments


through test cases.

Example Output

Enter a number to calculate its square: 4

Square of 4 is: 16
Enter a number (base): 2.5

Enter an integer (exponent): 3

2.5 raised to the power 3 is: 15.625

You might also like