Python Coding Questions with Solutions
1. Write a Python program to add two numbers entered by the user.
Solution:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Sum =", num1 + num2)
2. Write a program to check whether a number is even or odd using if-else.
Solution:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
3. Write a program to find the largest of three numbers using if-else.
Solution:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
print("Largest:", a)
elif b >= a and b >= c:
print("Largest:", b)
else:
print("Largest:", c)
4. Write a Python program to find the factorial of a number using a loop.
Solution:
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial:", fact)
5. Write a program to display the multiplication table of a number using a for loop.
Solution:
num = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
6. Write a Python program to calculate the sum of first N natural numbers using a while loop.
Solution:
n = int(input("Enter N: "))
sum = 0
i = 1
while i <= n:
sum += i
i += 1
print("Sum =", sum)
7. Write a Python program to check if a number is positive, negative or zero.
Solution:
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
8. Write a Python program to find the sum of digits of a number.
Solution:
num = int(input("Enter a number: "))
sum = 0
while num > 0:
sum += num % 10
num //= 10
print("Sum of digits:", sum)
9. Write a Python program to check whether a number is a palindrome or not.
Solution:
num = int(input("Enter a number: "))
rev = 0
temp = num
while temp > 0:
rev = rev * 10 + temp % 10
temp //= 10
if num == rev:
print("Palindrome")
else:
print("Not Palindrome")
10. Write a Python program to print all even numbers between 1 and 50 using a loop.
Solution:
for i in range(1, 51):
if i % 2 == 0:
print(i, end=' ')