0% found this document useful (0 votes)
4 views2 pages

Number Classification and Conversion Methods

The document provides Python code snippets for checking if a number is a perfect number, palindrome, or Harshad number, as well as converting decimal numbers to binary and vice versa. Each section includes a brief description of the criteria for the respective number type and the corresponding code implementation. The examples demonstrate basic input handling and conditional statements in Python.

Uploaded by

vithyathar14
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Number Classification and Conversion Methods

The document provides Python code snippets for checking if a number is a perfect number, palindrome, or Harshad number, as well as converting decimal numbers to binary and vice versa. Each section includes a brief description of the criteria for the respective number type and the corresponding code implementation. The examples demonstrate basic input handling and conditional statements in Python.

Uploaded by

vithyathar14
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

6.

Perfect Number

A number is perfect if the sum of its proper divisors (excluding itself) equals the number.

num = int(input('Enter a number: '))

sum_div = sum(i for i in range(1, num) if num % i == 0)

if sum_div == num:

print('Perfect Number')

else:

print('Not a Perfect Number')

7. Palindrome Number

Check if a number reads the same forward and backward.

num = input('Enter a number: ')

if num == num[::-1]:

print('Palindrome')

else:

print('Not Palindrome')

8. Harshad Number

A number is Harshad if it is divisible by the sum of its digits.

num = int(input('Enter a number: '))

sum_digits = sum(int(d) for d in str(num))

if num % sum_digits == 0:

print('Harshad Number')

else:

print('Not a Harshad Number')

9. Decimal to Binary

Convert a decimal number to binary using bin() or custom method.


num = int(input('Enter decimal number: '))

print('Binary:', bin(num)[2:])

10. Binary to Decimal

Convert binary to decimal using int() with base 2.

binary = input('Enter binary number: ')

print('Decimal:', int(binary, 2))

You might also like