Class 11 Practical Programs
Check Perfect Number, Armstrong Number or Palindrome
AIM
To write Python programs that determine whether a number is a perfect number, an Armstrong number, or a palindrome.
Definitions:
- Perfect Number: A positive integer equal to the sum of its proper divisors (excluding itself). Example: 6 = 1 + 2 + 3
- Armstrong Number: A number equal to the sum of its digits raised to the power of number of digits. Example: 371 = 3³ + 7³ + 1³
- Palindrome: A number that reads the same forwards and backwards. Example: 121, 1331
ALGORITHM
Perfect Number Algorithm:
- Start
- Input a number n
- Initialize sum = 0
- For i from 1 to n-1:
- If n % i == 0, then sum = sum + i
- If sum == n, then it's a perfect number
- Else, it's not a perfect number
- Stop
Armstrong Number Algorithm:
- Start
- Input a number n
- Count the number of digits
- Initialize sum = 0
- For each digit in the number:
- Extract the digit and raise it to the power of number of digits
- Add to sum
- If sum equals original number, it's Armstrong
- Stop
Palindrome Algorithm:
- Start
- Input a number n
- Initialize reverse = 0
- Store original number in temp
- While n != 0:
- Extract last digit
- reverse = reverse * 10 + last_digit
- n = n // 10
- If temp == reverse, it's palindrome
- Stop
PROGRAMS
Program 1: Perfect Number Check
# Determine whether a number is a perfect number
n = int(input("Enter any number: "))
sum = 0
i = 1
for i in range(1, n):
if(n % i == 0):
sum = sum + i
if (n == sum):
print("The number is a Perfect number!")
else:
print("The number is not a Perfect number!")Program 2: Armstrong Number Check
# Finding Armstrong Number
n1 = int(input("Enter any number: "))
temp = num = n1
no_of_digits = 0
# Count number of digits
while(num != 0):
no_of_digits += 1
num = int(num / 10)
print("No of digits is:", no_of_digits)
# Sum of the digits power raise to no of digits
sum = 0
while(n1 != 0):
last_digit = n1 % 10
sum += (last_digit ** no_of_digits)
n1 = int(n1 / 10)
# Checking Armstrong Number
if(sum == temp):
print("Number is Armstrong number")
else:
print("Number is Not Armstrong number")Program 3: Palindrome Check
# Finding Palindrome or Not
n = int(input("Enter the Number: "))
rev = 0
temp = n
while(n != 0):
last_digit = n % 10
rev = rev * 10 + last_digit
n = n // 10
if(temp == rev):
print("It is Palindrome Number")
else:
print("It is not a Palindrome Number")OUTPUT
Perfect Number Output:
Enter any number: 6
The number is a Perfect number!
Enter any number: 10
The number is not a Perfect number!
Armstrong Number Output:
Enter any number: 371
No of digits is: 3
Number is Armstrong number
Enter any number: 123
No of digits is: 3
Number is Not Armstrong number
Palindrome Output:
Enter the Number: 121
It is Palindrome Number
Enter the Number: 123
It is not a Palindrome Number
CONCLUSION
Thus, the given program was successfully executed and the output was verified as per the expected result.
VIVA QUESTIONS
What is a perfect number? Give examples.
A perfect number is a positive integer that is equal to the sum of its proper positive divisors (excluding itself). Examples: 6 (1+2+3=6), 28 (1+2+4+7+14=28), 496, 8128.
How do you calculate if 371 is an Armstrong number?
371 has 3 digits. Calculate: 3³ + 7³ + 1³ = 27 + 343 + 1 = 371. Since the sum equals the original number, 371 is an Armstrong number.
What is the difference between // and / operators in Python?
// is floor division (returns integer quotient), while / is true division (returns float). For digit extraction, we use // to get integer results.
Why do we use modulo operator (%) in these programs?
The modulo operator is used to: find divisors (n%i==0), extract last digit (n%10), and check divisibility conditions.
How would you modify the palindrome program to work with strings?
For strings, we can use string slicing: if string == string[::-1] or compare characters from both ends using loops.
What happens if we input 0 or negative numbers?
Perfect numbers are defined for positive integers only. For 0 or negative numbers, we should add input validation to handle these cases appropriately.
Can a single-digit number be an Armstrong number?
Yes, all single-digit numbers (1-9) are Armstrong numbers because each digit raised to the power 1 equals itself.
How can we optimize the perfect number checking algorithm?
We can optimize by checking divisors only up to √n, and for each divisor i found, also add n/i to the sum (if i ≠ n/i and n/i ≠ n).
Related Resources
- Prime/Composite Numbers
- Fibonacci Series Program
- GCD and LCM Program
- String Palindrome Check
- Flow of Control Theory
- Python Fundamentals
- Number Theory MCQs
- Mathematical Functions in Python
Mathematical Concepts
- • Perfect numbers: 6, 28, 496, 8128
- • Armstrong: 153, 371, 407, 1634
- • Palindromes: 121, 1331, 12321
- • Digit manipulation techniques
Programming Tips
- • Use // for integer division
- • Modulo (%) for digit extraction
- • Store original value before loops
- • Validate input for edge cases
Need Help?
Join our tuition classes for detailed explanations of number theory and mathematical programming.
Register for Classes →