I have used three variables in this program namely *number*, *counter* and *decider*. All of them are of *integer* type. *number* is used to store the number entered by the user which is to be checked. *counter* is initialized at 2 and then it is incremented by 1 in each loop and it is used to test the division of *number* against all integers between 2 and the number itself but excluding itself also. The so called variable *decider* is used to make decision during the last steps of the program while it is being told to the user that the number he enetered is a prime number.

The number entered by the user is also tested once to make sure that it is a positive number number. If it is not hten the user is promted to enter the number a second time. Hoewver, if the user again enters a negative number then now he will get a wrong output. So it is important to enter only positive numbers while using this program.

If number entered by user is either 0 or 1 then he simply gets a message that the number entered by him is neither prime nor composite.

However, if the number entered is greater than or equal to 2 then testing will start from the *while loop*. In each iteration of this loop, remainder will be calculated when number is divided by counter. If this remainder equals zero then user will be told that the number entered is not a prime number, the variable *decider* will be incrimented by 1 and the loop will exit after the *break* statement is encountered. But if the remainder *number % counter* is not equal to zero then the variable *counter* will be incremented by 1 and the loop will start again. As soon as the the variable *counter* will become equal to the variable *number*, the execution of the loop will stop. After the loop, their is an *if* statement. It tests the variable *decider* if it is equal to zero or not. If it is, then, user will be told that the entered number is a prime number. Please not that *decider* will not eual zero (i.e. it will be greater than zero) only if it has been tested and told to the user that number is not prime. In this case, it ofcourse makes sense to skip the statements inside this last *if* statement. If you still have any confusion about any step of this program or you have some alternative way of doing any of these steps then please feel free to share it with me via comments. I shall be very thankful to you.

Below is given the source code of this program. I have compiled and tested it on microsoft visual studio. It should run without problems in any other standard C++ compiler as well.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// Prime Checker.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <conio.h> using namespace std; int _tmain() { int number; // number entered by the user to check int counter = 2; // counter to check the entered number against all other numbers int decider = 0; //it will deside whether number is prime or not ... cout << "Please enter the number that you want to check:n"; cin >> number; if (number < 0) { cout << "Please enter a positive integer"; cin >> number; } else if (number == 0 || number == 1) { cout << number << " is neither a prime nor a composite number.n"; } else { while (counter < number) { if (number % counter == 0) { cout << number << " is not a prime number.n"; decider ++; break; } counter ++; } if (decider == 0) { cout << number << " is a prime number.n"; cout << endl; } } cout << "Press any key to close this program ..."; _getch(); return 0; } |

**Edit: February 11, 2011 at 9:40 AM**

I have just published a revised version of this program. Please check it too. It has a few nice improvements in it

## 7 replies on “C++ program to check whether a number is prime or not”

[…] two days ago, I published a post namely C++ program to check whether a number is prime or not. One of my kind readers, Jaume commented on that program and gave me some very useful suggestions […]

[…] [1.9.7_1111]please wait…Rating: 0.0/5 (0 votes cast)About two days ago, I published a post namely C++ program to check whether a number is prime or not. One of my kind readers, Jaume commented on that program and gave me some very useful suggestions […]

urgly, but efficienter:

https://gist.github.com/804850

[codesyntax lang=”cpp”]

cout <> number;

if (number < 0)

{

cout <> number;

}

/* else

shold be*/

while (number<0)

{

cout <> number;

}

/*(Perhaps more checking but I don’t use iostream usually, so I may have forgotten things)*/

while (counter < number)

{

if (number % counter == 0)

{

cout << number << " is not a prime number.n";

decider ++;

break;

}

counter ++;

}

[/codesyntax]

In here, one you've checked for 2 you don't need to check for 4,8,10,… . Just make 2 a special case and start counter at 3, increasing it by 2 each step.

Decider just needs a bool.

wow … thankyou very much such useful tips. especially checking the input for negative numbers using while loop is something which I realy missed in my programe. actually I am an absolute beginner in programming. I hope I will improve over time…

I shall try to publish a revised version of this program soon. For now let’s wait for some more useful suggestion from our other visitors.

This works better http://www.anyexample.com/programming/java/java_prime_number_check_(primality_test).xml

It works better unless the user enters a number that is larger than 514, in which case it can be a prime but the snippet will fail. I like Muhammad’s answer because it can check larger numbers than a set list.

While I’m here, you can stop checking numbers that are larger than 1/2 of the input, because at that point the only possible divisors would be smaller than 1. As an example, if 19 is your input you can stop checking at 10 and still know its a prime number.