I’ve just made a C program about finding perfect numbers. It is kinda working, but it writes out the answers more than once(and it writes 24 as well for some reason).
I have very basic C knowledge, I thought it’s simple enough.
The code:
#include <stdio.h>
int main()
{
int i, j, k;
for (i = 2; i < 1000; i++)
{
k = 0;
for (j = 1; j<=i; j++)
{
if (i % j == 0)
{
k = k + j;
}
if (k == i)
{
printf("%dn", i);
}
}
}
}
I tried placing k to somewhere else, but didn’t work, tried to use the return function, but my C knowledge is not big enough
New contributor
Bence Kostyál-Szilágyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1