Program to print first n prime numbers in C Language

Generate-First-n-prime-numbers-in-C-Language

Introduction:

Write a Program to print first n prime numbers in C programming language. We are going to use two for loops (Nested Loops) to calculate the first n prime numbers. The program is going to accept a positive number ( n) from the user and generates that many(first n) prime numbers.

If the user enters a Negative number program should display an error message – Please enter positive number

Example Inputs and Output:

Input:

Enter how many prime numbers do you want to print : 10

Output:

2 3 5 7 11 13 17 19 23 29

Related Prime Numbers Programs:

This article is one of the articles in the Series of Prime number programs in C language. In our previous article, We discussed 

First n prime numbers in C Program Explanation :

This Program accepts one integer from the user and prints first n prime numbers. If the user enters a Negative number, We display an error message and Ask for the user’s input again using the Goto Statement.

Here we are using two loops one outer loop and one inner loop.

The Outer loop will help us to track the number of prime numbers we printed onto the console, The Outer loop starts from the number 2 ( as prime numbers start from 2) and goes until we find n (user provided number) Prime Numbers. and The Inner loop is used to check if the given number is a prime number or not ( basic prime number checking logic).

As a result, the outer loop will determine whether or not any number between 2 and n are prime numbers.

We will also use a FLAG variable, The FLAG variable initialized with Zero (0) and We will update the FLAG variable inside of the Inner Loop with 1 whenever we found any number, which is not a prime ( i.e which is perfectly divisible by the other number)

Once we came out of the Inner loop, We will check if the FLAG is equal to zero(0). If so, Then the number i will be a prime number. We will print it onto the console and go to the next number.

We also maintain a Counter variable named cnt, The cnt variable will be incremented whenever we found any prime numbers. Our OuterLoop termination condition depends on the cnt variables which is cnt < n (Outer loop condition).

Here are the first 10 prime numbers:

nnth Prime Number
12
23
35
47
511
613
717
819
923
1029

Program to generate First n prime numbers in C Language:

Program Output:

We are compiling the program using the GCC compiler.

$ gcc first-n-primes.c

The above gcc command generates an executable file a.out. Run the executable file.

As you can see user entered n as 10, So the program generated 10 prime numbers.

Similarly, try a few more examples

Let’s try an example with a Negative number

As you can see from the above output, The program is displaying an Error message. and asking the user for the new Input number.

program-to-find-first-n-prime-numbers-in-C

Related Programs:

  1. C Program to generate Fibonacci series upto Given Number.
  2. C Program to generate First N Fibonacci numbers.
  3. C program to Calculate Nth Fibonacci Number.
  4. Generating Fibonacci numbers Series using Function.
  5. Generating Nth Fibonacci number using Recursion.

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

4 Responses

  1. […] C Program to print First N Prime numbers […]

  2. […] C Program to print First N Prime numbers […]

  3. […] C Program to print First N Prime numbers […]

  4. […] C Program to print First N Prime numbers […]

Leave a Reply