输入一个三位数,要求把这个数的百位数与个位数对调,输出对调后的数。C++代码
时间: 2023-04-04 22:01:41 浏览: 240
很高兴回答你的问题。以下是 C 代码实现:
```
#include <stdio.h>
int main() {
int num, hundreds, tens, ones, new_num;
printf("请输入一个三位数:");
scanf("%d", &num);
hundreds = num / 100;
tens = (num / 10) % 10;
ones = num % 10;
new_num = ones * 100 + tens * 10 + hundreds;
printf("对调后的数为:%d\n", new_num);
return ;
}
```
希望能够帮助到你。如果你还有其他问题,欢迎继续提问。
相关问题
输入一个三位数,要求把这个数的百位数与个位数对调,输出对调后的数代码
num = int(input("请输入一个三位数:"))
a = num // 100 # 求百位数
b = (num // 10) % 10 # 求十位数
c = num % 10 # 求个位数
new_num = c * 100 + b * 10 + a # 对调百位数和个位数
print("对调后的数为:", new_num)
C语言输入一个三位自然数,然后把这个数的百位数与个位数对调,输出对调后的数
好的,您可以使用以下代码实现这个功能:
```
#include<stdio.h>
int main() {
int num, hundreds, tens, ones, result;
printf("请输入一个三位自然数: ");
scanf("%d", &num);
hundreds = num / 100;
tens = (num / 10) % 10;
ones = num % 10;
result = ones * 100 + tens * 10 + hundreds;
printf("对调后的数为: %d\n", result);
return 0;
}
```
您只需要输入一个三位自然数,程序会自动计算出其百位数、十位数和个位数,然后将百位数和个位数进行对调,最后输出对调后的数即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)