C++ Program to Print Prime Numbers Between 1 to n using While Loop

C++ Program to Print Prime Numbers Between 1 to n using While Loop

Write C++ Program to Print Prime Numbers Between 1 to n using While Loop

// CPP Program to Print Prime Numbers Between 1 to n 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;

	cout << "Enter the number :--> ";
	cin >> n;

	int i = 1 ;
	cout << "Prime numbers between 1 to " << n << " :--> ";
	while(i < n)
	{
		if(isPrime(i))
		{
			cout << i << " ";
		}
		i++;
	}
	return 0;
}

Output:

Enter the number :--> 25
The prime numbers are :2 3 5 7 11 13 17 19 23