Codeforcoding

Program to print prime numbers between 1 to n using C#

Program to print prime numbers from 1 to n using C#

In this tutorial, we will discuss the concept of the Program to print prime numbers between numbers of 1 to n using C#

In this post, we will learn how to print prime numbers between numbers of 1 to n using C# programming language 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…

Program to print prime numbers between numbers of 1 to n using C#
Prime numbers 1 to n

Print prime numbers between numbers 1 to n

Prime numbers between 1 to n – 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 the C# language.

Program 1

using System;
namespace primeNumber
{
    class first_N_Prime
    {
        static void Main(string[]args)
        {
            bool isPrime=true;
        Console.WriteLine("Enter the number of prime you want: ");
        int num=Convert.ToInt32(Console.ReadLine());
        
        for(int i=2; i<=num; i++){
            for(int j=2; j<=num; j++)
            {
            if(i!=j && i%j==0){
                    isPrime=false;
                break;
            }
        }
            if(isPrime)
            {
                Console.Write("\n "+i);
            }
            
            isPrime=true;
        }
        Console.ReadKey();
    }
}
}

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

Enter the number of primes you want:
8

2
3
5
7

 

Prime numbers between 1 to n – method 2

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 the C# language.

Program 2

using System;
namespace primeNumber
{
    class first_N_Prime
    {
        static void Main(string[]args)
        {
            int i,j,num,flag;
        Console.WriteLine("first n prime number between 1 to n");
        num=int.Parse(Console.ReadLine());
        
        for(i=2; i<=num; i++){
            flag=1;
            for(j=2; j<=i/2; j++)
            {
            if(i%j==0){
                    flag=0;
                break;
            }
        }
            if(flag==1)
                Console.Write(i+",");
        }
        Console.ReadKey();
    }
}
}

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

first n prime number between 1 to n
20
2,3,5,7,11,13,17,19,

 

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

Csharp program to print first n prime numbers using while loop
Program for printing first n prime numbers using for loop in C#
Exit mobile version