用C语言输入一三位正整数 逆序输出
时间: 2024-01-02 12:16:55 浏览: 71
好的,以下是C语言代码实现:
```c
#include <stdio.h>
#include <string.h>
int main() {
char num[4];
printf("请输入一个三位正整数:");
scanf("%s", num);
int len = strlen(num);
for (int i = len - 1; i >= 0; i--) {
printf("%c", num[i]);
}
printf("\n");
return 0;
}
```
这里使用了字符数组来存储输入的数字,并使用`strlen`函数获取数字的长度,然后倒序遍历输出每一位数字。
相关问题
用交换语句输入一三位正整数 逆序输出
好的,以下是C语言代码实现:
```c
#include <stdio.h>
int main() {
int num;
printf("请输入一个三位正整数:");
scanf("%d", &num);
int a = num / 100;
int b = num / 10 % 10;
int c = num % 10;
int temp;
temp = a;
a = c;
c = temp;
printf("%d\n", 100 * a + 10 * b + c);
return 0;
}
```
这里使用了整型变量来存储输入的数字,并通过除法和取模运算获取每一位数字,然后通过交换变量的值来实现逆序输出。
写一段C语言将一个正整数逆序输出
```c
#include <stdio.h>
int main() {
int n;
printf("请输入一个正整数:");
scanf("%d", &n);
while (n > 0) {
printf("%d", n % 10);
n /= 10;
}
return 0;
}
```
这段代码首先读入一个正整数,然后通过 % 运算符取出该数的个位数,再将该数除以 10,将其余位数赋值给 n,重复执行直到 n 变为 0。每次取出的个位数通过 printf 函数输出即可。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)