C Program to Print first n Prime Number with explanation

In this tutorial, we are going to write a program in C to print the first N Prime Number. Basically, on the basis of the given input, our program will print the Prime Number.

For example, if we want to print the first 5 prime numbers, Our program will take 5 as an input and it will print 2,3,5,7,11 as an output.

What is Prime number?

A prime number is a number that can only divide by 1 and the number itself.

for example, 2, 3, 5 etc are the prime number. Here 2 and 3 can only be divided by 1 and number itself.

But 4 or 6 is not a prime number because 4 can be divided by 2 and 6 can be divided by 2 and 3.

Let’s see the program in C

C Program to Print first n Prime Number

#include<stdio.h>
void main() {
  int i = 0, n, temp, temp1 = 1;
  printf("Please give input a number: ");
  scanf("%d",&n);
  printf("Prime numbers Upto %d are \n",n);
  while (temp1<=n) {
    temp = 0;
    for (i=2;i<=(temp1/2); i++) {
      if (temp1%i==0) {
        temp = 1;
        break;
      }
    }
    if (temp == 0)
      printf("%d \n",temp1);
    temp1++;
  }
}

Output

Please give input a number: 26
Prime numbers Upto 26 are 
1 
2 
3 
5 
7 
11 
13 
17 
19 
23

[wpusb]