C++ Program to print All Prime Numbers Between n and m using While Loop

Write C++ Program to print All Prime Numbers Between n and m using While Loop

// CPP Program to print All Prime Numbers Between n and m using While Loop

#include <iostream>
#include <stdbool.h>

using namespace std;

bool isPrime(int n)
{
	if(n == 1 || n ==0)
		return false;

	int i = 2;
	while(i < n)
	{
		if(n % i == 0)
		{
			return false;
		}
		i++;
	}

	return true;
}

int main()
{
	int n,m;
	cout << "Enter n :--> ";
	cin >> n;

	cout << "Enter m :--> ";
	cin >> m;

	cout << "Prime numbers between " << n << " to " << m << " :--> ";

	int i = n, j = m;
	while(i < j)
	{
		if(isPrime(i))
		{
			cout << i << " ";
		}
		i++;
	}

	return 0;
}

Output:

Enter n :--> 22
Enter m :--> 56
Prime numbers between 22 to 54 :--> 23 29 31 37 41 43 47 53

≪ Previous | Next ≫