print("Even numbers from 1 to 100:")
for num in range(1, 101):
if num % 2 == 0:
if num <= 50:
print(num, end=" ")
else:
if num == 52:
print()
print(num, end=" ")
#source code --> clcoding.com
Code Explanation:
1. Print Header
print("Even numbers from 1 to 100:")
This prints the message "Even numbers from 1 to 100:" before the numbers are displayed.
2. Loop Through Numbers 1 to 100
for num in range(1, 101):
The for loop runs from 1 to 100.
3. Check if the Number is Even
if num % 2 == 0:
If num is divisible by 2 (num % 2 == 0), it's an even number.
4. Print First Half (2 to 50) on First Line
if num <= 50:
print(num, end=" ")
If the even number is ≤ 50, it gets printed on the first line.
5. Break the Line at 52
if num == 52:
print() # Move to a new line
When num reaches 52, the program prints an empty line (print()), which moves the output to the next line.
6. Print Second Half (52 to 100) on Second Line
print(num, end=" ")
Numbers from 52 to 100 are printed on the second line.
0 Comments:
Post a Comment