Adding commas to a set of integers.
I was wondering how to add commas to my output.
It just prints the perfect numbers, and their factors...but I was wondering how to add commas.
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 40 41 42
|
/*Should calculate all the perfect numbers only*/
#include <iostream>
#include <limits.h>
#include <math.h>
using namespace std;
int main()
{
int sum, i, n, perf = 0;
for (n = 1; n <= UINT_MAX ; n++) //changed for testing reasons.
{
sum = (pow(2,(double)(n-1)))*(pow(2, (double) n)-1); //2^(n-1)*2^(n)-1
for(i = 1; i < sum; i++)
{
if((sum % i) == 0) //is it a perfect number?
{ perf = perf + i; }
}// for i =1
if(sum == perf)
{
cout << sum << ":" ; //shows the perfect numbers
for(i = 1; i < sum; i++)
{
if((sum % i) == 0) //calculates the factors
{ printf("%u+", i); }//close if
}//close for
cout << "=" << sum << endl;
}//close if sum == perf
perf = 0;
}//close initial for loop
return 0;
}//close main
|
:) *snicker*
Why are you using half C and half C++ output?
Topic archived. No new replies allowed.