prime

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

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

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

In this post, we will learn how to write a program to print first n prime numbers using  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

Print first n Prime numbers – method 1

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

Program 1

using System;
namespace primeNumber
{
    class first_N_Prime
    {
        static void Main(string[]args)
        {
        int num, count , i=3, x;
        Console.WriteLine("Enter the number of prime you want: ");
        num=Convert.ToInt32(Console.ReadLine());
        if(num>=1)
        {
            Console.WriteLine("First "+num+"prime numbers are:\n ");
            Console.WriteLine("2");
        }
        for(count=2; count<=num;){
            for(x=2; x<=i-1; x++)
            {
                if(i%x==0)
                    break;
            }
            if(x==i)
            {
                Console.WriteLine("\n "+i);
                count++;
            }
            i++;
        }
    }
}
}

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

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

2

3

5

7

11

13


Print first n Prime numbers – method 2

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

Program 2

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

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

first n prime number
Enter the number
5
2,3,5,7,11,

 

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# program to print first n prime numbers using for loop

C# program to print first n prime numbers using while loop

C# program to print first n prime numbers using do-while loop

C# program to print 1 o n prime numbers using for loop

C# program to print 1 to n prime numbers using while loop

C# program to print 1 to n prime numbers using do-while loop

 

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

 

PHP function to check whether a number is prime or not
Csharp program to print first n prime numbers using while loop
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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago