EXERCISE 3
1.write a program to find the grade of the student using conditional statements in
python
def get_grade(score):
if score >= 90:
return 'A'
elif score >= 80:
return 'B'
elif score >= 70:
return 'C'
elif score >= 60:
return 'D'
else:
return 'F'
# Initialize lists to store student names and grades
student_names = []
student_grades = []
# Loop to collect names and scores for 20 students
for i in range(2):
name = input(f"Enter the name of student {i + 1}: ")
score = float(input(f"Enter the score of {name}: "))
grade = get_grade(score)
student_names.append(name)
student_grades.append(grade)
# Display the names and grades
print("\nStudent Grades:")
for i in range(2):
print(f"{student_names[i]}: {student_grades[i]}")
2. write a code to get the age of a person and print an age group (infant, children,
adolescents, adults, older adults) using conditional statements in python
# Function to determine age group based on age
def get_age_group(age):
if age < 2:
return 'Infant'
elif age <= 12:
return 'Children'
elif age <= 18:
return 'Adolescent'
elif age <= 64:
return 'Adult'
else:
return 'Older Adult'
# Get the person's age
age = int(input("Enter the person's age: "))
# Determine the age group
age_group = get_age_group(age)
# Print the age group
print(f"The age group for a {age} year old is: {age_group}")
EXERCISE 4
1. write a program to get a number from an user and print countdown from that
number to zero using basic control structures and loops
# Get a number from the user
number = int(input("Enter a number to start the countdown: "))
# Print the countdown
while number >= 0:
print(number)
number -= 1
2. write python program to check prime number using while loop
num = int(input("Enter a number: "))
if num < 2:
print(f"{num} is not a prime number.")
else:
i=2
is_prime = True
while i * i <= num:
if num % i == 0:
is_prime = False
break
i += 1
if is_prime:
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
3.write a program to print pascal triangle using for loop in python
def print_pascals_triangle(rows):
for i in range(rows):
num = 1
for j in range(i + 1):
print(num, end=" ")
num = num * (i - j) // (j + 1) # Compute next binomial coefficient
print() # Move to the next line
# Set number of rows
n = int(input("Enter the number of rows: "))
print_pascals_triangle(n)
4. write a program to print the multiplication table of the number entered by user
# Get a number from the user
number = int(input("Enter a number to print its multiplication table: "))
# Print the multiplication table using a for loop
print(f"Multiplication table of {number}:")
for i in range(1, 11):
print(f"{number} x {i} = {number * i}")