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>
usingnamespace 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;
}
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.
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.
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.
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.