题目:将一个四位数,反向输出。
(针对每组输入,反向输出对应四位数。)
示例
输入: 1234
输出: 4321
方法一:可以利用求余运算然后每次对10求商即可
#include <stdio.h>
int main() {
int n = 0;
scanf(“%d”, &n);
//利用求余获取a的最后一位,以此类推
printf(“%d”, n% 10);
n /= 10;
printf(“%d”, n% 10);
n /= 10;
printf(“%d”, n% 10);
n /= 10;
printf(“%d\n”, n);
return 0;
}
方法二:在方法一的基础之上加上while循环
#include<stdio.h>
int main()
{
int n = 0;
scanf(“%d”, &n);
while(n)
{
printf(“%d”, n%10);
n = n/10;
}
return 0;
}
方法三: 将数字以字符串读入,只要输出字符串后四位即可
#include <stdio.h>
int main() {
char str[10];
scanf(“%s”, str);
printf(“%c%c%c%c\n”, str[3], str[2], str[1], str[0]);
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/87406.html