输入一个三位数,对调
时间: 2024-06-13 15:08:48 浏览: 7
以下是两种C++语言实现输入一个三位数,对调的方法:
方法一:
```c++
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
int a = n % 10; //个位
int b = n / 10 % 10; //十位
int c = n / 100; //百位
cout << a * 100 + b * 10 + c;
return 0;
}
```
方法二:
```c++
#include<bits/stdc++.h>
using namespace std;
int main() {
int n,a,b,c;
cin>>n;
a=(n%100)%10;
b=n%100/10;
c=n/100;
if(a==0){
cout<<b<<c;
}else{
cout<<a<<b<<c;
}
return 0;
}
```
相关问题
用c语言输入一个三位数,将其个位与百位数字对调成为一个新的三位数
可以使用以下代码实现这个功能:
```c
#include <stdio.h>
int main() {
int num, new_num;
printf("请输入一个三位数:\n");
scanf("%d", &num);
new_num = (num % 10) * 100 + (num / 10 % 10) * 10 + (num / 100);
printf("将个位和百位数字对调后得到:%d\n", new_num);
return 0;
}
```
这个程序接受用户输入一个三位数,并将其个位和百位数字对调,得到一个新的三位数作为输出。
输入一个三位数,要求把这个数的百位数与个位数对调,输出对调后的数代码
num = int(input("请输入一个三位数:"))
a = num // 100 # 求百位数
b = (num // 10) % 10 # 求十位数
c = num % 10 # 求个位数
new_num = c * 100 + b * 10 + a # 对调百位数和个位数
print("对调后的数为:", new_num)
相关推荐
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)