I have a code here that tests prime and perfect numbers and gives it divisors. I
ID: 3625537 • Letter: I
Question
I have a code here that tests prime and perfect numbers and gives it divisors. I was wondering how can I make this code look something like this output:
Sample Run 1:
Enter an integer between 1 and 1000: 1000
1000 is not prime
The divisors of 1000 are:
1 2 4 5 8 10 20 25 40 50 100 125 200 250 500 1000
****I basically want the divisors to be indicated in a line horizontally at least 5 per line instead of going down vertically. Also how do I include the number 1000 in the list of divisors.?****
Sample Run 2:
Enter an integer between 1 and 1000: 0
Invalid the number should be between 1 to 1000.
Please re enter a number between 1 to 1000: 6
6 is not a prime number
Its divisors are 1 2 3 6
6 is a perfect number
***I basically want the user to get another chance once they enter an invalid number.****
Here is my code:
#include <iostream>
using namespace std;
int main ()
{
int num, n, divisor;
int sum=0;
const int SENTINEL=-1;
cout<<"enter integers between 1 and 1000:";
cin>>num;
if(0<num && num<=1000)
{
while(num!=SENTINEL)
{
for ( n=2; n < num; n++ )
if (num%n==0 )
break;
if ( n==num )
cout << num <<" is prime"<< endl;
else
{
cout << num << " is not prime" <for(divisor=1; divisor <= num/2; divisor++)
if (( num%divisor==0) && (num!=divisor))
{
cout << "The factor of " << num << " is: " << divisor << endl;
sum +=divisor ;
if(sum==num)
cout << num << " is a perfect number" << endl;
}
}
cin>>num;
}
}
else
cout<<" Invalid the number should be between 1 and 1000";
return 0;
}
Explanation / Answer
I put two versions here one works with the while loop you wrote (num!=SENTINEL) (I just worked around it). The other has that removed I'm not sure why you put the loop in unless that was part of the wrong number loop. If you do not need the loop I suggest the second program.
----------------------------------------------------------------------
#include
using namespace std;
int main ()
{
int num, n, divisor;
int sum=0;
const int SENTINEL=-1;
bool Continue = false; // I established this to help with re entering a wrong number
bool> do { // This loop is used to loop the program until a number between 1 - 1000 is entered
if (!Continue)
cout<<"Enter integers between 1 and 1000: ";
else
cout<<"Please re enter a number between 1 to 1000: ";
Continue = false;
cin>>num;
cout << endl;
if(0 {
while(num!=SENTINEL)
{
for ( n=2; n < num; n++ )
{
if ((num % n)==0 )
break;
}
if ( n==num )
{
cout << num <<" is prime "<< endl;
}
else
{
cout << num << " is not prime " << endl;
}
for(divisor=1; (divisor <= num/2); divisor++)
{
if (( num%divisor==0) && (num!=divisor))
{
if (!Once) // I just put this in there to format the look
{
cout << "The divisors of " << num << " are: " << endl;
> }
else;
cout << divisor << " "; // puts all the divisors in succesion
sum +=divisor ;
}
else;
if(sum==num)
{
cout << num; // Adds actual num as divisor in case of perfect
cout << " " << num << " is a perfect number " << endl;
}
}
if(sum!=num)
cout << num; // Adds actual num as divisor when not perfect
cout << endl;
break; // Break out of your first while loop which I am still not quite sure why it is there
}
}
else
{
cout<<"Invalid the number should be between 1 and 1000 " << endl;
Continue = true;
}
} while (Continue);
return 0;
}
----------------------------------------------------------------------
I put a version with the (num != Sentinel) loop removed below
---------------------------------------------------------------------
#include
using namespace std;
int main ()
{
int num, n, divisor;
int sum=0;
const int SENTINEL=-1;
bool Continue = false;
bool> do {
if (!Continue)
cout<<"Enter integers between 1 and 1000: ";
else
cout<<"Please re enter a number between 1 to 1000: ";
Continue = false;
cin>>num;
cout << endl;
if(0 {
for ( n=2; n < num; n++ )
{
if ((num % n)==0 )
break;
}
if ( n==num )
{
cout << num <<" is prime "<< endl;
}
else
{
cout << num << " is not prime " << endl;
}
for(divisor=1; (divisor <= num/2); divisor++)
{
if (( num%divisor==0) && (num!=divisor))
{
if (!Once)
{
cout << "The divisors of " << num << " are: " << endl;
> }
else;
cout << divisor << " ";
sum +=divisor ;
}
else;
if(sum==num)
{
cout << num;
cout << " " << num << " is a perfect number " << endl;
}
}
if(sum!=num)
cout << num;
cout << endl;
}
else
{
cout<<"Invalid the number should be between 1 and 1000 " << endl;
Continue = true;
}
} while (Continue);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.