从键盘输入一个三位整数x,分别求十位数,百位数,个位数
时间: 2023-05-25 08:06:24 浏览: 81
```
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:");
scanf("%d%d%d", &a, &b, &c);
(a % 2 == 0) + (b % 2 == 0) + (c % 2 == 0) == 2 ? printf("有两个偶数一个奇数\n") : printf("没有两个偶数一个奇数\n");
return 0;
}
```
解释:
- 表达式 `(a % 2 == 0)` 可以返回一个值,如果 `a` 是偶数,返回 `1`,否则返回 `0`;
- 通过类似的方式,对 `b` 和 `c` 也进行求余运算;
- 利用 C 语言中的隐式转换,将上述结果相加,得到结果为 `2`,则表明有两个偶数一个奇数,否则表明没有两个偶数一个奇数。
相关问题
从键盘输入一个三位整数,将百位,十位和个位数分别显示出来
好的,以下是Java程序代码,可以从键盘输入一个三位整数,将百位,十位和个位数分别显示出来:
```java
import java.util.Scanner;
public class ThreeDigitNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个三位整数:");
int number = scanner.nextInt();
int hundredsDigit = number / 100; // 获取百位数
int tensDigit = (number / 10) % 10; // 获取十位数
int onesDigit = number % 10; // 获取个位数
System.out.println("百位数为:" + hundredsDigit);
System.out.println("十位数为:" + tensDigit);
System.out.println("个位数为:" + onesDigit);
}
}
```
程序中使用了Java中的Scanner类来从键盘读取输入的整数,然后通过数学运算来获取百位、十位和个位数。其中获取十位数的方法使用了取模运算符,因为十位数位于整数的第二位。最后,程序将获取到的百位、十位和个位数分别输出。
运行时从键盘输入一个3位数整数 输出个十百位,每位占3个字符
n = int(input("请输入一个三位数整数:"))
hundreds = n // 100 # 取百位数
tens = n % 100 // 10 # 取十位数
ones = n % 10 # 取个位数
# 输出结果
print("{:3d}".format(hundreds), "{:3d}".format(tens), "{:3d}".format(ones))
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)