Prime numbers in an interval

Write a program that prints all the prime numbers in the range of two user defined numbers: low and high. Create a function that checks for a number to be prime or not.

this is my code, and it's not working, it just asks for my interval but it wont display the prime numbers. Does anyone know why?

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
56
57
58
59
60
61
62
63
64
  #include <iostream>
#include <cstdlib>  
#include <ctime>  

using namespace std;

void printRandomNumber(int low, int high);
bool isPrime(int n);
int randNum=0;

int main (){
	int low,high;
	
	cout<<"Enter the lower number? ";
	cin >> low;
	cout<<"Enter the higher number? ";
	cin >> high;
	
	
	if(high > low)
	{
		if(isPrime(randNum))
			printRandomNumber(low, high);
	} 
	else //in case the lower is greater than the upper
	{
		while(low > high);
		{
			cout<<"Invalid input. Second value should be greater than first value."<<endl;
			cout<<"Enter the lower number? ";
			cin >> low;
			cout<<"Enter the higher number? ";
			cin >> high;
		}
	}
	
	return 0;
}

void printRandomNumber(int low, int high)
{
srand (time(NULL));
int randNum = rand() % (high-low) + low;
cout<<"Your random number is "<<randNum<<endl;
}
bool isPrime(int n)
{
	int i,count=0;
	if(n==1)
		return false;
	if(n==2)
		return true;
	if(n%2==0)
		return false;
	for(i=2;i<=n;i++)
	{
		if(n%i==0)
			count++;
	}
	if(count==2)
		return true;
	else
		return false;
}
You do test at most one value, which is . Of course that is not printing much.

You should test each value in a range.
For now you should forget about getting a valid range.
1
2
3
4
5
6
7
8
9
10
int main (){
	int low
	int high;
	std::cin >> low;
	std::cin >> high;
	if ( 0 < low ) {
		// test values in range [low..high]
	} 
	return 0;
}



What is the printRandomNumber? Where in the problem description was it mentioned? I thought you were supposed to print the prime values, not some random numbers.
Line 23: Why are you printing a random number? That does not appear to be part of the assignment.

Line 42: Do not call srand() multiple times. srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/

Line 20: Assuming you enter high > low, you're calling isPrime() on 0 (see line 9).

Lines 27-34: You input new values for low and high, but never use them.

Line 57-58: No reason to count number of times a number is evenly divisible. If it's evenly divisible, it's not prime. Just return false.

Line 55: Your termination condition should be < n (or better yet < sqrt(n)). Dividing n by itself will appear to be evenly divisible.
@keskiverto the printrandomnumber was from a different problem i was doing, where it would print a random number in an interval, i tried to adapt that code to this problem but im realizing i dont need that.

also, what does the std::cin>>low/high do?
what does the std::cin >> low; do?


It usually does the same as:
1
2
using std::cin;
cin >> low;

does, or the:
1
2
using namespace std;
cin >> low;


http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

Surely you don't have the habit of writing using namespace std; willy-nilly into your code?
Topic archived. No new replies allowed.