Perfect Numbers help

I need help with my perfect numbers program. I have it working how i need it to but it is not displaying the factors correctly. I need it to display the perfect number then the factors in a row following it and the way it sits now is it is putting the number and one factor after it then making more columns. any suggestions? here is my code.

#include <cstdio>
#include <cctype>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>

using namespace std;

int main()
{
int sum = 0;
int factors;
int num;

cout << "Perfect Numbers" << endl;

for(int num = 1; ; num++)
{
sum = 0;
for(int i = 1; i < num; i++)
{
if(!(num%i))
{
sum+=i;
}
}
if(sum == num)
{
for(factors=1; factors<=sum; ++factors)
{
if (sum%factors==0)
{
cout << sum << ":" << factors << " " << endl;
}// If
}// For
}// If
}// For
}// Main
Ok I have something to learn.
What do you mean by Perfect number?
all the factors of the number add up to be the number. ex 6 is a perfect number. 1 + 2 + 3 = 6
Test me some examples :
1 + 2 + 3 + 4 (Perfect number?)
3 + 4 + 5 (Perfect number?)
2 + 4 + 6 (Perfect number?)
the first 4 perfect numbers are 6, 28, 496, 8128
Please use code tags. It's hard to make out parentheses
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdio>
#include <cctype>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>

using namespace std;

int main()
{
  int sum = 0;
  int factors;
  int num;

  cout << "Perfect Numbers" << endl;

  for(int num = 1; ; num++)
  {
    sum = 0;
    for(int i = 1; i < num; i++)
    {
      if(!(num%i))
      {
        sum+=i;
      }
    }
    if(sum == num)
    {
      for(factors=1; factors<=sum; ++factors)
      {
        if (sum%factors==0)
        {
          cout << sum << ":" << factors << " " << endl;
        }// If
      }// For
    }// If
  }// For
}// Main 


You only need to move cout << sum << ":" << endl; outside the loop, and in the loop just print 'factors' separated by a space. When the loop ends put any number of newlines so that the next number is printed on its own line
i tried to move the "cout << sum << ":" << endl;" outside of the loop but it would not work correctly. where do you suggest I place it?
I wrote it like this and it seems to work like you want to
1
2
3
4
5
6
7
8
9
10
11
12
    if(sum == num)
    {
      cout << sum << ":" << endl;
      for(factors=1; factors<=sum; ++factors)
      {
        if (sum%factors==0)
        {
          cout << factors << " ";
        }// If
      }// For
      cout << endl;
    }// If 
I cant believe it was that simple of a fix. Thank you for all your help
Ok I see. Thanks you!!
Syntax :
2n * (2(n + 1) - 1)


So, you can try the code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
  cout << "Perfect Numbers (10 numbers)" << endl;

 for(int n = 1;n <= 10;n++)
 {

cout << "Result : " << pow(2, n) * (pow(2, n + 1) - 1) << endl;

 }

}// Main  
Last edited on
Topic archived. No new replies allowed.