一道关于逆序打印的问题

来源:百度知道 编辑:UC知道 时间:2024/06/03 11:06:35
题目如下:
Write a program to accept an integer from the user, and then print the integer in reverse order.The reversed print should also be a proper integer. For example, if the input is 453600, then the
program outputs 6354; if the input is -9867, then the output is -7689; if the input is 9030; then the output is 309; if the input is 0, then the output is 0. You are required to use a recursive
function when write the program.

注:还没学到字符串和数组,所以不要用~

代码如下,有问题hi我

#include<stdio.h>
void recursive(int n)
{
if(n==0) {printf("0\n");return;}
else if(n<0) {printf("-");n=-n;}
bool flag=true;
int digit;
while(n!=0)
{
digit=n%10;
if(digit!=0||(digit==0&&!flag))
{
printf("%d",digit);
flag=false;
}
n/=10;
}
printf("\n");
}
void main()
{
int n;
scanf("%d",&n);
recursive(n);
}

#include <iostream>

using namespace std;

int main()
{
int num,i=0;
cout <<"please enter a number:" <<endl;
cin >>num;
while(true)
{
i=i*10+num%10;
num=num/10;
if(num==0)break;
}
cout <<i <<endl;
system("pause");
return 0;