While learning Python, I started practising different number-based programs to improve my logical thinking and problem-solving skills.
In this blog, I'm going to explore three interesting number programs:
- Spy Number
- Perfect Number
- Neon Number
Let's understand each one with an example and Python code using while loops.
1. Spy Number
A Spy Number is a number where the sum of its digits is equal to the product of its digits.
Example: 1124
Digits: 1, 1, 2, 4
Sum of digits:
1 + 1 + 2 + 4 = 8
Product of digits:
1 × 1 × 2 × 4 = 8
Since both results are equal, 1124 is a Spy Number.
Python Code:
num = 1124
temp = num
total = 0
product = 1
while temp > 0:
digit = temp % 10
total += digit
product *= digit
temp //= 10
if total == product:
print("Spy Number")
else:
print("Not a Spy Number")
Output:
Spy Number
2. Perfect Number
A Perfect Number is a number whose sum of proper divisors is equal to the original number.
Example: 6
The proper divisors of 6 are:
1, 2, 3
Sum of divisors:
1 + 2 + 3 = 6
Since the sum is equal to the original number, 6 is a Perfect Number.
Python Code:
num = 6
i = 1
total = 0
while i < num//2:
if num % i == 0:
total += i
i += 1
if total == num:
print("Perfect Number")
else:
print("Not a Perfect Number")
Output:
Perfect Number
3. Neon Number
A Neon Number is a number where the sum of the digits of its square is equal to the original number.
Example: 9
First, calculate the square:
9 × 9 = 81
Now calculate the sum of its digits:
8 + 1 = 9
Since the sum is equal to the original number, 9 is a Neon Number.
Python Code:
num = 9
square = num * num
total = 0
while square > 0:
digit = square % 10
total += digit
square //= 10
if total == num:
print("Neon Number")
else:
print("Not a Neon Number")
Output:
Neon Number
Top comments (0)