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

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

#include <stdio.h>
#include <stdbool.h>

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;

	printf("Enter the number :--> ");
	scanf("%d", &n);

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

Output:

Enter the number :--> 25

The prime numbers are :2 3 5 7 11 13 17 19 23