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>
usingnamespace 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