#include<iostream>
usingnamespace std;
int main()
{
int start,end;
cout<<"Starting number: ";
cin>>start;
cout<<"Ending number: ";
cin>>end;
for (int outer=start; outer<end; outer++)
{
int sum=0;
for (int inner=1; inner<=outer/2; inner++)
if (outer%inner==0)
sum += inner;
if (sum==outer)
cout<<outer<<" is a Perfect Number."<<endl;
else
cout<<"No Perfect Numbers found between "<< start <<" and "<< end <<endl;
}
return 0;
}
i tried using break and continue statement, but using them simply doesnt work as the compiler say illegal use without matching if .
i am trying to find a way so that when the prgm run, it will loop from x to y and see if there is any perfect number or not.
with perfect num:
6 is a Perfect Number.
28 is a Perfect Number.
w/o any perfect num:
No Perfect Numbers found between 29 and 66
is there any possible way to make line 18 and 19 to only appear when there is no perfect number found? without using the break statement of course. but that will end the prgm....im kinda lost
make another int abc = 0 at the begining of main.
delete line 18 and 19.
modify ur loop to such a way that when u find a perfect number that the int abc increments
after the loops put a if/else statement in which if int abc is 0, then u found no perfect numbers, else nothing appears.
#include<iostream>
usingnamespace std;
int main()
{
int start,end,abc=0;
cout<<"Starting number: ";
cin>>start;
cout<<"Ending number: ";
cin>>end;
for (int outer=start; outer<end; outer++)
{
int sum=0;
for (int inner=1; inner<=outer/2; inner++)
if (outer%inner==0)
sum += inner;
if (sum==outer)
cout<<outer<<" is a Perfect Number."<<endl;
abc+=1; //only add 1 if sum==outer
{if (abc==0)
cout<<"No Perfect Numbers found between "<< start <<" and "<< end <<endl;
}
}
system("Pause");
return 0;
}
}
then i remember that you said
after the loops put a if/else statement in which if int abc is 0, then u found no perfect numbers, else nothing appears.
so i moved line 19-21 to after line 22 and it still doesnt work. entered 2 number: 9 and 19 and it just ended there. shouldnt abc==0 and display no perfect number found when the prgm checked and saw that there is no number between 9 and 19 thats a perfect number?
#include<iostream>
usingnamespace std;
int main()
{
int start,end,abc=0;
cout<<"Starting number: ";
cin>>start;
cout<<"Ending number: ";
cin>>end;
for (int outer=start; outer<end; outer++)
{
int sum=0;
for (int inner=1; inner<=outer/2; inner++)
if (outer%inner==0)
sum += inner;
//Check if current number is a perfect number
if (sum==outer)
{
//if so - inform the user
cout<<outer<<" is a Perfect Number."<<endl;
//update the count of prefect numbers found so far.
abc+=1; //only add 1 if perfect number
}
}
//OK all numbers have been checked
//if the count of prefect numbers found is still 0 at this point
if (abc==0)
{
//Then output an appropriate message
cout<<"No Perfect Numbers found between "<< start <<" and "<< end <<endl;
}
system("Pause");
return 0;
}
the only thing i see why my prgm didnt work, even after i moved
1 2 3
{if (abc==0)
cout<<"No Perfect Numbers found between "<< start <<" and "<< end <<endl;
}
was a simple {} for
1 2 3
if (sum==outer)
{ cout<<outer<<" is a Perfect Number."<<endl;
abc+=1;} //only add 1 if sum==outer
can someone clarify to me why did a simple {} somehow make the prgm display the correct output? from what i understand,
1 2 3
if (sum==outer)
cout<<outer<<" is a Perfect Number."<<endl;
abc+=1; //only add 1 if sum==outer
should have been read and compile since it belonged to that if statement, and it also ended within the main()
But for some reason, by adding that {}, it just solved the problem
if (sum==outer)
cout<<outer<<" is a Perfect Number."<<endl;
abc+=1; //only add 1 if sum==outer
should have been read and compile since it belonged to that if statement, and it also ended within the main()
But for some reason, by adding that {}, it just solved the problem
You will find that the way you have written it - cout<<outer<<" is a Perfect Number."<<endl; is part of the if
statement - but abc+=1; //only add 1 if sum==outer is not.