Need to figure out how to write this program

I need help writing a program that does this. I am new to this and I do not know how to start this off can someone give me some advice please?

An integer is said to be a perfect number if the sum of its divisors, including 1 (but not the number
itself), is equal to the number. For example, 6 is a perfect number, because 6=1+2+3.
Write a program that:
• finds and prints all the perfect numbers between 1 and a random number between 50 and 100.
• uses a function isPerfect that determines whether parameter number is a perfect number.
• prints the divisors of each perfect number to confirm that the number is indeed perfect
Hello bitcyknw,

You could use this as a start and adjust as you need.

Furry Guy wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cctype>
#include <limits>

int main()
{
   std::cout << "Do you want someone to do all the work for you? ";
   char answer{};
   std::cin >> answer;

   std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

   if (std::toupper(answer) == 'Y' ) { std::cout << "Post it in the Jobs section.\n"; }

   else { std::cout << "Show what you have coded so far.\n"; }

   std::cout << "Good luck.\n";
}


Or post what you have done and go from there.

Andy
Gonna have lots of trouble with school if you won't even try stuff.


• finds and prints all the perfect numbers between 1 and a random number between 50 and 100.


1) Generate a random number.
2) Loop from 1 to that random number.


• uses a function isPerfect that determines whether parameter number is a perfect number.


3) Write a method isPerfect that takes a number as an argument and returns a boolean.
4) Loop through all numbers from 1 to (argument-1) to check if they are divisors.
5) To check if something is a divisor, use the modulo operator to look for left overs after division.
6) Keep a tally of the divisors.
7) When the loop is done, check the tally. If it is the same number as the argument, return true.


• prints the divisors of each perfect number to confirm that the number is indeed perfect

8) Pass a vector pointer to isPerfect by adding it as a second arg.
9) Add code to the loop that will add each divisor to the vector.
10) Use printf to print the divisors when isPerfect returns true.

If you try, you will learn a lot faster. Mistakes can be frustrating, but they are a valuable tool when learning. Let me know if any of the steps confuse you.
Last edited on
or just research the darn math problem. Why professors keep asking these kinds of questions with a pat answer is beyond me.

cin >> in;
if(in >=6 && in <28)
cout << "6" << endl;
else if (in >= 28)
cout << "6, 28" << endl;
else
cout << "none\n";

you can even write the factors of both hard-coded, given that there are only 2 of them.
even if you go all the way out to what a 64 bit int holds its only 8 or 9 or something of them.
Its probably not what the professor wanted, but a silly question deserves a silly program.
Last edited on
They're a bit thin on the ground!

1
2
3
4
5
6
7
8
9
10
#include <iostream>
//const int MAX = 100000000;
const int MAX = 1000000;
int main()
{
   static int sumProperDivisors[1+MAX] = { 0 };
   for ( int i = 1; i <= MAX / 2; i++ )
      for ( int j = i + i; j <= MAX; j += i ) sumProperDivisors[j] += i;
   for ( int i = 1; i <= MAX; i++ ) if ( i == sumProperDivisors[i] ) std::cout << i << " ";
}
Topic archived. No new replies allowed.