how to use recursion on this program.

I have made a very simple program and I am trying to make it more complex.
Can someone please tell me how to do this program by recursion and input should be numbers like 3422 not 3 4 2 2 .

1
2
3
4
5
6
7
8
9
10
#include<iostream>
using namespace std;
int main()
{
    int a,b,c,d,e,f;
    cout<<"enter the number ";
    cin>>a>>b>>c>>d>>e>>f;
    cout<<"the reverse number is "<<f<<e<<d<<c<<b<<a<<endl;
    return 0;
}
closed account (S6k9GNh0)
Well I don't think you can use recursion on the main function unless you make a separate function but recursion isn't exactly the most safe operation out there.

Also, programs aren't about complexity, it's about simplicity. If I ever make code, I avoid complex matters that make it difficult to read and / or hurt my performance.

Let's begin:
1. The problem with your algorithm is that it's hard coded. At all times there is a set number of six digits that you can enter and no other and clearly this isn't good. What you want is something that can take any number and be able to turn it around.

1
2
3
4
5
6
int main()
{
   int input;
   cout << "Please enter a number to be reversed." << endl;
   cin >> input;
}


After this, we must make an algorithm that reverses ANY number. This is a challenge question and with a little bit of research you should be able to figure this out.

I'd probably make a function for reuse of the code.

Last edited on
Sir, I did my research and couldn't do anything useful . now I post my problem here.
Can you give me some hints.
I have to make a recursive function to do it.
Last edited on
If you were writing out the number in the opposite way, how would you start?
I would start looking the last number first and then the second last and so on.
why not try making it into an array?
I dont think that will work.
here's a hint, you can find the last digit of a number by % it by 10, and get rid of the last digit by / it by 10, so it's just a matter of repeating the process till there is no number left.

do you mean something like this ?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
void vertical_number(int n)
{ if
(n<10){
cout<<n<<endl;
}else 
{ vertical_number(n/10);
cout<<n%10)<<endl;
}
}
int main()
{
cout<<"vertical"<<vertical_number(10);
cout<<"vertical"<<vertical_number(100);
return 0;
}

Last edited on
yes, but I think you'll want to output the digit before calling the function again.
closed account (S6k9GNh0)
Here's a method that takes a different approach:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int reverse(long num)
{
   long result=0,temp=num,k;
   int a=0,i;
   
   for(; temp/10!=0; temp/=10)   //finds the number or digits   
       a++;
 
   for(; num/10!=0; num/=10, a--)
     {
        k=num%10;
        for(i=0; i<a; i++)
          {
             k*=10;
          }
         result+=k;
     }
   return result;
} 


This isn't mine and was grabbed from http://www.dreamincode.net/code/snippet526.htm
I think I can use the algorithm he used to grab the number of digits. Really simple but it's quite a bit shorter than mine.
Last edited on
Hmm i made a program like this a while ago, this is the way i did it... maybe it will give you an idea. This is just the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void reverse(char str[], int cnt)
{
        if(cnt<=0) 
         {
           cout << "\n\nNo characters left";
           return;
         }
        else
         {
           cnt--; // string length -1 to get rid of null ending character and to reduce array element
           cout << str[cnt];
           reverse(str, cnt);
           return;
         }
}
Thank you All of you Guys !!
Last edited on
Topic archived. No new replies allowed.