Write a code to find the given number is Perfect number or not
Perfect Number
A Perfect Number is a positive integer that is equal to the sum of its proper divisors, excluding the number itself. For example, 6 is a perfect number because its divisors (excluding 6 itself) are 1, 2, and 3, and 1 + 2 + 3 = 6. Below, we'll explore the solutions in C, C++, Java, and Python.
What is a Perfect Number?
A perfect number is a positive integer that equals the sum of its proper divisors, excluding the number itself.
Examples:
6: Divisors are 1, 2, 3 → 1 + 2 + 3 = 6
28: Divisors are 1, 2, 4, 7, 14 → 1 + 2 + 4 + 7 + 14 = 28
Solutions:
Now, let's see how to determine if a given number is a perfect number in different programming languages: Java, Python, C, and C++.
import java.util.Scanner;
public class PerfectNumber {
// Function to check if a number is perfect
public static boolean isPerfect(int number) {
int sum = 0;
// Calculate the sum of proper divisors
for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum += i;
}
}
// Check if the sum of divisors equals the number
return sum == number;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Check and print if the number is perfect
if (isPerfect(number)) {
System.out.println(number + " is a perfect number.");
} else {
System.out.println(number + " is not a perfect number.");
}
scanner.close();
}
}
#include <iostream>
using namespace std;
// Function to check if a number is perfect
bool isPerfect(int number) {
int sum = 0;
// Calculate the sum of proper divisors
for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum += i;
}
}
// Check if the sum of divisors equals the number
return sum == number;
}
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
// Check and print if the number is perfect
if (isPerfect(number)) {
cout << number << " is a perfect number." << endl;
} else {
cout << number << " is not a perfect number." << endl;
}
return 0;
}
#include <stdio.h>
// Function to check if a number is perfect
int isPerfect(int number) {
int sum = 0;
// Calculate the sum of proper divisors
for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum += i;
}
}
// Check if the sum of divisors equals the number
return sum == number;
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
// Check and print if the number is perfect
if (isPerfect(number)) {
printf("%d is a perfect number.\n", number);
} else {
printf("%d is not a perfect number.\n", number);
}
return 0;
}
Explanation of Code:
Each code version defines an isPerfect
function to determine if a number is a perfect number.
Divisors: We loop through all numbers from 1 to
number - 1
and sum those that dividenumber
without leaving a remainder.Check Equality: We then check if the sum of these divisors equals the original number.
Sample Input & Output:
Input 1:
number = 6
Output: 6 is a perfect number.
Input 2:
number = 10
Output: 10 is not a perfect number.
Conclusion:
Determining whether a number is a perfect number is a common programming problem that helps enhance your understanding of loops, conditionals, and basic arithmetic operations in various programming languages.
** Please do subscribe my blog for future updates and share with your friends, if you find this informative **
Comments
Post a Comment