Prime Number Program - Questions

I am making a program that finds 160 prime numbers after a starting number. How well am I doing so far. I am new to this. I believe next I need to make a for statement, but in the past i've only used simple ones such as
 
for (x=1; x<= max ; x++)


I am not sure how to manipulate this to start at the number entered by the user.
Possibly: for (x = num; x >= num; x++)

Am I on the right track?

I am currently in my first c++ class but this is an assignment from tutoring I am receiving outside of class. If you post a code please explain how it works.

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

#include<iostream>
using namespace std;

int main()
{
  int num;
  int x, y;
  int count;
  // User Prompts
  cout << "This program finds 160 prime numbers. " << endl;
  cout << "Enter a starting number : " << endl;
  cin >> num;


  // Prime Number Function
  bool isPrime (int num);
  {
	bool status;

	if (num % 2 == 0	)	// Write something here to determine is the number is prime
		status = false;		// If the number is prime it will be assigned the boolean value True
	else					// If the number is not prime it will be assigned the boolean value False
		status = true;
	return status;
  }




  system("pause");
  return 0;
}
Last edited on
Your for loop should look like:
1
2
3
4
5
6
7
8
9
int i, x, num; 
std::cout << "Enter number: "; 
std::cin >> num; 
x = num;
for(i = 0; i < 160; ++i)
{
    //Find the next prime after x
   //Then update x to be that number
}


Do you know how to find primes?
1
2
3
4
5
6
7
8
9
10
11
12

  bool isPrime (int num);
  {
	bool status;

	if (num % 2 == 0	)	// Write something here to determine is the number is prime
		status = false;		// If the number is prime it will be assigned the boolean value True
	else					// If the number is not prime it will be assigned the boolean value False
		status = true;
	return status;
  }



Doesnt this determine if the value is prime or not? And assign it either True or False?

For your code doesnt that list the numbers up to 160, instead of 160 prime numbers after the value specified by the user
My for loop would work fine. It would increment by 1 everytime a new prime is found. Once it reached 160 new primes found, the loop would break.

Prime number != Odd number.

Prime numbers are only divisible by 1 & themselves. Bearing this in mind, you need to check that a number, p, does not divide by any n > 1 n < p. In the most simplistic method, you can do exactly that.
1
2
3
4
5
6
7
8
9
10
bool isprime = true; 
int n = 2, p = 91; 
while(n < p)
{
    if(p % n == 0)
    {
        isprime = false; 
    }
    ++n;
}


Of course, there exist much better methods for finding primes, but I would suggest getting a basic implementation working first and then we can show you some more effective algorithms for finding primes.
Last edited on
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

#include<iostream>
using namespace std;

int main()
{
  int num;
  int i, x;
  int count;
  // User Prompts
  cout << "This program finds 160 prime numbers. " << endl;
  cout << "Enter a starting number : " << endl;
  cin >> num;


  // Prime Number Function
  bool isPrime (int num);
  {
	bool status;

	if (num % 2 == 0	)	// Write something here to determine is the number is prime
		status = false;		// If the number is not prime it will be assigned the boolean value False
	else					// If the number is  prime it will be assigned the boolean value True
		status = true;
	return status;
  }


for(i = 0; i < 160; ++i)
	{
	




	}




  system("pause");
  return 0;
}
I have tried using your code in a different .cpp to maniuplate things and see how it works. This program compiles but the output makes no sense to me.

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

int main()
{

int num;
cout << "This program finds 160 prime numbers. " << endl;
  cout << "Enter a starting number : " << endl;
  cin >> num;
  cout << "" << endl;



bool isprime = true; 
int n = 2, p = num;
while(n<p)
{
    if(p % n == 0)
    {
        isprime = false; 
    }
    ++n;
	cout << ""<<++n <<endl;
}

system("pause");
return 0;
} 
Last edited on
Hi I am newbie too, :)
First, Prime is a number that could divide by itself and 1 that the mod will get 0.
(note that: num % 2 == 0 will tell if the number is even or odd only if it true it is even, if it false it is odd)

let look at code
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
#include <iostream>
#include <Windows.h>
#include <stdlib.h>

using namespace std;

int main()
{

    int x,y,count,count2=0; //we have the variable here
    int input1; //input from user

    cout<<"Please Input Fisrt Number\(Integer\) to Scan : ";
    cin>>input1; //get number from user
    cin.ignore(); //throw enter away


    for(x=input1;;x++)//lets x start from the input form user
        {
            count=0;//make initial value =0, for beginning of every loop

            for(y=1;y<=x;y++)//make to divide number from 1 to the input number
                {
                    if(x%y==0)//if we divide and get mod = 0 then let the count increase by 1
                        {
                            count++;
                        }
                }
            if(count==2 && count2<160)//for the display
                {
                    count2++;
                    cout<<"The Prime Number Orders: "<<count2<<"        Prime Number = "<<x<<endl;
                }
                /*we have count=2 from the defination of prime
                and set 160 for the orders of prime*/

        }

        cin.get();//freeze for the display
}

If you take away that cout << line which I don't know what it is doing there.
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
#include<iostream>
using namespace std;

int main()
{

int num;
cout << "This program finds 160 prime numbers. " << endl;
  cout << "Enter a starting number : " << endl;
  cin >> num;
  cout << "" << endl;



bool isprime = true;
int n = 2, p = num;
while(n<p)
{
    if(p % n == 0)
    {
        isprime = false;//No, n is not prime; 
        break; //There is no point continuing to check after we found it is not prime already. 
    }
    ++n;
}
if(isprime == 1) //If our function found out the number is prime. 
{
    cout << n << "\n"; //Let's see the prime number. 
}

return 0;
}


Do you understand this so far?
Topic archived. No new replies allowed.