I wrote a simple C++ program in Visual Studio Community edition that checks if a number given to the program is a prime number or not:
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter a number to find out if its a prime number: " << 'n';
int number;
cin >> number;
int counter = 1;
while (counter <= number / 2)
{
if (number % counter == 0 && counter != 1 && counter != number)
{
cout << "The number is not a prime number." << 'n';
cout << "The divisor is: " << counter;
return 1;
}
else
{
if (counter == number / 2)
{
cout << "The number is a prime number.";
return 0;
}
}
counter++;
}
}
The program works for small numbers but when it is given a big number, the program gives wrong answers. Where is the fault in the program?
New contributor
hornet1234567 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2