First off, I do want to state that I have read the forums and have not found an issue directly regarding this exact problem, though it is possible I may have overlooked it or something and if so please just point me in the right direction and I will leave you alone.

The program I have been writing is a prime number checker, just to check to see if the number input is prime and list numbers up to that number that are prime also. The program is working fine, the listing functions correctly and it properly identifies whether the input is prime or not, except for one exception. When I entered the number 777 into the program, it states that it is prime, which is incorrect, same with 7777. I have been trying to figure this out for a couple days now to no avail. So, any help will be greatly appreciated. Here is the snippet of code that handles the input checker:

void userPrime(int num)
{
	if(num == 2)
	{
		cout << "The user entered the number, " << num << ", this is a prime number.\n\n";
	}
	else
	{
		for(int c = 2;c <= num;c++)
		{	
			numPrime = true;
			if(num % c == 0)
			{
				numPrime = false;
			}
			if(numPrime == true)
			{
				cout << "The user entered the number, " << num << ", this is a prime number.\n\n";
				break;
			}
				else
				{
					cout << "The user entered the number, " << num << ", this is not a prime number.\n\n";
					break;
				}
		}
	}
}
StuXYZ commented: Excellent first post +3
prvnkmr449 commented: Your code never print the list , it always print value of num +0

Ok first off, welcome to daniweb, and thank you for writing a clear post with code blocks and not-too much and not too little code!

Ok to your problem, you have unfortunately made a couple of logic errors. First, consider your loop, for(int c=2;c<= num;c++) . This will test each possible factor up and including the number itself. If this was the only error, ALL of your test numbers [except 2] would be reported as not-prime.

Now, let us consider the inner part of the loop, numPrime is set true each and every time round the loop. Then it is set if a factor is found. BUT then it is tested, and regardless if it is either true/false you execute a break statement. Therefore,you only test if the number divides by 2. No other numbers. You can see this if you add a line cout<<"Test number c == "<<c<<endl; as the first instruction of the loop.

So to remedy you could do something like this

numPrime=true;
for(int c=2;c<num;c++)
   {
     if (num % c == 0) 
       {
          numPrime=false;
          break;
       }
   }

  if (numPrime == true) 
    std::cout<<"Number prime"<<std::endl;
  else
    std::cout<<"Number not prime"<<std::endl;

Some simple improvements that will help, you only need to test up to the square root
of the number, since if a*b=N^2 and a=N+x and b=N+y where x, y and >=0 then
a*b== N^2 + N(x+y) +xy. Which can only hold if x,y==0.

I don't think you work hard to solve this problem or you attach wrong code please try once more you have many more exception then you mention even not provide the list also............Basic logic behind finding prime number is that prime number is only divisible by itself and 1 thats all about prime number.............
Best Of Luck

I don't think you work hard to solve this problem or you attach wrong code please try once more you have many more exception then you mention even not provide the list also............Basic logic behind finding prime number is that prime number is only divisible by itself and 1 thats all about prime number.............
Best Of Luck

This is the code regarding just the prime number checker in my program. I did not include all of the other code in the program because it is functioning correctly and does not affect this. I know how to find prime numbers, etc., but this problem is dealing specifically with it giving the correct answer on some and wrong answers on higher number problems. So, I feel that the code that I put in the post is relevant. Thank you for response.

Ok first off, welcome to daniweb, and thank you for writing a clear post with code blocks and not-too much and not too little code!

Ok to your problem, you have unfortunately made a couple of logic errors. First, consider your loop, for(int c=2;c<= num;c++) . This will test each possible factor up and including the number itself. If this was the only error, ALL of your test numbers [except 2] would be reported as not-prime.

Now, let us consider the inner part of the loop, numPrime is set true each and every time round the loop. Then it is set if a factor is found. BUT then it is tested, and regardless if it is either true/false you execute a break statement. Therefore,you only test if the number divides by 2. No other numbers. You can see this if you add a line cout<<"Test number c == "<<c<<endl; as the first instruction of the loop.

