题目:
编写一个递归函数reverse,它有一个整型参数。reverse函数按逆序打印出参数的值。例如,参数值为12345时,函数打印出54321。
代码:
#include <iostream>
using namespace std;
void reverse(int n);
int main()
{
int n;
cout << "请输入一个非负整数:";
cin >> n;
cout << endl << "它的逆序为";
reverse(n);
cout << endl << endl;
system("pause");
return 0;
}
void reverse(int n)
{
if (n > 0)
{
cout << n % 10;
n = n / 10;
reverse(n);
}
}