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 

CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online Exam Quiz O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Best Website and Software Company in Allahabad Website development Company in Allahabad