Number Problems
Problem 1 Write a program to find the factorial of a number. ▾
Question breakdown
Factorial means multiplying all numbers from 1 to n.
View Solution
fact = 1
i = 1
num = 5
while i <= num:
fact *= i
i += 1
print(fact)
Understand the Logic
# Example Input
num = 5
# Initial values
fact = 1
i = 1
# Loop runs until i <= num
# --------------------------------
# Iteration 1:
# fact = 1 * 1 = 1
# i = 2
# Iteration 2:
# fact = 1 * 2 = 2
# i = 3
# Iteration 3:
# fact = 2 * 3 = 6
# i = 4
# Iteration 4:
# fact = 6 * 4 = 24
# i = 5
# Iteration 5:
# fact = 24 * 5 = 120
# i = 6
# Loop stops because i <= num becomes False
# Final Output:
# 120
# Factorial Formula:
# 5! = 1 × 2 × 3 × 4 × 5 = 120
Problem 2 Write a Python program to check whether a number is divisible by 3, 5, or both. ▾
Question breakdown
A number is divisible if the remainder becomes 0.
View Solution
n = int(input("enter number="))
if n % 3 == 0 and n % 5 == 0:
print(n, "is divisible by 3 and 5")
elif n % 5 == 0:
print(n, "is divisible by only 5")
elif n % 3 == 0:
print(n, "is divisible by only 3")
else:
print(n, "is neither divisible by 3 and nor 5")
Understand the Logic
# Example Input
n = 9
# Step 1:
# 9 % 3 == 0 → True
# 9 % 5 == 0 → False
# Output:
# 9 is divisible by only 3
Problem 3 Write a Python program to reverse a number. ▾
Question breakdown
Reverse means printing the digits in opposite order.
Example:
1234 → 4321
View Solution
n = int(input("enter number="))
rev = 0
n1 = n
while n > 0:
rem = n % 10
n = n // 10
rev = rem + (rev * 10)
print(f"Reverse of {n1}={rev}")
Understand the Logic
# Example Input
n = 1234
# Initial values
rev = 0
# --------------------------------
# Iteration 1:
# rem = 1234 % 10 = 4
# rev = 0 * 10 + 4 = 4
# n = 1234 // 10 = 123
# Iteration 2:
# rem = 123 % 10 = 3
# rev = 4 * 10 + 3 = 43
# n = 123 // 10 = 12
# Iteration 3:
# rem = 12 % 10 = 2
# rev = 43 * 10 + 2 = 432
# n = 12 // 10 = 1
# Iteration 4:
# rem = 1 % 10 = 1
# rev = 432 * 10 + 1 = 4321
# n = 1 // 10 = 0
# Loop stops because n > 0 becomes False
# Final Output:
# 4321
Problem 4 Write a Python program to check whether a number is palindrome or not. ▾
Question breakdown
A palindrome number remains the same after reversing.
Example:
121 → 121
1331 → 1331
View Solution
n = int(input("enter number="))
rev = 0
n1 = n
while n > 0:
rem = n % 10
n = n // 10
rev = rem + (rev * 10)
if rev == n1:
print(f"{n1} is palindrome")
else:
print(f"{n1} is not palindrome")
Understand the Logic
# Example Input
n = 121
# Initial values
rev = 0
n1 = 121
# --------------------------------
# Iteration 1:
# rem = 121 % 10 = 1
# rev = 0 * 10 + 1 = 1
# n = 121 // 10 = 12
# Iteration 2:
# rem = 12 % 10 = 2
# rev = 1 * 10 + 2 = 12
# n = 12 // 10 = 1
# Iteration 3:
# rem = 1 % 10 = 1
# rev = 12 * 10 + 1 = 121
# n = 1 // 10 = 0
# Loop stops because n > 0 becomes False
# Final Comparison:
# rev == n1
# 121 == 121 → True
# Output:
# 121 is palindrome
Problem 5 Write a Python program to check whether a number is an Armstrong number or not. ▾
Question breakdown
An Armstrong number is a number where the sum of each digit raised to the power of total digits is equal to the original number.
Example:
153
Total digits = 3
1³ + 5³ + 3³
= 1 + 125 + 27
= 153
So:
153 is an Armstrong number
View Solution
n = int(input("enter number="))
digitcount = 0
n1 = n
while n > 0:
n = n // 10
digitcount += 1
print(f"Count of digits of {n1}={digitcount}")
ans = 0
n2 = n1
while n1 > 0:
rem = n1 % 10
n1 = n1 // 10
ans = ans + (rem**digitcount)
if ans == n2:
print(f"{n2} is armstrong number")
else:
print(f"{n2} is not armstrong number")
Understand the Logic
# Example Input
n = 153
# Initial values
digitcount = 0
n1 = 153
# --------------------------------
# Step 1: Count digits
# Iteration 1:
# 153 // 10 = 15
# digitcount = 1
# Iteration 2:
# 15 // 10 = 1
# digitcount = 2
# Iteration 3:
# 1 // 10 = 0
# digitcount = 3
# Total digits = 3
# --------------------------------
# Step 2: Calculate Armstrong sum
# Initial:
# ans = 0
# n1 = 153
# Iteration 1:
# rem = 3
# ans = 0 + (3^3)
# ans = 27
# Iteration 2:
# rem = 5
# ans = 27 + (5^3)
# ans = 152
# Iteration 3:
# rem = 1
# ans = 152 + (1^3)
# ans = 153
# --------------------------------
# Final Comparison:
# ans == original number
# 153 == 153 → True
# Output:
# 153 is Armstrong number
Problem 6 Write a Python program to check whether a number is prime or not. ▾
Question breakdown
A prime number has only 2 factors:
1 and itself.
Example:
7 → factors are 1 and 7 only
So:
7 is a prime number
View Solution
n = int(input("enter number="))
flag = False
if n == 1:
print(f"{n} is not a prime number")
else:
for i in range(2, n):
if n % i == 0:
flag = True
break
if flag:
print(f"{n} is not a prime number")
else:
print(f"{n} is a prime number")
Understand the Logic
# Example Input
n = 7
# Initial value
flag = False
# Step 1:
# Check if n == 1
# 7 == 1 → False
# --------------------------------
# Loop starts:
# for i in range(2, 7)
# Iteration 1:
# i = 2
# 7 % 2 == 0 → False
# Iteration 2:
# i = 3
# 7 % 3 == 0 → False
# Iteration 3:
# i = 4
# 7 % 4 == 0 → False
# Iteration 4:
# i = 5
# 7 % 5 == 0 → False
# Iteration 5:
# i = 6
# 7 % 6 == 0 → False
# No number divides 7 completely
# flag remains False
# --------------------------------
# Final Check:
# flag == False
# Output:
# 7 is a prime number
Problem 7 Write a Python program to check whether a number is a Perfect Number or not. ▾
Question breakdown
A perfect number is a number where the sum of its factors
(excluding itself) is equal to the number.
Example:
6 → factors are 1, 2, 3
1 + 2 + 3 = 6
So:
6 is a perfect number
View Solution
n = int(input("enter number="))
sum = 0
for i in range(1, n):
if n % i == 0:
sum = sum + i
if sum == n:
print(f"{n} is perfect number")
else:
print(f"{n} is not perfect number")
Understand the Logic
# Example Input
n = 6
# Initial value
sum = 0
# Loop:
# for i in range(1, 6)
# --------------------------------
# Iteration 1:
# i = 1
# 6 % 1 == 0 → True
# sum = 0 + 1 = 1
# Iteration 2:
# i = 2
# 6 % 2 == 0 → True
# sum = 1 + 2 = 3
# Iteration 3:
# i = 3
# 6 % 3 == 0 → True
# sum = 3 + 3 = 6
# Iteration 4:
# i = 4
# 6 % 4 == 0 → False
# Iteration 5:
# i = 5
# 6 % 5 == 0 → False
# --------------------------------
# Final Comparison:
# sum == n
# 6 == 6 → True
# Output:
# 6 is perfect number
Problem 8 Write a Python program to print the Fibonacci series. ▾
Question breakdown
In Fibonacci series, each number is the sum of previous two numbers.
Example:
0 1 1 2 3 5 8 13
Because:
0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5
View Solution
num = int(input("ENter a number of series : "))
a = 0
b = 1
i = 1
print(f"{a}\n{b}")
while num > i:
c = a + b
print(c)
a = b
b = c
i += 1
Understand the Logic
# Example Input
num = 5
# Initial values
a = 0
b = 1
i = 1
# First output:
# 0
# 1
# --------------------------------
# Iteration 1:
# c = a + b
# c = 0 + 1 = 1
# print(1)
# a = b → 1
# b = c → 1
# i = 2
# --------------------------------
# Iteration 2:
# c = 1 + 1 = 2
# print(2)
# a = 1
# b = 2
# i = 3
# --------------------------------
# Iteration 3:
# c = 1 + 2 = 3
# print(3)
# a = 2
# b = 3
# i = 4
# --------------------------------
# Iteration 4:
# c = 2 + 3 = 5
# print(5)
# a = 3
# b = 5
# i = 5
# Loop stops because num > i becomes False
# Final Output:
# 0 1 1 2 3 5
Problem 9 Write a Python program to count even and odd numbers in a given range. ▾
Question breakdown
Even numbers are divisible by 2.
Odd numbers are not divisible by 2.
Example:
2, 4, 6 → Even
1, 3, 5 → Odd
View Solution
start = int(input("enter start number="))
end = int(input("enter end number="))
even = 0
odd = 0
for i in range(start, end + 1):
if i % 2 == 0:
even = even + 1
else:
odd = odd + 1
print("Even numbers count=", even)
print("Odd numbers count=", odd)
Understand the Logic
# Example Input
start = 1
end = 5
# Initial values
even = 0
odd = 0
# Loop:
# for i in range(1, 6)
# --------------------------------
# Iteration 1:
# i = 1
# 1 % 2 == 0 → False
# odd = 0 + 1 = 1
# --------------------------------
# Iteration 2:
# i = 2
# 2 % 2 == 0 → True
# even = 0 + 1 = 1
# --------------------------------
# Iteration 3:
# i = 3
# 3 % 2 == 0 → False
# odd = 1 + 1 = 2
# --------------------------------
# Iteration 4:
# i = 4
# 4 % 2 == 0 → True
# even = 1 + 1 = 2
# --------------------------------
# Iteration 5:
# i = 5
# 5 % 2 == 0 → False
# odd = 2 + 1 = 3
# --------------------------------
# Final Output:
# Even numbers count = 2
# Odd numbers count = 3
Problem 10 Write a Python program to count the digits in a number ▾
Question breakdown
Digit count means counting how many digits are present in a number.
Example:
12345 → 5 digits
View Solution
n = int(input("Enter number: "))
digitcount = 0
while n > 0:
n = n // 10
digitcount += 1
print("Total digits =", digitcount)
Understand the Logic
# Example Input
n = 12345
# Initial value
digitcount = 0
# --------------------------------
# Iteration 1:
# n = 12345 // 10 = 1234
# digitcount = 1
# Iteration 2:
# n = 1234 // 10 = 123
# digitcount = 2
# Iteration 3:
# n = 123 // 10 = 12
# digitcount = 3
# Iteration 4:
# n = 12 // 10 = 1
# digitcount = 4
# Iteration 5:
# n = 1 // 10 = 0
# digitcount = 5
# Loop stops because n > 0 becomes False
# Final Output:
# 5
Problem 11 Write a Python program to find the sum of digits of a number ▾
Question breakdown
Sum of digits means adding all digits present in a number.
Example:
123 → 1 + 2 + 3 = 6
View Solution
n = int(input("Enter number: "))
sum = 0
while n > 0:
rem = n % 10
sum = sum + rem
n = n // 10
print("Sum of digits =", sum)
Understand the Logic
# Example Input
n = 123
# Initial value
sum = 0
# --------------------------------
# Iteration 1:
# rem = 123 % 10 = 3
# sum = 0 + 3 = 3
# n = 123 // 10 = 12
# Iteration 2:
# rem = 12 % 10 = 2
# sum = 3 + 2 = 5
# n = 12 // 10 = 1
# Iteration 3:
# rem = 1 % 10 = 1
# sum = 5 + 1 = 6
# n = 1 // 10 = 0
# Loop stops because n > 0 becomes False
# Final Output:
# 6
Problem 12 Write a Python program to swap two numbers. ▾
Question breakdown
Swapping means exchanging the values of two variables.
Example:
Before swapping:
a = 10
b = 20
After swapping:
a = 20
b = 10
View Solution
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
temp = a
a = b
b = temp
print("After swapping:")
print("a =", a)
print("b =", b)
Understand the Logic
# Example Input
a = 10
b = 20
# Initial values
# a = 10
# b = 20
# --------------------------------
# Step 1:
# temp = a
# temp = 10
# Step 2:
# a = b
# a = 20
# Step 3:
# b = temp
# b = 10
# --------------------------------
# Final Output:
# a = 20
# b = 10
Problem 13 Write a Python program to find the largest among three numbers ▾
Question breakdown
Compare all three numbers and print the greatest one.
Example:
10, 25, 15
25 is largest
View 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(a, "is largest")
elif b > a and b > c:
print(b, "is largest")
else:
print(c, "is largest")
Understand the Logic
# Example Input
a = 10
b = 25
c = 15
# --------------------------------
# Step 1:
# Check:
# a > b and a > c
# 10 > 25 → False
# Step 2:
# Check:
# b > a and b > c
# 25 > 10 → True
# 25 > 15 → True
# Condition becomes True
# Output:
# 25 is largest
Problem 14 Write a Python program to check whether a year is a leap year or not ▾
Question breakdown
A leap year has 366 days.
Rule:
A year is a leap year if:
1. It is divisible by 400
OR
2. It is divisible by 4 but not divisible by 100
View Solution
year = int(input("Enter year: "))
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
print(year, "is leap year")
else:
print(year, "is not leap year")
Understand the Logic
# Example Input
year = 2024
# --------------------------------
# Step 1:
# 2024 % 400 == 0
# False
# Step 2:
# 2024 % 4 == 0 → True
# 2024 % 100 != 0 → True
# Both conditions become True
# Output:
# 2024 is leap year
Pattern Problems
Problem 1 Write a Python program to print a Left Incremental Triangle pattern. ▾
Question breakdown
In Left Incremental Triangle,
the number of stars increases by 1 in each row.
*
**
***
****
View Solution
rows = int(input("Enter number of rows: "))
row = int(input("enter row count="))
for i in range(1, row + 1):
for j in range(i):
print("*", end="")
print()
Understand the Logic
# Example Input
rows = 4
# --------------------------------
# Iteration 1:
# i = 1
# print("*" * 1)
# Output:
# *
# --------------------------------
# Iteration 2:
# i = 2
# print("*" * 2)
# Output:
# **
# --------------------------------
# Iteration 3:
# i = 3
# print("*" * 3)
# Output:
# ***
# --------------------------------
# Iteration 4:
# i = 4
# print("*" * 4)
# Output:
# ****
Problem 2 Write a Python program to print a Left Decremental Triangle pattern ▾
Question breakdown
****
***
**
*
Question Breakdown
In Left Decremental Triangle,
the number of stars decreases by 1 in each row.
View Solution
row = int(input("enter row count="))
column = int(input("enter column count="))
for i in range(1, row + 1):
for j in range(1, column + 1):
if i == j:
print("1", end="")
else:
print("0", end="")
print()
Understand the Logic
# Example Input
rows = 4
# --------------------------------
# Iteration 1:
# i = 4
# print("*" * 4)
# Output:
# ****
# --------------------------------
# Iteration 2:
# i = 3
# print("*" * 3)
# Output:
# ***
# --------------------------------
# Iteration 3:
# i = 2
# print("*" * 2)
# Output:
# **
# --------------------------------
# Iteration 4:
# i = 1
# print("*" * 1)
# Output:
# *