C# code to print flrst n prime numbers using do-while loop

C# code to print flrst n prime numbers using do-while loop

In this tutorial, we will discuss the concept of the C# code to print flrst n prime numbers using do-while loop

In this post, we will learn how to print first n prime numbers using a do-while loop in C# programming language with Example program.

First, n prime

Prime number

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

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

Print first n prime numbers using do-while

Print first n Prime numbers

In this program,  the user declares and initializes variables then it will print first n prime numbers,  using the do-while loop in the C# language.

Program 1

// C# code to print first n prime numbers using do-while loop
using System;
namespace primeNumberdo
{
    class first_N_Prime
    {
        static void Main(string[]args)
        {
            int i=1,j=1,num=20,count=0,n=0;
        Console.WriteLine("print first "+num+" prime numbers");
        do{
        j=1;
        count=0;
        do{
            if(i%j==0)
                    count++;
                    j++;
        }while(j<=i);
            if(count==2){
                Console.Write(i+",");
                n++;
        }
        i++;
    }while(n<num);
}
}
}

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

print first 20 prime numbers
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,

 

Print first n Prime numbers- Entered by user

This program allows the user to enter a positive number for num, then it will print first n prime numbers,  using the do-while loop in the C# language.

Program 2

// C# code to print flrst  n prime numbers 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,n=0;
        Console.WriteLine("print first n prime number ");
        Console.WriteLine("Enter the number");
        num=int.Parse(Console.ReadLine());
        
        do{
        j=1;
        count=0;
        do{
            if(i%j==0)
                    count++;
                    j++;
        }while(j<=i);
            if(count==2){
                Console.Write(i+",");
                n++;
        }
        i++;
    }while(n<num);
}
}
}

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

print first n prime number
Enter the number
12
2,3,5,7,11,13,17,19,23,29,31,37,

 

 

Suggested for you

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

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

Program to find whether a Number is Prime or Not using the PHP function

 

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

C# code to print 1 to n prime numbers using do-while loop
C# program to print all prime numbers between 1 to 100 using for loop
C#C# programloops