how to print all prime numbers between 2 numbers

Can anyone help me with this question please

Write a program that prompts the user to enter two positive integers m and n where m < n then output all prime numbers in the range m and n, inclusive. You must write a function that would test a number whether it is prime.

thanks in advance

Hi @saraA,

first off you
have to study the
algorithm to find
prime numbers i
saw your last code
and after all you
are not lost at all.

-This is not the best way but
works-


Enter two numbers to
find primes
First: 41
Second: 20
Enter first number again: 20
Second: 41

Prime numbers in the range 20 41
23 29 31 37 41
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
//prime.cpp
//##

#include <iostream>
using std::cout;
using std::cin;
using std::endl;


bool isPrime(int number); //function prototype

int main(){
int m;
int n;

	cout<<"Enter 2 numbers to\nfind primes\nFirst: ";
	cin>>m;
	cout<<"Second: ";
	cin>>n;
	while(!(m<n)){//while m is not less than n 
		cout<<"Enter first number again: ";
		cin>>m;
        cout<<"Second: ";
        cin>>n;
	}//end while

	for(int number=m;number<=n;number++){
		if(isPrime(number))
			cout<<number<<' ';
	}//end for
	cout<<endl;


return 0; //inidcates success
}//end main

bool isPrime(int number){
	int divisible_numbers=0;

		for(int i=1;i<=number;i++){
			if(number%i==0)
				++divisible_numbers;
		}//end for
		if(divisible_numbers==2)
			return true;


return false;
}//end function isPrime 
Last edited on
Topic archived. No new replies allowed.