Write a program to display first n prime numbers.

This answer is restricted. Please login to view the answer of this question.

Login Now

The program to print N prime number is

#include <stdio.h>

int main(){

    int n, count = 1, flag, i = 2, j;

    printf("Enter how many prime numbers? \n");
    scanf("%d", &n);

    /* Generating prime numbers */
    while (count <= n){

        flag = 0;

        for (j = 2; j <= i / 2; j++){
            if (i % j == 0)
            {
                flag = 1;
                break;
            }
        }

        if (flag == 0){
            printf("%d\t", i);
            count++;
        }
        i++;

    }
    
    return (0);
}

The output of above program is

Enter how many prime numbers? 
10
2       3       5       7       11      13      17      19      23      29
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .