Python Exercise No.
3
1. Write a program to check the largest among the three given numbers
# Program to find the largest among the three numbers
x = int(input(‘Enter the first number: ‘))
y = int(input(‘Enter the second number: ‘))
z = int(input(‘Enter the third number: ‘))
if (x>y) and (x>z):
large = x
elif (y>x) and (y>z):
large = y
else:
large = z
print(f “The largest among {} {} { } is {}’.format(x,y,z,large))
2. Program to check if the input year is a leap year
# A program to check whether the year is leap year or not
year = int(input('Enter the year :'))
if (year%4 == 0) and (year%400 == 0):
print(f'The year {year} is a leap year')
else:
print(f'The year {year} is not a leap year')
3. Program to display the factors of a given number
# Factors of a given number without using lists and functions
a = int(input('Enter a number to find it\'s factors : '))
for i in range(2,a+1):
if a%i == 0:
print(i, end = " ")
4. Program to find the sum and average of n natural numbers, where n is provided by
user
# Sum and Average of n natural numbers
num = int(input('Enter the number : '))
sum = 0
for i in range(num+1):
sum += i
average = sum/num
print(f'The sum of {num} natural numbers is {sum} and average is {average} ')
1
5. Program to find the sum of digits in a given number
# A program to find the sum of digits of a given number
num = int(input(‘Enter the number : ‘))
summation = 0
reminder = 0
while num!= 0:
remainder = num%10
summation = summation + reminder
num = int(num/10)
print(f’The sum of digits in the number {num} is {summation}’)
6. A program to print A-Z and z-a in two lines
# Program to write Capital letters and small letters in reverse order
print("Capital Letters\n")
for i in range(65,91):
print(chr(i), end = ' ')
print('\n')
print("Small letters in reverse \n")
for i in range(122,96,-1):
print(chr(i), end = ' ')
7. A program to find the factorial of a given number
# Program to find the factorial
num = int (input('Enter the numer : '))
fact = 1
if num < 0 :
print('Factorial not defined for negative numbers')
elif num == 1:
print('Factorial of one is 1')
else:
for i in range(2,num+1):
fact = fact*i
print(f' Factorial of number {num} is {fact}')