Python: Perfect Number In Python Using While Loop-DecodingDevOps
Perfect Number In Python Using While Loop-DecodingDevOps
A perfect number is a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3. Other perfect numbers are 28, 496, and 8,128. All even perfect numbers can be represented by N = 2p-1(2p -1) where p is a prime for which 2p -1 is a Mersenne prime. For Example, 28 is a perfect number since divisors of 28 are 1, 2, 4,7,14 then sum of its divisor is 1 + 2 + 4 + 7 + 14 = 28. in this post i am going to explain how to find perfect number in python using while loop.
Perfect Number In Python Code
i = 2;sum = 1; n = int(input("Enter a number: ")) while(i <= n//2 ) : if (n % i == 0) : sum += i i += 1 if sum == n : print(n,"is a perfect number") else : print(n,"is not a perfect number")
STEP 1: In this step, we first initialize the sum with 1 and iterate i with number 1.
STEP 2: In this step, we take input from the user and typecast it into the integer.
STEP 3: In this step, we iterate using the python while loop by using the floor division.
STEP 4: In this step, if a proper divisor is found we add it.
STEP 5: In this step, we will check if the sum is equal to the number or not.
STEP 6: we will print whether the number is a perfect number or not a perfect number.
OUTPUT:
Enter a number: 28 28 is a perfect number Enter a number: 10 10 is not a perfect number