Finding prime numbers without cmath

In this assignment the user gives a positive integer, and from that integer the program should determine whether it is prime or not, and then give all the values between 1 and nth (user interger) term that are prime.

I need help on figuring out what is wrong with my code. If the value can be divided by 3, it is considered prime. I can't figure out how to fix it. Here is what I have so far...

#include <iostream>

using namespace std;


bool isPrime (int number);
void findPrimes (int n);
unsigned long long int fibo (int n);
void findPrimeFactors (int number);

int main ()
{

int testNum;

// test for the isPrime function
cout << "Enter a test number - ";
cin >> testNum;
cout << endl;

if( isPrime(testNum) )
cout << testNum << " is prime." << endl;
else
cout << testNum << " is not prime." << endl;

// test for how many primes
cout << "The prime numbers between 1 and "<< testNum << " are: " << endl;
findPrimes( testNum );
cout << endl;

// test for Fibonacci number finder
cout << "The Fibonacci number for " << testNum << " is : " << fibo( testNum ) << endl;

//test for prime factors
cout << "The prime factors of " << testNum << " are: " << endl;

findPrimeFactors( testNum );

cout << endl;
system("PAUSE");

return 0;
}


bool isPrime (int number)
{
int i;

for(i=2; i<=number; i++)
{
if(number % i != 1 && number!=i)
{
return false;
}
else
{
return true;
}

}
}


void findPrimes (int n)
{
int i=1;

while(i <= n)
{
if(isPrime(i))
{
cout << i << " ";
i++;
} else i++;
}

}

void findPrimeFactors (int number)
{
int i=2;

cout << "1" ;

while ( i <= number)
{
if(isPrime(i))
{
if(number % i == 0)
{
cout << "*" << i ;
number = number/i;
}
else
{
i++;
}
}
else i++;
}
}







side note...I haven't done Fibonacci part yet. Just working on getting the prime numbers correct first.

I think the problem is in your isPrime ()
I've implemented it like this:
1
2
3
4
5
6
7
bool isPrime (int number)
{
    for(int i=2; i<number; i++)
        if(number % i == 0)
            return false;
    return true;
}

and it works fine.
Thank you sooooo much! That worked. Now all I need to do is figure out the Fibonacci part and I can finally turn it in (hopefully I can do that just fine). Thanks a bunch!
hi.. please help me to determine a prime number or not with the simplest codes.. not using the bool.. only if or loop
愚か…
LOL. You know, I actually had someone once offer me money to do his homework when at the University. It was about as simple as this.

Pretty darn blatant.

am1767 The fibonacci is actually easier than primes. You only need to remember two values each time through the loop: the n-1 and n-2.

Good luck! (And kudos on using your brain! :-)
Topic archived. No new replies allowed.