C Program to Print First n Prime Numbers using While loop

Write C Program to Print First n Prime Numbers using While loop

//Write a program to print first n prime numbers 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("First %d Prime Numbers are :--> ", n);

    int count = 0;
    i = 2;
	while(count < n)
	{
		if(isPrime(i))
		{
			printf("%d ",i);
			count++;
		}
		i++;
	}
	return 0;
}

Output:

Enter the number :--> 7
First 7 Prime Numbers are :--> 2 3 5 7 11 13 17

≪ Previous | Next ≫