Write a program to check the given number is perfect or not ?Constraints:1<n<1000Input FormatAn integer nOutput FormatA corresponding stringExample:Input 1:6Output 1:YesInput 2:3Output 2:No Explanation:Sum of factors of the given number must be equal to the numberFactors of 6 are 1,2,3 . 1+2+3=6 which is equal to the given number. So 6 is a perfect number
Question
Write a program to check the given number is perfect or not ?Constraints:1<n<1000Input FormatAn integer nOutput FormatA corresponding stringExample:Input 1:6Output 1:YesInput 2:3Output 2:No Explanation:Sum of factors of the given number must be equal to the numberFactors of 6 are 1,2,3 . 1+2+3=6 which is equal to the given number. So 6 is a perfect number
Solution 1
Sure, here is a Python program that checks if a number is perfect or not:
def is_perfect(n):
sum = 1
i = 2
while i * i <= n:
if n % i:
i += 1
else:
if i * (n // i) == n:
sum = sum + i + n//i
else:
sum = sum + i
i += 1
return sum == n and n!=1
n = int(input("Enter a number: "))
if is_perfect(n):
print("Yes")
else:
print("No")
This program works by finding all the divisors of the number and adding them up. If the sum of the divisors (excluding the number itself) is equal to the number, then the number is perfect.
Solution 2
Sure, here is a Python program that checks if a number is perfect or not:
def is_perfect(n):
sum = 1
i = 2
while i * i <= n:
if n % i:
i += 1
else:
if i * (n // i) == n:
sum = sum + i + n//i
else:
sum = sum + i
i += 1
return sum == n and n!=1
# Test the function
n = int(input("Enter a number: "))
print("Yes" if is_perfect(n) else "No")
This program works by finding all the divisors of the number and adding them up. If the sum equals the original number, then the number is perfect. The function is_perfect(n) checks if a number n is perfect or not. The while loop inside the function is used to find the divisors of n. If n is divisible by i, then both i and n//i are divisors of n. The sum of the divisors is kept in the variable sum. If sum equals n and n is not 1, then n is a perfect number. The input() function is used to take the number to be checked from the user. The result is printed out using the print() function. If the number is perfect, "Yes" is printed, otherwise "No" is printed.
Similar Questions
Write a program to check the given number is perfect or not ?Constraints:1<n<1000Input FormatAn integer nOutput FormatA corresponding stringExample:Input 1:6Output 1:YesInput 2:3Output 2:No
Write a program to check the given number is perfect or not ?
Write a function “perfect()” that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000.[An integer number is said to be “perfect number” if its factors, including 1(but not the number itself), sum to the number. E.g., 6 is a perfect number because 6=1+2+3].
Problem Statement:Given a number, check whether it is a perfect square or not.Input Format:Given an integer n.Output Format:Print Yes if it is a perfect square or No if it is not.Constraints1 <= n <= 10^6Sample Input 1:100Sample Output 1:YesSample Input 2:34Sample Output 2:No
Given an input check for perfect number, abundant or deficent number by using the given function find_sum_of_facfors(). find_sum_of_factors() is predefined and it will return the sum of factors of the given number.If sum of factors of a number is more than the given number - Abundant numberIf sum of factors of a number is less than the given number - Deficient numberIf sum of factors of a number is equal to the given number - Perfect numberNote: Dont include the number as factor for finding sum of factorsi.e., 10 -> 1 + 2 + 5 = 8(sum of factors)Input Format:Accept an integer as input.Output Format:On checking print "Perfect number" or "Abundant number" or "Deficient number".Constraints:1 <= number <= 10^9Sample Input 1:13Sample Output 1:Deficient numberSample Input 2:6Sample Output 2:Perfect number
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.