Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Program To Calculate Number Power Using Recursion In C++. The power number should always be positive integer.

#include <iostream>
using namespace std;

int calculatePower(int, int);

int main()
{
    int base, powerRaised, result;

    cout << "Enter base number: ";
    cin >> base;

    cout << "Enter power number(positive integer): ";
    cin >> powerRaised;

    result = calculatePower(base, powerRaised);
    cout << base << "^" << powerRaised << " = " << result;

    return 0;
}

int calculatePower(int base, int powerRaised)
{
    if (powerRaised != 0)
        return (base*calculatePower(base, powerRaised-1));
    else
        return 1;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: last character of std::string 
Cpp :: cpp getter as const 
Cpp :: c++ int length 
Cpp :: c++ struct constructor 
Cpp :: substr in cpp 
Cpp :: stack overflow c++ 
Cpp :: remove first occurrence of value from vector c++ 
Cpp :: c detect os 
Cpp :: C++, for-loop over an array array 
Cpp :: C++ code for Dijkstra’s Algorithm 
Cpp :: c++ operator overloading 
Cpp :: sum of a matrix c++ 
Cpp :: double to float c++ 
Cpp :: show stack c++ 
Cpp :: accumulate() in c++ 
Cpp :: factorial in c++ using recursion 
Cpp :: function overriding in c++ 
Cpp :: initialize vector 
Cpp :: c++ changing string to double 
Cpp :: c++ print 
Cpp :: exponent power of x using c c++ 
Cpp :: changing values of mat in opencv c++ 
Cpp :: convert ascii char value to hexadecimal c++ 
Cpp :: adddynamic ue4 c++ 
Cpp :: if argv == string 
Cpp :: remove comments c++ 
Cpp :: Basic Input / Output in C++ 
Cpp :: move assignment operator c++ 
Cpp :: c++ multiline string 
Cpp :: string copy in cpp 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =