ICS

Write an if-else statement and a short-hand if-else statement to check if a
number is even or odd and print the appropriate message. in python

Solution

num = int(input(“Enter a number: “))

if num % 2 == 0:
print(“The number is Even”)
else:
print(“The number is Odd”)

Short-hand if-else statement

num = int(input(“Enter a number: “))
print(“Even” if num % 2 == 0 else “Odd”)

Write an if-elif-else statement to check if a number is positive, negative, or zero

num = int(input(“Enter a number: “))

if num > 0:
print(“The number is Positive”)
elif num < 0:
print(“The number is Negative”)
else:
print(“The number is Zero”)

Write a Python program that print even and count the odd numbers from 1 to
20 using a while loop.

i = 1
odd_count = 0

while i <= 20:
if i % 2 == 0:
print(i, “is Even”)
else:
odd_count += 1
i += 1

print(“Total odd numbers between 1 and 20:”, odd_count)

  1. Write a for loop using range() to print the even numbers from 2 to 10.
  2. Write a Python program that prints the first 10 multiples of 3 using a for
    loop and the range() function.

for i in range(2, 11, 2):
print(i)

for i in range(1, 11):
print(3 * i)