Find elements

Program to find first n prime numbers in C language

Program to find first n prime numbers in C language

In this tutorial, we will discuss the concept of an Example  Program to find first n prime numbers in C language

In this post, we are going to learn how to write a program to find the first n  prime number using for, while, and do-while loops in C programming language:

Prime numbers

The number that can be divided by 1 and itself only. it is called the prime number

for Example 2,3,5,7,11,13….

To understand this example program, you should have previous knowledge of the following Java topics

for loop in C language

while  loop in C language

do-while  loop in C language

if statement in C language

 

Example program to print first n prime numbers

Print the first n prime number using the for loop

In this program, we will print the first “n” prime numbers, using for loop. first, the user is asked to enter a number randomly for n, and then the program prints the prime numbers 1 to n in C language.

Program 1

//Program to find first n prime numbers in C language
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i=3,count,num;
    //declare and initialize the variable
    printf("Enter the number of prime you want\n");
    //ask input from the user and store in n
    scanf("%d",&n);

    if(n>=1){
        printf("The first %d primes numbers are: \n",n);
        //Generating prime numbers
        printf("2\n");
    }
    for(count=2; count<=n; i++){
        for(num=2; num<i; num++){
            if(i%num==0)
                break;
        }
        if(num==i){
            printf("%d \n",i);
            count++;
        }
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the number of primes you want
The First 6 prime numbers are

2
3
5
7
11
13

 

Print the first n prime number using the while loop

In this program, we will print the first “n” prime numbers, using a while loop. first, the user is asked to enter a number randomly for n, and then the program prints the prime numbers 1 to n   in C language.

Program 2

//Program to find first n prime numbers in C language
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i=3,count,num;
    //declare and initialize the variable
    printf("Enter the number of prime you want\n");
    //ask input from the user and store in n
    scanf("%d",&n);

    if(n>=1){
        printf("The first %d prime numbers are: \n",n);
        //Generating prime numbers
        printf("2\n");
    }
    count=2;
    while(count<=n){
            num=2;
        while(num<i){
            if(i%num==0)
                break;
     num++;
        }
        if(num==i){
            printf("%d \n",i);
            count++;
        }
        i++;
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the number of primes you want
The first 10 prime numbers are

2
3
5
7
11
13
17
19
23
29

 

Print the first n prime number using the do-while loop.

In this program, we will print the first “n” prime numbers, using the do-while loop. first, the user is asked to enter a number randomly for n, and then the program prints the prime numbers 1 to n.

Program 3

//Program to find first n prime numbers in C language
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i=3,count,num;
    //declare and initialize the variable
    printf("Enter the number of first n prime you want\n");
    //ask input from the user and store in n
    scanf("%d",&n);

    if(n>=1){
        printf("First %d prime numbers are: \n",n);
        //Generating prime numbers
        printf("2\n");
    }
    count=2;
   do{
            num=2;
        do{
            if(i%num==0)
                break;
     num++;
        }while(num<i);
        if(num==i){
            printf("%d \n",i);
            count++;
        }
        i++;
    } while(count<=n);
    getch();
    return 0;
}


When the above code is executed, it produces the following result

Enter the number of first n primes you want
The first 12 prime numbers are

2
3
5
7
11
13
17
19
23
29
31
37

 

Suggested for you

Type of variable in C language

The operator in C language

 

Program to find whether a Number is Prime or Not in C++

Program to find whether a Number is Prime or Not in C

Program to find whether a Number is Prime or Not in Java

Program to find whether a Number is Prime or Not in Python

 

C++ program to find first n prime numbers

Java program to find first n prime numbers

C program to find first n prime numbers

 

C code to 5 ways to check whether the given integer is Even or Odd

Java code to 5 ways to check whether the given integer is Even or Odd

C++ code to 5 ways to check whether the given integer is Even or Odd

Python code to 5 ways to check whether the given integer is Even or Odd

 

Write a program to find first n prime numbers in Java
C++ program: find first n prime numbers
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

1 week ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

3 weeks ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

4 weeks ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

4 weeks ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago