prime

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

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

In this tutorial, we will discuss the concept of the C# program to print 1 to n prime numbers using the while loop

In this post, we will learn how to print prime numbers between numbers of 1 to n using C# programming language using  while loop with Example program.

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…

Prime numbers 1 to n

Print prime numbers between numbers of 1 to n

Prime numbers between 1 to n using while- method 1

This program allows the user to enter a positive number for n then it will print prime numbers between numbers of 1 to n,  using while  loop in the C# language.

Program 1

using System;
namespace primeNumber
{
    class first1toNPrimewhie
    {
        static void Main(string[]args)
        {
        Console.WriteLine("print 1 to n prime number between 1 to n");
        Console.WriteLine("Enter the number");
        int num=int.Parse(Console.ReadLine());
        //reading the input
        bool isPrime=true;
        var i=2;
        while(i<=num){
var j=2;
            while(j<=num)
            {
            if(i!=j && i%j==0){
                    isPrime=false;
                break;
            }
            j++;
        }
            if(isPrime){
                Console.Write(i+",");
        }
        isPrime=true;
        i++;
    }
    Console.ReadKey();
}
}
}

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

print 1 to n prime number between 1 to n
Enter the number
30
2,3,5,7,11,13,17,19,23,29,

 

Prime numbers between 1 to n using while- method 2

This program allows to declare variables and enter a value for num, and it will print prime numbers between numbers of 1 to n,-using while  loop in the C# language

Program 2

using System;
namespace primeNumber
{
    class first_N_Prime
    {
        static void Main(string[]args)
        {
            int i=1,num,count=0;
        Console.WriteLine("first n prime first number ");
        Console.WriteLine("Enter the number");
        num=int.Parse(Console.ReadLine());
        int n=0;
        while( n<num){
        
        count=0;
        int j=1; 
        while(j<=i){
            if(i%j==0)
                    count++;
                j++;
        }
            if(count==2){
                Console.Write(i+",");
                
        }
        i++;
        n++;
    }
}
}
}

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

first n prime first number
Enter the number
25
2,3,5,7,11,13,17,19,23,

 

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

Program for printing first n prime numbers using for loop in C#
Program for printing 1 to n prime numbers using for loop in 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

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