输入一个三位数,要求把这个数的百位数与个位数对调,输出对调后的数代码
时间: 2024-06-05 18:06:18 浏览: 6
num = int(input("请输入一个三位数:"))
a = num // 100 # 求百位数
b = (num // 10) % 10 # 求十位数
c = num % 10 # 求个位数
new_num = c * 100 + b * 10 + a # 对调百位数和个位数
print("对调后的数为:", new_num)
相关问题
输入一个三位数,要求把这个数的百位数与个位数对调,输出对调后的数。C++代码
很高兴回答你的问题。以下是 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 ;
}
```
希望能够帮助到你。如果你还有其他问题,欢迎继续提问。
C语言输入一个三位自然数,然后把这个数的百位数与个位数对调,输出对调后的数
好的,您想要交换一个三位数的百位数和个位数。下面是相应的 C 语言代码:
```
#include <stdio.h>
int main() {
int num, hundreds_digit, ones_digit, result;
printf("请输入一个三位自然数:");
scanf("%d", &num);
hundreds_digit = num / 100;
ones_digit = num % 10;
result = num - hundreds_digit * 100 - ones_digit + ones_digit * 100 + hundreds_digit;
printf("对调后的数为:%d\n", result);
return 0;
}
```
您可以将此代码复制并粘贴到您的 C 语言编辑器中,并将其编译运行。然后,您可以输入一个三位数,程序将输出对调后的数。
相关推荐
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)