prime

Csharp program to print first n prime numbers using while loop

Csharp program to print first n prime numbers using while loop

In this tutorial, we will discuss the concept of the Csharp program to print first n prime numbers using a while loop

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

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_Num
    {
        static void Main(string[]args)
        {
            int i=1,j=1,count=0,n=0;
        Console.WriteLine("first 15 prime number ");
        //read input from the user
        while(n<15){//outter while
        j=1;
        count=0;
        while(j<=i){//inner while
            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 15 prime number
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,

 

 

Print first n Prime numbers – method 2

This program allows the user to enter a positive number for num 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_Num
    {
        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());
        //read input from the user
        while(n<num){//outter while
        j=1;
        count=0;
        while(j<=i){//inner while
            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
10
2,3,5,7,11,13,17,19,23,29,

 

 

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

Write a program for printing first n prime numbers using C#
Program to print prime numbers between 1 to n using C#
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…

3 weeks ago

C# Full Pyramid star pattern program

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

1 month 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…

1 month 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…

1 month 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