在控制台输入两个数,统计这两个数之间偶数的个数
时间: 2023-02-08 20:45:43 浏览: 70
你可以使用 Python 来实现这个需求。下面是一个简单的例子:
```python
start = int(input("请输入第一个数: "))
end = int(input("请输入第二个数: "))
count = 0
for i in range(start, end+1):
if i % 2 == 0:
count += 1
print("在这两个数之间偶数的个数是:", count)
```
这段代码将会在控制台提示输入两个数,然后统计第一个数与第二个数之间偶数的个数,最后输出结果
相关问题
Java实现在控制台输入两个数,统计这两个数之间偶数的个数
使用Java实现在控制台输入两个数,统计这两个数之间偶数的个数的代码如下:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入第一个数:");
int num1 = scan.nextInt();
System.out.print("请输入第二个数:");
int num2 = scan.nextInt();
int start = Math.min(num1, num2);
int end = Math.max(num1, num2);
int count = 0;
for (int i = start; i <= end; i++) {
if (i % 2 == 0) {
count++;
}
}
System.out.println("这两个数之间的偶数个数是:" + count);
}
}
```
说明:使用了Scanner类来读取控制台输入的两个数,Math.min()和Math.max()函数来获取这两个数中的较大值和较小值,并通过for循环来遍历这两个数之间的数,统计偶数的个数。
python 在控制台输入两个数,输出这两个数的最小公倍数
你好,这个问题可以使用 Python 编写程序来解决。以下是一个示例代码:
```
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
def lcm(a, b):
return a * b // gcd(a, b)
num1 = int(input("请输入第一个数:"))
num2 = int(input("请输入第二个数:"))
print("最小公倍数为:", lcm(num1, num2))
```
希望能对你有所帮助。