输入一个整数,将他的逆序刷出。要求定义并调用函数reverse(number).如reverse(12345)的返回值是54321.

急需!谢谢!跪求!要详细的 我新手.
2024-12-22 15:25:05
推荐回答(2个)
回答1:

#include "stdio.h"

int reverse(int number)
{
int res = 0;
while(number)
{
res = res * 10 + number % 10;
number /= 10;
}

return res;
}

void main()
{
int num;
printf("input a number : ");
scanf("%d", &num);

printf("before reverse : %d\n", num);
printf("after reverse : %d\n", reverse(num));
}

回答2:

function y = reverse(number)

yy = num2str(number);
yy_len = length(yy);
for i = 1:yy_len
yy1(i) = yy(yy_len-i+1);
end
y = str2num(yy1);

在命令窗口输入
y = reverse(123)
得到
y=321