while循环,输出下列数组中,偶数的数字; {100,8,23,44,65,78,101,17,666}
时间: 2023-10-04 14:11:39 浏览: 7
可以使用for循环来遍历数组,然后使用if语句判断每个数字是否为偶数,如果是就输出。代码如下:
```python
arr = [100, 8, 23, 44, 65, 78, 101, 17, 666]
for num in arr:
if num % 2 == 0:
print(num)
```
输出结果为:
```
100
8
44
78
666
```
相关问题
分别利用for循环与while循环,输出下列数组中,偶数的数字; {100,8,23,44,65,78,101,17,666}
使用for循环的代码如下:
```python
nums = [100, 8, 23, 44, 65, 78, 101, 17, 666]
for num in nums:
if num % 2 == 0:
print(num)
```
输出结果为:
```
100
8
44
78
666
```
使用while循环的代码如下:
```python
nums = [100, 8, 23, 44, 65, 78, 101, 17, 666]
i = 0
while i < len(nums):
if nums[i] % 2 == 0:
print(nums[i])
i += 1
```
输出结果为:
```
100
8
44
78
666
```
分别利用for循环与while循环,输出下列数组中,偶数的数字; {100,8,23,44,65,78,101,17,666}用java
使用for循环实现:
```java
int[] nums = {100, 8, 23, 44, 65, 78, 101, 17, 666};
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 2 == 0) {
System.out.println(nums[i]);
}
}
```
使用while循环实现:
```java
int[] nums = {100, 8, 23, 44, 65, 78, 101, 17, 666};
int i = 0;
while (i < nums.length) {
if (nums[i] % 2 == 0) {
System.out.println(nums[i]);
}
i++;
}
```
阅读全文