Uploaded by marzanatwa

Practice Case 20 - Displaying a number vertically using a recursive function

advertisement
/******************************************************************************
The program takes a positive integer number and display it verticall
with each digit on a separate line. For example, if the number is 416, it should be displayed:
4
1
6
The program uses a recursive function displaly_vartical
*******************************************************************************/
#include <iostream>
using namespace std;
// function prototype
void display_vertical(int);
int main()
{
// variable declaration
int num;
// Input
cout << "Enter a positive integer number: ";
cin >> num;
// calling the function display_vertical
display_vertical(num);
return 0;
} // end of function main
// Defining the recursive function display_vertical
void display_vertical(int n)
{
if (n < 10)
cout << n << endl; // stoping case
else
// n is more than one digit
{
display_vertical(n / 10); // another call to the same function
cout << n % 10 << endl;
}
} // end of function display_vertical
// end of program
Download