Given data = [2, 7, 5, 64, 14, 99], write a Python program to print all even numbers in the given list.
时间: 2024-05-30 12:14:46 浏览: 5
Sure, here is the Python program to print all even numbers in the given list:
data = [2, 7, 5, 64, 14, 99]
for num in data:
if num % 2 == 0:
print(num)
I hope this helps! And as for what you said earlier... Why did the tomato turn red? Because it saw the salad dressing!
相关问题
Write a program to print all digits of a given number by using a recursive function.
Here's the Python code to print all digits of a given number using a recursive function:
```
def print_digits(n):
if n < 10:
print(n)
else:
print_digits(n // 10)
print(n % 10)
# Example usage:
print_digits(12345) # Output: 1 2 3 4 5
```
In this code, the `print_digits` function takes an integer `n` as input. If `n` is less than 10, we simply print it. Otherwise, we first call the `print_digits` function recursively with `n // 10`, which gives us all the digits except for the last one. Then, we print the last digit by computing `n % 10`. This way, we print the digits in reverse order (from right to left).
Write a C program to reverse and print a given integer number whose absolute value greater than 99 and less than 1000.
Sure, here's the program:
```c
#include <stdio.h>
int main() {
int num, reversed_num = 0;
printf("Enter a number between 100 and 999: ");
scanf("%d", &num);
while (num != 0) {
int remainder = num % 10;
reversed_num = reversed_num * 10 + remainder;
num /= 10;
}
printf("Reversed number: %d", reversed_num);
return 0;
}
```
This program prompts the user to enter a number between 100 and 999, then uses a while loop to reverse the digits of the number. It does this by repeatedly taking the remainder of the number when divided by 10 (which gives the rightmost digit), adding that digit to the reversed number (which has been multiplied by 10 to shift its digits left), and dividing the original number by 10 to discard the rightmost digit. Finally, the program prints the reversed number.