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

  • Home
  • SEARCH
  • 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 6553301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:33:56+00:00 2026-05-25T12:33:56+00:00

Here is my code. Recursion: #include <stdio.h> int Recursion(int m,int n) { if(m==0 ||

  • 0

Here is my code.

Recursion:

#include <stdio.h>

int Recursion(int m,int n)
{
    if(m==0 || n==0)
        return 1;
    else
        return 2*Recursion(m-1,n+1);
}

void main()
{
    int m,n,result;
    m=3;
    n=-2;
    result=Recursion(m, n);
    printf("%d",result);
}

Iteration

#include <stdio.h>

int Recursion(int m,int n)
{
    int returnvalue=1;
    while(m!=0 && n!=0)
    {
        returnvalue=returnvalue*2;
        m--; n++;
    }
    return returnvalue;
}

void main()
{
    int m,n,result;
    m=3;
    n=-2;
    result=Recursion(m,n);
    printf("%d",result);
}

Now my doubts are:

  1. I read that to change from recursion to iteration, you need to use a stack and keep pushing the data and popping later. Now I don’t see doing it here. Why? When is stack used, and when is it not?
  2. Is my translation to an iterative version correct here? Is this tail recursive because the source of the Recursion code said so.
  3. How do I change from a recursive version to an iterative one. I would really need a nice source from where I can study this. Googling did not help much.
  • 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-05-25T12:33:56+00:00Added an answer on May 25, 2026 at 12:33 pm

    1) You need to use a stack when there are values to save for each call. In your case, you don’t use the values from deeper recursive calls after the recursive invocation so there is nothing to save on the stack.

    2) It’s tail recursion if the call to itself is the last thing it does. As @leppie comments, you are performing a 2* after the last method call.

    3) The general approach is to use a stack; however, in this case, there is nothing to save on a stack, so you can drop it.

    Here are some examples, one which requires a stack and another which uses tail recursion and does not.

    void reverseCounting(int i, int n) {
        if (i >= n) return;
        reverseCounting(i + 1, n);
        cout << i << endl;
    }
    
    void counting(int i, int n) {
        if (i >= n) return;
        cout << i << endl;
        counting(i + 1, n);
    }
    
    // you could just count backwards, but this is a simple example.
    void reverseCountingLoop(int i, int n) {
        // for C you can write you own stack to do the same thing.
        stack<int> stack; 
        //// if (i >= n) return;
        while (!(i >= n)) {
            //// reverseCounting(i + 1, n);
            // save i for later
            stack.push(i);
            // reuse i
            i = i + 1;
        }
        // unwind the stack.
        while (!stack.empty()) {
            //// cout << i << endl;
            i = stack.top(); stack.pop();
            cout << i << endl;
        }
    }
    
    void countingLoop(int i, int n) {
        //// if (i >= n) return;
        while (!(i >= n)) {
            //// cout << i << endl;
            cout << i << endl;
            //// counting(i + 1, n);
            // reuse i
            i = i + 1;
        }
        //// nothing after the recursive call.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code here: var infiltrationResult; while(thisOption) { var trNode = document.createElement('tr'); var
here is the plus code that i don't understand plus(0,X,X):-natural_number(X). plus(s(X),Y,s(Z)) :- plus(X,Y,Z). while
I want to calculate the power of 2 using recursion. Here is my code:
#include <stdio.h> #include <ctype.h> int isPalindrome( char *str, int length ) { if (
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here is code from MSDN . I don't understand why the work isn't just
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
enter code here I have a table on SQL server 2005 with bigint primary
The code here is X++. I know very little about it, though I am
enter code here Hi All, I have a simple windows service application that connects

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.