Hi, I am currently newbie in C++, I just learned it for around 2 weeks,

My question is when I put the number, it shows if it's a prime number or not.

The only problem when I put 2 and 3, it supposed to be prime numbers, but the fact that it is not.

Here is my code

#include <iostream>
#include <math.h>
#include <cstdlib>

using namespace std;

#define TRUE 1;
#define FALSE 0;

void getNumber(int*);
int isPrime(int*);

int main()
{
int number;

getNumber(&number);

if (isPrime(&number))
cout << "\n" << number << " is a prime number\n";
else
cout << "\n" << number << " is a not prime number\n";

return 0;
}

void getNumber(int *number)
{
// use pointers as parameter, not references
cout << "Please enter a positive number ";
cin >> *number;
if (!cin.good())
{
cout << "Invalid number entered\n";
exit(1);
}
}

int isPrime(int *number)
{
int count, s, i;

/* Every even number is not prime */
if (*number % 2 == 0 || *number % 3 == 0 || (*number < 4 && *number > 1) return FALSE;

/* check every odd number up to the square root of the number */
s = sqrt(*number);
for (count=3; count<=s; count+=2);
{
if (*number % count == 0) return FALSE;
}
return TRUE;
}

I think the green part need some fixing, can someone pointed out what is my mistakes? THank youu :)

## All 4 Replies

You would perhaps do better to specifically catch the values 2 and 3 and return TRUE before the line in green.

The line in green could then check if the number is divisible by 2 and return FALSE if it is.

Numbers divisible by 3 are caught by your loop on the first iteration so no special case is required for them.

If you want to correct the line in green try saying out load the logic you want it to perform and then try reading out load the logic it does perform, you should be able to hear the differences.

You would perhaps do better to specifically catch the values 2 and 3 and return TRUE before the line in green.

The line in green could then check if the number is divisible by 2 and return FALSE if it is.

Numbers divisible by 3 are caught by your loop on the first iteration so no special case is required for them.

If you want to correct the line in green try saying out load the logic you want it to perform and then try reading out load the logic it does perform, you should be able to hear the differences.

What do you mean I have to catch the values 2 and 3? Sorry I am quite new in programming.
I am not quite understand what are you talking about :(

By catch I mean test to see if *number is 2 or 3.

Since you don't change the value of number in the functions number is sent to is there a reason you send it by reference instead of by value?

If you are going to return true/false, then have the return type be bool.

>>catch the values 2 and 3? Sorry I am quite new in programming

All the more reason to keep the compound logic simple:

``````//handle the special cases first
if(number == 2 || number == 3)
number is prime
else if (number % 2 == 0)
number is not prime
else if (number %3 == 0)
number is not prime
else
then use the generalized algorhythm``````
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.