In this blog, I have practiced some important Python number-based programs. These programs helped me understand while loops, functions, conditional statements, digit extraction, mathematical operations, and problem-solving logic.
1. Find Two Prime Numbers Whose Sum Is Target
This program tries to find two prime numbers whose sum is equal to the given target number.
Code
def find_prime(no):
div = 2
while div <= no//2:
if no % div == 0:
return False
div += 1
else:
return True
prime1 = 2
target = 68
i = 1
while i <= target//2:
result = find_prime(prime1+i)
if result == True:
prime2 = prime1+i
if(prime1+prime2 == target):
print(prime1, "+", prime2, "=", prime1+prime2)
break
else:
prime1 = prime2
i = 0
i = i+1
Output
2 + 66 = 68
Note: The current logic has a mistake because
66is not a prime number. A correct prime pair for68would be7 + 61 = 68.
2. Neon Number
A Neon Number is a number where the sum of the digits of its square is equal to the original number.
For example:
9² = 81
8 + 1 = 9
Therefore, 9 is a Neon Number.
Code
num = 9
copy = num
num = num * num
result = 0
while num > 0:
digit = num % 10
result += digit
num //= 10
if result == copy:
print(f"{copy} is neon number")
else:
print(f"{copy} is not neon number")
Output
9 is neon number
3. Strong Number
A Strong Number is a number where the sum of the factorial of each digit is equal to the original number.
For example:
145
1! + 4! + 5!
= 1 + 24 + 120
= 145
Therefore, 145 is a Strong Number.
Code
num = 145
copy = num
result = 0
def factorial(num):
res = 1
while num > 0:
res = res * num
num = num - 1
return res
while num > 0:
last = num % 10
result += factorial(last)
num //= 10
if copy == result:
print(f"{copy} is Strong number")
else:
print(f"{copy} is not Strong number")
Output
145 is Strong number
4. Automorphic Number
An Automorphic Number is a number whose square ends with the original number.
For example:
25² = 625
The last two digits are 25, which is the original number.
Therefore, 25 is an Automorphic Number.
Code
num = 25
copy = num
digit = 0
while num > 0:
digit += 1
num = num // 10
n = copy
n = n * n
i = 1
while digit > 0:
i = i * 10
digit -= 1
if n % i == copy:
print(f"{copy} is Automorphic Number")
else:
print(f"{copy} is not Automorphic Number")
Output
25 is Automorphic Number
5. Duck Number
A Duck Number is a number that contains at least one zero.
For example:
1024
Since 1024 contains 0, it is a Duck Number.
Code
num = 1024
copy = num
found = False
while num > 0:
digit = num % 10
if digit == 0:
found = True
num //= 10
if found:
print(f"{copy} is Duck Number")
else:
print(f"{copy} is not Duck Number")
Output
1024 is Duck Number
6. Spy Number
A Spy Number is a number where the sum of its digits is equal to the product of its digits.
For example:
1124
Sum:
1 + 1 + 2 + 4 = 8
Product:
1 × 1 × 2 × 4 = 8
Since both are equal, 1124 is a Spy Number.
Code
num = 1124
copy = num
add = 0
product = 1
while num > 0:
digit = num % 10
add += digit
product *= digit
num = num // 10
if add == product:
print(f"{copy} is Spy Number")
else:
print(f"{copy} is Not Spy Number")
Output
1124 is Spy Number
These number-based programs are a good way to strengthen Python fundamentals.
The main goal of these exercises is not just to get the output, but to understand how loops, conditions, functions, and mathematical operations work together to solve a problem.
Top comments (0)