I need some help on my program. By using recursion, i have to write a program that give the output in vertical order... e.g. if i input 1234, the ouput will be:
1
2
3
4
I don't know how the structure works, and i have no idea how to begin...
This is a discussion on Recursion within the C++ Programming forums, part of the General Programming Boards category; I need some help on my program. By using recursion, i have to write a program that give the output ...
I need some help on my program. By using recursion, i have to write a program that give the output in vertical order... e.g. if i input 1234, the ouput will be:
1
2
3
4
I don't know how the structure works, and i have no idea how to begin...
hint: you will need '/' and '%' operators.
'/' will chop the number down by tenths.
1234 / 10 = 123 (note, really = 123.4, but .4 chopped off since dealing with int here)
'%' will extract the right most digit from the number.
1234 % 10 = 4.
just add some recursion and you solved it!
This is a program which is backwards.. how can i reverse it?
#include <iostream>
using namespace std;
void printForwards( int );
int main( void )
{
int num;
cout << "Please enter a positive integer: " << endl;
cin >> num;
cout << "Testing printForwards" << endl;
printForwards( num );
return 0;
}
void printForwards( int num )
{
if( num > 0 )
{
cout << ( num % 10 ) << endl;
printForwards( num / 10 );
}
}
Study the difference between
cout << ( num % 10 ) << endl;
printForwards( num / 10 );
and
PrintForwards( num / 10 );
cout << ( num % 10 ) << endl;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
thank u for ur help!