This works and shows the factors or tells if the number is a prime number but doesn't show ODD factors please help!! I need to turn this tomorrow:
#include %26lt;iostream%26gt;
using namespace std;
int prime(int num);
int main()
{
int num;
cout%26lt;%26lt;"Enter number: ";
cin%26gt;%26gt;num;
if(prime(num)==1)
cout%26lt;%26lt;"The number is prime";
else
{
cout%26lt;%26lt;"The number is not prime, factors are\n1\n";
if( num%2==0)
for(int j=2;j%26lt;num;j+=2)
{
if(num%j==0)
cout%26lt;%26lt;j%26lt;%26lt;endl;
}
for( int n=num;n%26lt;=num;n/=2)
{
if(num%n==0)
cout%26lt;%26lt;n%26lt;%26lt;endl;
}
for(int m=3;m%26lt;num;m+=3)
{
if(num%m==0)
cout%26lt;%26lt;m%26lt;%26lt;endl;
}
}
}
int prime(int num)
{
if(num %26lt;= 1)
{
return false;
}
if(num == 2)
{
return true;
}
if ( num%2 == 0)
{
return false;
}
for (int n=3;n*n %26lt;= num;n+=2)
{ if (num%n == 0)
return false;
}
return true;
}
Why wont this C++ program show any Odd numbers!!!!!?
I rewrote your program:
#include %26lt;iostream%26gt;
using namespace std;
int prime(int num);
int main()
{
int num;
cout%26lt;%26lt;"Enter number: ";
cin%26gt;%26gt;num;
if(prime(num)==1)
cout%26lt;%26lt;"The number is prime";
else
{
cout%26lt;%26lt;"The number is not prime, factors are\n1\n";
{
for(int j=2;j%26lt;num;j+=2)
{
if(num%j==0)
cout%26lt;%26lt;j%26lt;%26lt;endl;
if((num%(j-1)==0) %26amp;%26amp;(j%26gt;2))
{
cout%26lt;%26lt;(j-1)%26lt;%26lt;endl;
}
}
}
}
cin%26gt;%26gt;num;
return 0;
}
int prime(int num)
{
if(num %26lt;= 1)
{
return false;
}
if(num == 2)
{
return true;
}
if ( num%2 == 0)
{
return false;
}
for (int n=3;n*n %26lt;= num;n+=2)
{
if (num%n == 0)
return false;
}
return true;
}
Change this piece of code :
for( int n=num;n%26lt;=num;n/=2)
{
if(num%n==0)
cout%26lt;%26lt;n%26lt;%26lt;endl;
}
to this:
for( int n=1;n%26lt;=num;n+=2)
{
if(num%n==0)
cout%26lt;%26lt;n%26lt;%26lt;endl;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment