Hello, I am a newbie a C++. I was doing a program where you calculate prime numbers, and it prints prime and non-prime numbers. Here is my code,

``````#include <iostream>
#include <conio.h>
using namespace std;

main()
{
int n = 1;
int i = 2;
bool IsPrime;
int Max;
int x = 0;

cout << "Maximum number: ";
cin >> Max;

while (x < Max)
{
IsPrime = true;

while ((i < n) &&)
{
if (n % i == 0)
{
// Is not prime
IsPrime = false;

}
else
{
// Is prime
i++;

}
}

if (IsPrime)
{
cout << "Prime: " << n << endl;
}

n++;
x++;
}

getch();
}``````

Thanks,
~mr. doughnuts

## All 10 Replies

simplify.

1) Make a bool function that takes a int i as a parameter and checks if it is a prime or not.
2) inside your loop, check if i to MAX is a prime.

your function prototype might look like this :

``````bool isPrime(int num);
``````

HINTS :
a) How do you when a number is a prime ?
- When it is only evenly divisible by itself an 1.
b) Use the mod operator.

firstperson, thanks for the quick reply.
What is a bool function? :)

~Mr doughnuts

A bool function returns true or false.

This is some really quick prime number checker that I made since the post above. It checks to see if the number can be divided by 2, 3, 5, 7 evenly and if it is not 1. If it passes through all this then it is prime.

``````#include <iostream>

using namespace std;

bool isPrime(int x)
{
if( x == 1 )
return false;
if( x % 2 == 0 && x != 2 )
return false;
if( x % 3 == 0 && x != 3 )
return false;
if( x % 5 == 0 && x != 5 )
return false;
if( x % 7 == 0 && x != 7 )
return false;
return true;
}

int main()
{
int max;
cout << "Max?: ";
cin >> max;
for( int i = 1; i <= max; i++ )
{
if( isPrime(i) )
{
cout << "Prime: " << i << endl;
}
}
system("PAUSE");
return 0;
}``````

Fail.

Lets see yours because last time I checked this works and its short.

``````#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(int x)
{
if( x == 1 )
return false;
if( x % 2 == 0 && x != 2 )
return false;
if( x % 3 == 0 && x != 3 )
return false;
if( x % 5 == 0 && x != 5 )
return false;
if( x % 7 == 0 && x != 7 )
return false;
return true;
}

bool is_prime(int i)
{
if(i < 2) return false;

if(i == 2) return true;

float half = ceil( sqrt((float(i))) ) + 1;

for(int j = 2; j < half; j++)
if(i % j == 0) return false;

return true;
}
int main()
{
int i = 0;

cout << boolalpha;

while(true)
{
if(isPrime(i) != is_prime(i))
{
cout<<"When your function versus my simple brute force disagrees : \n\n";

cout<<"Result :  from your function : "<<i<<" is prime = "<<isPrime(i)<<endl;
cout<<"Result :  from my function   : "<<i<<" is prime = "<<is_prime(i)<<endl;
cin.get();
break;
}
i++;
}

return 0;
}``````

Flawed and bigger :( you win.

Flawed and bigger :( you win.

You learn from mistakes.

Wow, that is smaller, masterful, flawless code. :|
Thanks, firstperson !!!

~mr. doughnuts

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.