Hi, I know this kind of stuff been wandering around. I did have a look through the search function but I haven't got the answer.

This is the program to check whether the number inputted is a prime number or not.

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

#define TRUE 1;
#define FALSE 0;

using namespace std;

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 not a prime number\n";
   }
   
   system("PAUSE");
   return 0;
}

void getNumber(int *number)
{
   cout << "Please enter a positive number ";
   cin >> *number;
   if (!cin.good())
   {
      printf("Invalid number entered\n");
      exit(1);
   }
}

int isPrime(int number)
{
   int count;
   double s;
	
   if (number > 0)
   {
      if (number != 1)
      {
         if (number != 2)
         {
           /* Every even number is not prime */
           if (number % 2 == 0) 
           {
             return FALSE;
           }
           else
           {
             /* check every odd number up to the square root of the number */
             s = sqrt(number);

              for (count=3; count<=s; count+=2);
              {
                 cout << count << "<- This is the count";
                 if (number % count == 0)
                    return FALSE;
              }
             return TRUE;
           }
         }
         else
         {
           return TRUE;
         }
      }
      else
      {
        return FALSE;
      }
   }
   else
   {
     cout << "You have entered negative number, please enter positive number only!\n";
     exit(1);
   }
 
}

I've got problem with the for statement.
When I inputted 3 the program says 3 is not a prime number
but when I inputted 9, 21, 25, 27 it says they are prime number..

I tried to search google for the right prime number algorithm but didn't find anything useful.

Can someone help me? I think, the for statement suppose to check whether the number is divisible by 3, 5, 7 and so on...

Recommended Answers

All 2 Replies

Take a look at line 63. There seems to be an extra character on the line ;)

Take a look at line 63. There seems to be an extra character on the line ;)

jesuss............................

I've been working on this all day....
OMG!!!!!!!!!!!!!!!

I've should have noticed it earlier..............

THANKKKKKKSSSSSSS

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.