Page Contents
Interview Content
- C Programming Coding Questions
- C Pattern Programming Questions
- C Programming Interview Questions
- Java Programming Coding Questions
- Java Pattern Programming Questions
- Java Programming Interview Questions
- Python Programming Coding Questions
- Python Pattern Programming Questions
- Python Programming Interview Questions
- SQL Interview Questions
Perfect number program in Python with example
Ans:
In this tutorial you will learn how to write a program in Python programming language to check a given number is perfect or not.
This program is little bit tricky and logical.
But Before directly moving on writing and learning the program first you should know
What is Perfect Number?
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.
Lets understand it with example
6 is a positive number and its divisor is 1,2,3 and 6 itself.
But we should not include 6 as by the definition of perfect number.
Lets add its divisor excluding itself
1+2+3 = 6 which is equal to number itself.
It means 6 is a Perfect Number.
How our program will behave?
Python program for perfect number is written below. This program will take a positive number as an input.
Suppose you want to check that a number is perfect or not then you have to give that number as an input to this below program at time of execution.
After the calculation it will give you appropriate output.
If number is perfect then
Below is a program to check a given number is perfect or not in Python
num = int(input("please give first number a: "))
sum=0
for i in range(1,(num//2)+1):
remainder = num % i
if remainder == 0:
sum = sum + i
if sum == num:
print("given no. is perfect number")
else:
print("given no. is not a perfect number")
Output:
Also prepare these Python Programs:
- Write a program to reverse an integer in Python.
- Write a program in Python to check whether an integer is Armstrong number or not.
- Python Program to check a given number is Prime number or not.
- Write a program in Python to print the Fibonacci series using iterative method.
- Write a program in Python to print the Fibonacci series using recursive method.
Latest Uploads on Website
- AVL Tree with explanation
- Radix sort algorithm explanation with example
- Quick Sort Algorithm with explanation
- Bubble sorting algorithm with Bubble sort program in C
- Insertion sort algorithm and program in C
- Selection Sort Algorithm and Program in C
- Linear probing technique explanation with example
- Collision in Hashing and Collision resolution technique
- Hashing in data structure with its types
- Binary search tree operations with Program
- Binary search tree in data structure
- Binary search algorithm in data structure with explanation
- linear search in data structure with Algo and Program