Page 255 - CTS - CSA TP - Volume 2
P. 255
COMPUTER SOFTWARE APPLICATION - CITS
TASK 2: If-Else Statement
Code:
# Program to check if a number is even or odd
num = int(input(“Enter a number: “))
ifnum % 2 == 0:
print(“The number is even.”)
else:
print(“The number is odd.”)
Here, the program checks if the entered number is even or odd. If the remainder when divided by 2 is 0, it prints
a message indicating that the number is even. Otherwise, it prints a message indicating that the number is odd.
Output:
TASK 3: Nested if Statements
Code:
# Simple Python Program to print the largest of the three numbers.
# Taking user input for three numbers
a = int(input(“Enter a: “))
b = int(input(“Enter b: “))
c = int(input(“Enter c: “))
# Checking if ‘a’ is the largest
if a > b and a > c:
# If the condition is true, we will enter this block
print(“From the above three numbers, given ‘a’ is the largest”)
# Checking if ‘b’ is the largest
if b > a and b > c:
# If the condition is true, we will enter this block
print(“From the above three numbers, given ‘b’ is the largest”)
# Checking if ‘c’ is the largest
if c > a and c > b:
# If the condition is true, we will enter this block
print(“From the above three numbers, given ‘c’ is the largest”)
240
CITS : IT & ITES - Computer Software Application - Exercise 125