So to remedy you could do something like this

numPrime=true;
for(int c=2;c<num;c++)
   {
     if (num % c == 0) 
       {
          numPrime=false;
          break;
       }
   }

  if (numPrime == true) 
    std::cout<<"Number prime"<<std::endl;
  else
    std::cout<<"Number not prime"<<std::endl;

Some simple improvements that will help, you only need to test up to the square root
of the number, since if a*b=N^2 and a=N+x and b=N+y where x, y and >=0 then
a*b== N^2 + N(x+y) +xy. Which can only hold if x,y==0.

Thank you for your prompt response to this, I will take your advice and look into the things that you pointed out. I will reply if I have any more issues. Once again, thank you for your time on this.

In addition to what StuXYZ said, I suggest using smaller test cases than 777 and 7777. It'll break at 9, calling it prime. That's because, as StuXYZ mentioned, due to the break statements, you don't really have a loop and thus don't test for anything higher than 3.

Your function is not provide the list.........it always provide you num.

numPrime = true;
			if(num % c == 0)
			{
				numPrime = false;
			}
			if(numPrime == true)
			{
				cout << "The user entered the number, " << num << ", this is a prime number.\n\n";
				break;
			}
				else
				{
					cout << "The user entered the number, " << num << ", this is not a prime number.\n\n";
					break;
#include <iostream>

int main ()  {
   int num;
   bool prm;
   const char* na[]={"prime","not prime"};
   std::cin >> num;
   for (int x=2;x<=10;x++)  {
      if (prm=!(num%x)) break;         
    }
   std::cout << na[prm];
 }

wrongie me :D.sowwy .. don't use that

#include <iostream>

int main ()  {
   int num;
   bool prm;
   const char* na[]={"prime","not prime"};
   std::cin >> num;
   for (int x=2;x<=10;x++)  {
      if (prm=!(num%x)& !(num<10)) break;         
    }
   std::cout << na[prm];
 }

In addition to what StuXYZ said, I suggest using smaller test cases than 777 and 7777. It'll break at 9, calling it prime. That's because, as StuXYZ mentioned, due to the break statements, you don't really have a loop and thus don't test for anything higher than 3.

Yes, I know what you mean. I personally had not tested it with anything high, but then told my girlfriend to test it with random numbers just to see it's functionality and she tried 777 and that's where I saw the problem. I just understand what you mean about the break, I seem to be very break happy. I will definitely be trying to use less breaks and start making sure they are in the right place. Thank you for your response.

Ok first off, welcome to daniweb, and thank you for writing a clear post with code blocks and not-too much and not too little code!

Ok to your problem, you have unfortunately made a couple of logic errors. First, consider your loop, for(int c=2;c<= num;c++) . This will test each possible factor up and including the number itself. If this was the only error, ALL of your test numbers [except 2] would be reported as not-prime.

Now, let us consider the inner part of the loop, numPrime is set true each and every time round the loop. Then it is set if a factor is found. BUT then it is tested, and regardless if it is either true/false you execute a break statement. Therefore,you only test if the number divides by 2. No other numbers. You can see this if you add a line cout<<"Test number c == "<<c<<endl; as the first instruction of the loop.

So to remedy you could do something like this

numPrime=true;
for(int c=2;c<num;c++)
   {
     if (num % c == 0) 
       {
          numPrime=false;
          break;
       }
   }

  if (numPrime == true) 
    std::cout<<"Number prime"<<std::endl;
  else
    std::cout<<"Number not prime"<<std::endl;

Some simple improvements that will help, you only need to test up to the square root
of the number, since if a*b=N^2 and a=N+x and b=N+y where x, y and >=0 then
a*b== N^2 + N(x+y) +xy. Which can only hold if x,y==0.

I managed to get it work using the issues pointed out in your post. I can't thank you and the community here enough for your help. I am still fairly new to C++ and hope to be able to add valuable discussion to the forum in the future but for right now if I happen to come across another issue with any other programs I may have in the future I will post it. Thank you once again.

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.