Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'cant get the factors of the perfect numbers to print after the perfect number

ID: 3622538 • Letter: I

Question

I'cant get the factors of the perfect numbers to print after the perfect number. please help!
[code]

#include <iostream>
using namespace std;

int main() {

int i=1;
int sum = 0;
int count=0;
int factors[10000],

int fcount=0;

int j;

while(count<44)
{
sum = 0;

for (j = 1; j < i; j++) {
if (i % j == 0)
    {factors[fcount++]=j;
      sum += j;
      }
}
if (sum == i)

{cout << i<<": " ;
 for(j=0;j<fcount;j++)
     cout<<factors[j]<<" ";
 cout<< endl;

count++;
}
i++;
}
cout << "DONE!" << endl;
system("pause");
return EXIT_SUCCESS;
}

Explanation / Answer

please rate - thanks

one way

#include <iostream>
using namespace std;

int main() {
int i=1;
int sum = 0;
int count=0;
int factors[10000],fcount=0;
int j;
while(count<44)
{fcount=0;
sum = 0;
for (j = 1; j < i; j++) { //check every number from 1 to i-1 for divisibility
if (i % j == 0) // if j divides evenly into i
    {factors[fcount++]=j;
      sum += j; //add j (the divisor) to the sum
      }
}
if (sum == i)
//if the sum of the divisors equals the number
{cout << i<<": " ;
for(j=0;j<fcount;j++)
     cout<<factors[j]<<" ";
cout<< endl; //print out the perfect number

count++;
}
i++;
}
cout << "DONE!" << endl;
system("pause");
return EXIT_SUCCESS;
}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote