#include<iostream.h>
#include<conio.h>
using namespace std;
int re(int);
int main()
{
 int a, asd;
 cout<<"enter number\n";
 cin>>a;
 asd=re(a);
 cout<<asd;
 getch();
 return 0;   
}
int re(int a)
{
    int b;
 if(a==1)
 return ( 1 ) ; 
 else
  b=re(a-1);
  return (b);  
}

Hey, i was just trying to understand the concept of recursive funcition from the book of let us C by yashwant kanetkar.
Anyway, as far i understood ( i may be wrong) recursive function calls itself , and results as a loop like structure.
So in the above program, i want a username to input his no....
Then the program should print all the numbers from , the number itself to one. In decreasing order.
According to my logic, the number will get subtracted by one in each recursion the number should be subtracted by 1, and then it should be printed.
But, when the output has become 1, it should return 1 only.
For eg: when the username inputs 5.
The output should be:
54321
But, when i use the same theory to make a program to find out the factorial.
It works fine. I just multiply the number with its one decreased number.
for eg:

#include<iostream.h>
#include<conio.h>
using namespace std;
int re(int);
int main()
{
 int a, asd;
 cout<<"enter number\n";
 cin>>a;
 asd=re(a);
 cout<<asd;
 getch();
 return 0;   
}
int re(int a)
{
    int b;
    if(a==1)
    {
            return 1;
            }
            else
            {
 b=a*re(a-1);
 return b;  
}
}

Can you tell me where i am wrong. I guess in the 1st program, the number gets printed only when the output is changed to 1. If this is the problem can you tell me how to fix it.
Thank You

Recommended Answers

All 6 Replies

Yes you are right.

Simply put, recursion is when a function calls itself. That is, in the course of the function definition there is a call to that very same function.

Now in the factorial program, b is a local variable of the re method. So each time the re method is called, the result is stored in a different variable than the previous re invocation.

Can you tell me where i am wrong, can you specify what is your problem ?

In the first program you wanted each recursion to output a result where as in the second recursion you just wanted the result of the whole recursion.

You have no output (cout) calls in your recursion in either case; this does not matter for the second program as you only want the overall result so it works. However in the first program you wanted an output at each stage of the recursion which doesn't happen because you didn't put in an output statement.

@banfa
i modified the program with this. It didnt work either.

#include<iostream.h>
#include<conio.h>
using namespace std;
int re(int);
int main()
{
 int a, asd;
 cout<<"enter number\n";
 cin>>a;
 re(a);
 getch();
 return 0;   
}
int re(int a)
{
    int b;
    if(a==1)
    {
            return 1;
            }
            else
            {
 b=re(a-1);
 cout<<b<<"\n";
 return b;  
}
}

Did i misinterpreted you?

@np complete..
I am not getting the desired output. Whatever no. the username gives. You always get 1 as an output.

you're displaying the number after it has been fully decreased, try decrasing the number first, display it then call the function again.

when you call the function from inside itself, it creates a new function of itself on the stack and starts the code from the top down all over again, when the base value is reached as you've defined at the top of the function it will return the base value and unwind the stack created from recursive calls.

i think a good way to look at it is as a loop where the top of the function is the start of the loop and the recursive call, where the function calls itself is the bottom of the loop, what you had done is put the display call outside of the loop so it will only get called once the recursion has finnished displaying 1 every time.

hope this helps.

-edit- no idea what you wanted "re" to return so it returns "nothing"..

#include<iostream>

using namespace std;

int re(int a)
{
    cout << a << endl;

    if (a > 0)
    {
        re(a - 1);
    }
}

int main()
{
     int a;

     cout<<"enter number\n";
     cin>>a;

     re(a);

     return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.