Perfect Number: Perfect number is a positive real number which is the sum of its all positive divisors without itself.
Example: $6$ is a perfect number. The divisor of $6$ are $1, 2, 3$.
The sum of its divisors are : $1 + 2 + 3 = 6$
$28$ is a perfect number. The divisors of $28$ are $1, 2, 4, 7, 14$.
The sum of its divisors are : $1 + 2 + 4 + 7 + 14 = 28$
Some other perfect numbers are: $496, 8128$, etc
Algorithm:
1. Find the number of divisors of the number
2. Add all the divisors
3. Check the addition of the numbers is equivalent to that number or not
4. If equivalent then perfect number
5. If not equivalent not a perfect number
#include "iostream" #include "conio.h" #include "math.h" using namespace std; int main() //Start of main { int n, i=1, u=1, sum=0; cout << "Enter a range to find the perfect numbers :" ; cin >> n; while(i<=n) { // start of first loop. while(u<=n) { //start of second loop. if(u<i) { if (i%u == 0) sum = sum + u; } // End of the if statement u++; } // End of second loop if (sum == j) { cout << i << "is a perfect number" << "\n"; } i++; u = 1 ; sum = 0; } // End of the First loop return(0); } // End of main
Input: Range := 500
Output: Perfect number := 6, 28, 496
Input: Range := 10000
Output: Perfect number := 6, 28, 496, 8128
No comments:
Post a Comment