Prime Numbers help

Hello, I am having a little problem with this program I wrote. It accepts an integer in an argument, and it will tell the user if that number is a prime number or not.

This is what I did:
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
#include <iostream>
using namespace std;

int isPrime(int);

int main()
{
	int num;

	cout << " Enter a number, and I will tell you if it is a prime number or not: ";
	cin >> num;

	isPrime(num);

	return 0;
}

int isPrime(int number)
{
	int num1, result;
	
	num1 = number;
	result = number % num1;

	if(result == 0)
	{
		cout << " " << number << " is a prime number." << endl;
	}
	else if(result != 0)
	{
		cout << "  "<< number << " is not a prime number." << endl;
	}

	return result;
}


The modulus part returns 0 for any number entered and it is returning true, displaying the first if statement in the function.
Please help me correct this problem
First off, at the top, you should have
int isPrime(int number);
Never have a function declaration that doesn't match another call...

EDIT:

Second, here is a little tip for debugging
...
When you have a problem, try to break it down to the exact line where the problem is occurring.. So for this code, I would forget the int number part and just use a predefined variable, say 2. If you did this then you know that it wouldn't be a problem in your function. You would know that it must be a declaration issue.
Last edited on
What problem? This program is doing exactly what you are telling it to. On Line 22 you set "num1" equal to the argument passed in, then immediatly after on Line 23 you set "result" equal to "number" mod "num1", basically number mod itself which will always be zero. You just need to double check your math.

Also, even though I agree with strongdrink in spirit, this is a style preference and it will not effect how your program runs.
On observing the code, i noticed that you are not checking the prime number. The number is said to be prime if it is only divisible by 1 or number itself. So to check prime number, you must divide the argument by all numbers between 1 and the number itself. But you are dividing the number by itself and check the modulus which is obviously zero. The following syntax may work, check it out

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
#include <iostream>
using namespace std;

int isPrime(int);

int main()
{
	int num;

	cout << " Enter a number, and I will tell you if it is a prime number or not: ";
	cin >> num;

	isPrime(num);

	return 0;
}

int isPrime(int number)
{
	int num1 = 2, result;

    do{
        result = number % num1;
        if(result == 0) //if it is divisible then it is not prime
        break;
        num1++;
    }while(num1!=number);

	if(num1 == number) //if not it is prime
	{
		cout << " " << number << " is a prime number." << endl;
	}
	else
	{
		cout << "  "<< number << " is not a prime number." << endl;
	}
}
Last edited on
@ Bibek Subedi: Check this out: http://en.wikipedia.org/wiki/Prime_number#Testing_primality_and_integer_factorization
It really cuts down on the systems work load once you get into larger integers. If anything why would you waste the extra processor cycles?
Here is a faster function for prime evaluation:

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
#include <math.h>
bool isPrime (int num)
{
    if (num <=1)
        return false;
    else if (num == 2)         
        return true;
    else if (num % 2 == 0)
        return false;
    else
    {
        bool prime = true;
        int divisor = 3;
        double num_d = static_cast<double>(num);
        int upperLimit = static_cast<int>(sqrt(num_d) +1);
        
        while (divisor <= upperLimit)
        {
            if (num % divisor == 0)
                prime = false;
            divisor +=2;
        }
        return prime;
    }
}


Please note that this is not my code, it was written by user Grey Wolf. I copied and pasted because if you do a search, you will find multiple previous threads asking about prime number evaluation in C++, since this is a common freshman assignment. Always search first for quicker answers, helped me a lot in my first year ;-)
#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int i,j,x = 0;
cout<<"2 3"<<"\t";
for(i=4;i<15;i++)
{
x=0;
for(j=2;j<i;j++)
{
if(i%j==0)
x=1;
}//j loop
if (x==0)
cout<<i;
}// i loop
}// compound statement

Hey guys I have this code, can u help me debug it?
I need to find prime numbers till 15.
I am getting a warning at 19: Function should return a value
Guys I need help fast gotta give it by tomorrow morn.
This is the true program!


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
51
52
53
54
55
#include <iostream>
#include <cmath>
using namespace std;

int isPrime(int number);

bool is_prime = false;

int main()
{
	int num;

	cout << " Enter a number, and I will tell you if it is a prime number or not: ";
	cin >> num;

	isPrime(num);
	
	if(is_prime){
                 cout << num << " Is a prime. " << endl;
                 }
          else{
                cout << num << " Is not a prime. " << endl;
              }
	

system("PAUSE");

	return 0;
}

int isPrime(int number)
{
	
	
	for(int i = 2; i <= sqrt(number); i++){
            if(number % i == 0){
                      
            return is_prime = false;
            
            }
            else{
            return is_prime = true;
            }
            }
            
           
	
	
	
   

	

	
}
Sir I appreciate your help and your logic is good too but my teacher told me to do it without any usage of a function anywhere
closed account (D80DSL3A)
int main() should return an integer value. Add return 0; just before the end of main().

@happykiller. Your program concludes that all odd numbers are primes. First failure is for num = 9.
Also @ happykiller:

Don't put sqrt in a for loop like that because it will be called every time the loop runs (slow).

1
2
3
// this is better:
int end = sqrt(number);
for(int i = 2; i <= end; i++){
Last edited on
If you aren't allowed to use functions then just put the function from happy killers' code into the main:


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
#include <iostream>
#include <cmath>
using namespace std;

bool is_prime = false;

int main()
{
	int number;

	cout << " Enter a number, and I will tell you if it is a prime number or not: ";
	cin >> number;

        int end = sqrt(number);
	for(int i = 2; i <= end; i++){
		if(end % i == 0){
			return is_prime = false; 
            }
            else{
            return is_prime = true;
            }
            }
	
	if(is_prime){
                 cout << num << " Is a prime. " << endl;
                 }
          else{
                cout << num << " Is not a prime. " << endl;
              }
	

system("PAUSE");

	return 0;
}


Hopefully that should get you sorted.
Last edited on
@ditch Yeah i know. i just wanted to simplify.
happykiller wrote:
@ditch


=(
closed account (zb0S216C)
happykiller wrote:
ditch (sic)

You're not alone, Disch. I've been called Formwork :/

Wazzak
If you aren't allowed to use functions that I'd argue that "main()" is a function, but I've never done well in classroom settings. Also, "system()" is out since that's a function also.

It seems like some people here don't understand how to use the return value from a function... That concerns me...
@Disch I am so so so sorry. Please forgive.
Computergeek01 wrote:
If you aren't allowed to use functions that I'd argue that "main()" is a function, but I've never done well in classroom settings. Also, "system()" is out since that's a function also.


Smartass.


:P
Last edited on
I stumbled upon this thread while looking for a simple method to check for Primes. stridexr's post with some of Grey Wolf's code worked perfectly, but what I have here is a slightly more compact and I think faster version of Grey Wolf's original code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <math.h>
bool isPrime(int num)
{
    if (num <= 2)
        return (num == 2);//returns true if 2 since it is prime
    else if (num % 2 == 0)
        return false;//even numbers aren't primes
    else
    {
        int upperLimit = (int)sqrt((float)num)+1;
        for(int divisor = 3; divisor <= upperLimit; divisor+=2)
        {
            if (num % divisor == 0)
                return false;
        }
        return true;
    }
}

if you don't want to use sqrt() you can use the following. Though sqrt() is much better (despite it being a heavy function)
int upperLimit = (num/2)+(num%2);
This can cause the for loop to go through a lot of useless checks if you get to higher numbers but it will still work if you can't use sqrt().

This is what I've added to my small library of helper functions =P

Thanks for the great posts =)
Last edited on
Topic archived. No new replies allowed.