For ststement

Write a program for printing first n prime numbers in C#

Write a program for printing first n prime numbers in C#

In this article, we will discuss the concept of the Write a program for printing first n prime numbers in C#

In this post, we are going to learn how to write a program to find first n prime numbers and display the result on the screen in C# programming language.

Code to print first n prime number

Print the prime number in C# -using for loop

In this program, the user initiates the values to variables and the program asked to enter value for num, then it will print first n prime nunbers using for loop in the C# programming language.

Program 1

//Write a program for printing first n prime numbers in C#
// using for loop
using System;
namespace primeNumber
{
  class first_N_Prime
  {
      static void Main(string[]args)
      {
      int i=1,j=1,num,count=0;
      //variable decaration 
    Console.WriteLine("first n prime number ");
    Console.WriteLine("Enter the number");
    num=int.Parse(Console.ReadLine());
    //\reading the input
    for(int n=0; n<num;  i++){//outer for loop
    
    count=0;
    for(j=1;j<=i; j++){//inner for loop
      if(i%j==0)
          count++;
          
    }
      if(count==2){
        Console.Write(i+",");
        n++;
    }
  
  }
}
}
}

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

first n prime number
Enter the number
15
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,

 

 

Print the prime number in C# -using while loop

In this program, the user initiates the values to variables and the program asked to enter value for num, then it will print first n prime nunbers  using while loop in the C# programming language.

Program 2

// Write a program for printing first n prime numbers in C#
// using while loop

using System;
namespace primeNumber
{
  class first_N_Prime
  {
      static void Main(string[]args)
      {
      int i=1,j=1,num,count=0;
      //variable decaration 
    
    Console.WriteLine("Enter the number");//ask input rom the user
    num=int.Parse(Console.ReadLine());
    //reading the input
    Console.WriteLine("print first "+ num+ " prime number \n");
    int n=0; 
    while(n<num){
    //outer while loop
    count=0;
    j=1;
    while(j<=i){//inner while loop
      if(i%j==0)
          count++;
           j++;
    }
      if(count==2){
        Console.Write(i+",");
        //print prime numbers
        n++;
    }
    i++;
  }
}
}
}

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

Enter the number
12
print first 12 prime number

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

 

Print the prime number in C# -using do-while loop

In this program, the user declare and initiates the values to variables and the program asked to enter value for num, then it will print first n prime nunbers using do-while loop in the C# programming language.

Program 3

// Write a program for printing first n prime numbers in C#
// using do-while loop

using System;
namespace primeNumber
{
  class first_N_Prime
  {
      static void Main(string[]args)
      {
      int i=1,j=1,num,count=0;
      //variable decaration 
    
    Console.WriteLine("Enter the number");//ask input rom the user
    num=int.Parse(Console.ReadLine());
    //reading the input
    Console.WriteLine("print first "+ num+ " prime number \n");
    int n=0; 
    do{
    //outer do-while loop
    count=0;
    j=1;
    do{//inner do-while loop
      if(i%j==0)
          count++;
           j++;
    }while(j<=i);
      if(count==2){
        Console.Write(i+",");
        //print prime numbers
        n++;
    }
    i++;
  }while(n<num);
}
}
}

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

Enter the number
8
print first 8 prime number

2,3,5,7,11,13,17,19,

 

Suggested 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

C# program to check whether a number is prime or not

C# function to check whether a number is 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

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

9 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

10 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

10 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

10 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

10 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

10 months ago

This website uses cookies.