Ok well this is what I did and it yields the correct output I need but I know there is a better way. All I need is put what I want in the print f statement. So again if someone could help me find a better way to do this it would be greatly appreciated. Im not sure how to save each integer that it finds? Where in the code to store it and then how to get it to print with the addition sign? Anyways here is the code I have.
Code:
#include<stdio.h>
int isperfect(int x)
{
int total=0,i=0;
for (i=0;i<(x/2);i++)
{
if (!(x%(i+1)))
{
total+=i+1;
}
}
if (total==x) return 1;
return 0;
}
int main(void)
{
int a=2,b=10000, total;
for(; a<b; a++)
if(isperfect(a))
if (a==6)
printf("%d = 1 + 2 + 3\n",a);
else if(a==28)
printf("%d = 1 + 2 + 4 + 7 + 14\n",a);
else if(a==496)
printf("%d = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248\n",a);
else if (a==8128)
printf("%d = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064\n",a);
return 0;
}
Here is my sample run results which is exactly what is required...
6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064