Software Development Exam  >  Software Development Notes  >  DSA in C++  >  Code: Find Nth Power of a number using Recursion

Code: Find Nth Power of a number using Recursion | DSA in C++ - Software Development PDF Download

Here's a C++ code that calculates the Nth power of a number using recursion:

#include <iostream>

double power(double base, int exponent) {

    if (exponent == 0)

        return 1;

    else if (exponent < 0)

        return 1 / power(base, -exponent);

    else

        return base * power(base, exponent - 1);

}

int main() {

    double base;

    int exponent;

    std::cout << "Enter the base: ";

    std::cin >> base;

    std::cout << "Enter the exponent: ";

    std::cin >> exponent;

    double result = power(base, exponent);

    std::cout << base << " raised to the power of " << exponent << " is: " << result << std::endl;

    return 0;

}

Explanation:

  • The 'power' function takes two parameters: 'base' (the number to be raised to a power) and 'exponent' (the power to which the number is raised).
  • Inside the 'power' function, we have three cases:
  • If the exponent is 0, we return 1 since any number raised to the power of 0 is 1.
  • If the exponent is negative, we compute the reciprocal of the power by calling '1 / power(base, -exponent)'.
  • For any other positive exponent, we recursively call 'power' with 'base' multiplied by 'power(base, exponent - 1)'.
  • In the 'main' function, we ask the user to enter the base and exponent values.
  • We call the 'power' function with the given base and exponent, and store the result in the 'result' variable.
  • Finally, we output the base, exponent, and the calculated result.

Example Output:

Let's say we want to calculate 2 raised to the power of 5.

Enter the base: 2

Enter the exponent: 5

2 raised to the power of 5 is: 32

In this case, the code will output that 2 raised to the power of 5 is 32. The code uses recursion to repeatedly multiply the base by itself until the exponent becomes 0, effectively calculating the desired power.

The document Code: Find Nth Power of a number using Recursion | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
153 videos|115 docs|24 tests

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Extra Questions

,

Viva Questions

,

Previous Year Questions with Solutions

,

MCQs

,

mock tests for examination

,

Code: Find Nth Power of a number using Recursion | DSA in C++ - Software Development

,

study material

,

Semester Notes

,

Free

,

Sample Paper

,

video lectures

,

shortcuts and tricks

,

Important questions

,

pdf

,

past year papers

,

ppt

,

practice quizzes

,

Objective type Questions

,

Code: Find Nth Power of a number using Recursion | DSA in C++ - Software Development

,

Exam

,

Summary

,

Code: Find Nth Power of a number using Recursion | DSA in C++ - Software Development

;