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.
#include <iostream>
usingnamespace 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;
}
elseif(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.
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
#include <iostream>
usingnamespace 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;
}
}
#include <math.h>
bool isPrime (int num)
{
if (num <=1)
returnfalse;
elseif (num == 2)
returntrue;
elseif (num % 2 == 0)
returnfalse;
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>
#include <cmath>
usingnamespace 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;
}
}
}
#include <iostream>
#include <cmath>
usingnamespace 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;
}
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...
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.
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
elseif (num % 2 == 0)
returnfalse;//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)
returnfalse;
}
returntrue;
}
}
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