Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Java program for display first n prime numbers

Program for print first n prime numbers in C

Posted on December 17, 2020December 17, 2020

Table of Contents

  • Program for print first n prime numbers in C
    • Code to display first n prime numbers
      • Code to display first n prime numbers using for loop
      • Code to display first n prime numbers using while loop
      • Code to display first n prime numbers using do-while loop

Program for print first n prime numbers in C

In this article, we will discuss the concept of Program for print first n prime numbers in C

In this code, we are going to learn how to find first n prime numbers  using different methods in C language.

This is done using for loop,while loop and ,do-while loop in C language

Code to display first n prime numbers

Code to display first n prime numbers using for loop

In this program, we will display first n  prime numbers using for loop in C language

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i=3,count,num;
    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("First %d prime numbers are: \n",n);
        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  ",i);
            count++;
        }
    }
    return 0;
}

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

Enter the number of prime you want
15
First 15 prime numbers are :
2  3  5  7  11  13  17  19  23  29  31  37  41  43  47

 

Code to display first n prime numbers using while loop

In this program, we will display first n  prime numbers using while loop in C language

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i=3,count,num;
    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("First %d prime numbers are: \n\n",n);
        printf("2 ");
    }
    count=2;
    while(count<=n){
            num=2;
        while(num<i){
            if(i%num==0)
                break;
     num++;
        }
        if(num==i){
            printf("%d ",i);
            count++;
        }
         i++;
    }
    getch();
    return 0;
}

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

Enter the number of prime you want
10
First 10 prime numbers are :
2  3  5  7  11  13  17  19  23  29

 

Code to display first n prime numbers using do-while loop

In this program, we will display first n  prime numbers using do-while loop in C language

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i=3,count,num;
    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("First %d prime numbers are: \n\n",n);
        printf("2 ");
    }
    count=2;
    do{
            num=2;
        do{
            if(i%num==0)
                break;
     num++;
        }while(num<i);
        if(num==i){
            printf("%d ",i);
            count++;
        }
         i++;
    }while(count<=n);
    getch();
    return 0;
}


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

Enter the number of prime you want
8
First 8 prime numbers are :
2  3  5  7  11  13  17  19

 

Suggested post

for loop in C language

while loop in C language

do-while loop in C language

if statements in C language

function in C language

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

 

Code to print prime numbers from 1 to 100 or 1 to n in Java

Code to print prime numbers from 1 to 100 or 1 to n in C

Code to print prime numbers from 1 to 100 or 1 to n in C++

Code to print prime numbers from 1 to 100 or 1 to n in Python

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes