I need to make a program wich finds all prefect numbers in between two numbers given by the user: 1-51 = 6 and 28 are prefect,but I dont know how, I made a program wich only test one number,given by the user! Can i get help?
#include <iostream>
usingnamespace std;
int main()
{
int number, i, sum;
cout<<"Input a number to see is he a prefect number!\n";
cin>> number;
sum=1;
for(i=2; i<=(number/2);i++)
{
if(number%i==0)
sum+=i;
}
if(number==sum)
cout<<"Number "<<number<<" is perfect!";
else cout<<"Number "<<number<<" is not prefect";
return 0;
}
You do know how to test one number. Encapsulate that procedure in a function. The use of functions keeps the main() simpler.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
void testperfect( int value )
{
// what to do here in order to get the
// same result as your program does?
}
int main()
{
int number;
cout<<"Input a number to see is he a prefect number!\n";
cin>> number;
testperfect( number );
return 0;
}
Now we can focus on the main(). You should get not one, but to numbers, which define a range. You must test each value in the range. Call the testperfect() with each value in the range. Repeat. Iterate. Loop.