I'm trying to make a program where you enter the number you want to find primes up to which then lists all the primes up to that number... But I enter 100 then it just sits there... Here's my code so far:

#include <iostream>
#include <cmath>
int prime(int);

int main()
{
using namespace std;
int upto;
int current = 3;
cout << "Find primes up to: ";
cin >> upto;
while(current < upto){
        cout << prime(current) << endl;
}
system("pause");
return 0;
}

int prime(int p)
{
int test = p - 1;
while(test > -1){
if(test == 0){
        return p;
}
else if(test % p ==  0){
     return 0;
}
else
    test - 1;
}
}

Recommended Answers

All 11 Replies

I think the problems in the way i put the formula for primes into code.

You never change the value of your variable "current," so your program can never terminate.

thanks but i might still have issues. ill post new code if that is the case.

my new code looks like this

#include <iostream>
#include <cmath>
int prime(int);

int main()
{
using namespace std;
int upto;
int current = 3;
cout << "Find primes up to: ";
cin >> upto;
while(current < upto){
        cout << prime(current) << endl;
        current += 1;
}
system("pause");
return 0;
}

int prime(int p)
{
int test = p - 1;
while(test > -1){
if(test == 0){
        return p;
}
else if(test % p ==  0){
     return 0;
}
else
    test - 1;
}
}

when i run it and enter the value it doesnt output anything or end ?

That's because you have the same problem in your prime function: You never change the value of the variable test, so the program never terminates.

i believe i do

else
    test - 1;
}

sorry forgot to do

else
test -= 1;

now it "works" but it just outputs all the numbers 3+ to the number you entered. again heres my code

#include <iostream>
#include <cmath>
int prime(int);

int main()
{
using namespace std;
int upto;
int current = 3;
cout << "Find primes up to: ";
cin >> upto;
while(current < upto){
        cout << prime(current) << endl;
        current += 1;
}
system("pause");
return 0;
}

int prime(int p)
{
int test = p - 1;
while(test > -1){
if(test == 0){
        return p;
}
else if(test % p ==  0){
     return 0;
}
else
    test -= 1;
}
}

You have (at least) two problems.

The first one is that the expression test % p in line 27 should be p & test.

The second is more subtle. Look at the loop in lines 23-32. You will see that the only way that your prime function can ever return anything but 0 is if the variable "test" reaches 0.

However, before it reaches 0, it has to reach 1--and when it does, line 27 will compute p % test (after you've fixed it) with test equal to 1, which will always be zero. So the function will always return zero.

I'm not going to tell you how to solve this problem, because I've already done enough of your homework for you.

thanks for all the help you dont need to do anything else. happy posting!

You have (at least) two problems.

The first one is that the expression test % p in line 27 should be p & test.

The second is more subtle. Look at the loop in lines 23-32. You will see that the only way that your prime function can ever return anything but 0 is if the variable "test" reaches 0.

However, before it reaches 0, it has to reach 1--and when it does, line 27 will compute p % test (after you've fixed it) with test equal to 1, which will always be zero. So the function will always return zero.

I'm not going to tell you how to solve this problem, because I've already done enough of your homework for you.

FIXED:

#include <iostream>
#include <cmath>
int prime(int);

int main()
{
  using namespace std;
  int upto;
  int current = 3;
  cout << "Find primes up to: ";
  cin >> upto;
  cout << "upto = " << upto << endl;
  while(current < upto){
  if(prime(current) != 0){
    cout << prime(current) << endl;
    }
    current += 1;
  }
  system("pause");
  return 0;
}

int prime(int p)
{
  int test = p - 1;
  while( test > 1 ){
    if( p % test ==  0 ){
      return 0;
    }
    else
      test -= 1;
  }
  return p;
}
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.