Programming Examples

Python program to print first n perfect numbers


first n perfect numbers

Write a Python program which accept the value of n from user and print first n Perfect Numbers.

n=int(input("Enter the value of N : "))
count=0
a=1;
while count<=n:
    sum=0
    for b in range (1,(a//2)+1):
        if a%b==0:
            sum=sum+b
    if sum==a:
        print(a ,end=' ')
        count+=1
    a+=1
Output

Enter the value of N : 4

6 28 496 8128 

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad