Program 1: Sum of Two Numbers
Question: Write a Python program to calculate the sum of two numbers.
# Program to calculate the sum of two numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
sum_result = num1 + num2
print(f"The sum of {num1} and {num2} is {sum_result}.")
Example Output:
Enter the first number: 12
Enter the second number: 8
The sum of 12.0 and 8.0 is 20.0
Program 2: Find the Average of Three Numbers
Question : Write a Python program to calculate the average of three
numbers.
Ans
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
average = (num1 + num2 + num3) / 3
print(f"The average of {num1}, {num2}, and {num3} is {average}.")
Example Output:
Enter the first number: 10
Enter the second number: 20
Enter the third number: 30
The average of 10.0, 20.0, and 30.0 is 20.0.
Program 3: Check Even or Odd
Question: Write a Python program to check if a number is even or odd.
# Program to check if a number is even or odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print(f"{num} is an even number.")
else:
print(f"{num} is an odd number.")
Example Output:
Enter a number: 15
15 is an odd number.
Program 4: Find the Largest of Three Numbers
Question: Write a Python program to find the largest of three numbers.
# Program to find the largest of three numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
largest = max(num1, num2, num3)
print(f"The largest number among {num1}, {num2}, and {num3} is
{largest}.")
Example Output:
Enter the first number: 25
Enter the second number: 12
Enter the third number: 45
The largest number among 25.0, 12.0, and 45.0 is 45.0.
Program 5: Print Multiplication Table
Question: Write a Python program to display the multiplication table of a
given number.
# Program to display the multiplication table
num = int(input("Enter a number: "))
print(f"Multiplication table for {num}:")
for i in range(1, 11):
print(f"{num} × {i} = {num * i}")
Example Output:
Enter a number: 5
Multiplication table for 5:
5×1=5
5 × 2 = 10
...
5 × 10 = 50
Program 6: Find Factorial
Question: Write a Python program to calculate the factorial of a number.
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print(f"The factorial of {num} is {factorial}.")
Example Output:
Enter a number: 5
The factorial of 5 is 120.
Program 7: Check if a Number is Positive, Negative, or Zero
Question: Write a Python program to check whether a number is positive,
negative, or zero.
num = float(input("Enter a number: "))
if num > 0:
print(f"{num} is positive.")
elif num < 0:
print(f"{num} is negative.")
else:
print("The number is zero.")
Example Output:
Enter a number: -7
-7.0 is negative.
Program 8: Reverse a String
Question: Write a Python program to reverse a string entered by the user.
# Program to reverse a string
text = input("Enter a string: ")
reversed_text = text[::-1]
print(f"The reversed string is: {reversed_text}")
Example Output:
Enter a string: python
The reversed string is: nohtyp
Program 9: Convert Celsius to Fahrenheit
Question: Write a Python program to convert temperature from Celsius to
Fahrenheit.
# Program to convert Celsius to Fahrenheit
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit}°F.")
Example Output:
Enter temperature in Celsius: 25
25.0°C is equal to 77.0°F.
Program 10: Check Leap Year
Question: Write a Python program to check if a given year is a leap year.
# Program to check leap year
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Example Output:
Enter a year: 2024
2024 is a leap year.