Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8014219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:51:54+00:00 2026-06-04T19:51:54+00:00

I have the following task: Write a program that asks for a number and

  • 0

I have the following task:

Write a program that asks for a number and a power. Write a recursive
function that takes the number to the power. For example, if the
number is 2 and the power is 4, the function will return 16.

I wrote a program and there are no errors when I compile it, but when I start the program and enter a value gives an error saying “Stack Overflow”. I suppose my recursive function became infinite but I have no idea how to write it in other way.

This is my code:

#include <iostream>
using namespace std;
int powpow(int number);

int main(){
    cout<<"Enter number:";
    int x;
    cin>>x;
    cout<<"The result of ("<<x<<" * "<<x<<") * "<<x*x<<" is: "<<powpow(x);
    system("pause");
    return 0;
}
int powpow(int number){
    int result = number*number;
    return powpow(result);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:51:56+00:00Added an answer on June 4, 2026 at 7:51 pm

    You have no terminating condition for your recursion, so it runs forever.

    It sounds like maybe you don’t have a good grasp of recursion, so I’d like to start with something a little simpler, the Fibonacci sequence.

    Any time we define a function in terms of recursion, we need to first define a base case(s). In the case of Fibonacci, we have 2 base cases:

    F(0) = 0
    F(1) = 1
    

    That says, in english, “F of 0 is 0, F of 1 is 1”. Or even more simply, if we pass 0 to function F, we will get 0 back. If we pass 1, we will get 1 back.

    Once we have the base cases defined, then we need to look for a recurrence relation. In the case of Fibonacci, we have the following recurrence:

    F(n) = F(n-1) + F(n-2)

    So for n >= 2, we can use the above recurrence. Why? Well, lets try it for n = 2.

    F(2) = F(n-1) + F(n-2)
         = F(1) + F(0)
         = 1    + 0
         = 1
    

    So now we know that the answer to F(2) is 1. And what’s more, we can now compute the answer to F(3). Why? Well, what do we need to compute F(3)? We need F(2) and F(1). We now have both of those answers since F(1) is a base case, and we just solved F(2) above.

    So, now let’s try to write a piece of pseudo code to solve F.

    function F(int n) {
      // handle base cases
      if (n equals 0)
        return 0
      if (n equals 1)
        return 1
    
      // recurrence
      return F(n-1) + F(n-2);
    }
    

    Note that in a recursive function, we always handle the base cases at the beginning of the function. We cannot define this recurrence if we don’t have base cases in place, otherwise, we will have no terminating condition for our recurrence. So that’s why you always put the base cases at the beginning of the function.

    Now, given the above explanation, another good exercise would be to write a recursive function for the factorial function. So, follow these steps:

    1. Define the base case (use wikipedia article for hints).
    2. Define recurrence in terms of base case
    3. Write pseudo code to solve the recurrence, and be sure to put base case(s) at beginning of function, and recurrence at end.
    

    Once you grasp these steps, then moving on to the power recurrence should make much more sense to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write program that create process using pipe() . My first task
I have the following situation: var Task = Backbone.Model.extend({ initialize: function() { }, save:
I am charged with the following task in a Rails project. Clients will have
We currently have an ant task that contains something similar to the following: <filelist
I'm needing to write a program (C#) that will allow the user to create
I have a multithreaded openCV program that uses 4 threads to do the following:
I have a task that requires me to print out the following quantities: I
I've been given the following task: An external source will write a name, datetime
I have the following task, which because of the combination of DestinationFiles and DestionationFolder
I have the following task in my MSBuild script: <Target Name=ZipStates> <Message Text=CREATING ZIP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.