Uncategorized

C# program to print all prime numbers between 1 to n

C# program to print all prime numbers between 1 to n

In this article, we will discuss the concept of the C# program to print all prime numbers between 1 to n

In this post, we are going to learn how to write a program to find between 1 to n prime numbers and display the result on the screen in C# programming language.

Code to print 1 ton prime number

Print the prime number in C# -using for loop -#1

In this program, the user initiates the values to variables and the program asked to enter value for num, then it will print prime nunbers 1 to n using for loop in the C# programming language.

Program 1

// Write a program for printing 1 to n prime numbers in C#
// using for loop

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

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

print 1 to n prime numbers
Enter the number
25
2,3,5,7,11,13,17,19,23,

 

Print the prime number in C# -using for loop – #2

In this program, the user is asked to enter value for variable num, then it will print prime numbers 1 to n using for loop in the C# programming language.

Program 2

// Write a program for printing 1 to n prime numbers in C#
// using for loop

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

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

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

 

Print the prime number in C# -using while loop -#1

In this program, the user initiates the values to variables and the program asked to enter value for num, then it will print prime nunbers 1 to n using while loop in the C# programming language.

Program 3

// Write a program for printing 1 to n prime numbers in C#
// using while loop

using System;
namespace primeNumber
{
  class print_1_to_N_Prime
  {
      static void Main(string[]args)
      {
      int i=1,num,count=0;
      //variable decalaration 
    Console.WriteLine("print 1 to n prime  numbers ");
    Console.WriteLine("Enter the number");
    num=int.Parse(Console.ReadLine());
    //reading the input
    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+",");
        //print prime numbers
    }
    i++;
     n++;
  }
}
}
}

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

print 1 to n prime numbers
Enter the number
40
2,3,5,7,11,13,17,19,23,29,31,37,

 

Print the prime number in C# -using while loop – #2

In this program, the user is asked to enter value for variable num, then it will print prime numbers 1 to n using while loop in the C# programming language.

Program 2

// Write a program for printing 1 to n prime numbers in C#
// using while loop

using System;
namespace primeNumber
{
  class first1toNPrime
  {
    static void Main(string[]args)
      {
    Console.WriteLine("print 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 prime number between 1 to n
Enter the number
40
2,3,5,7,11,13,17,19,23,29,31,37,

 

 

Print the prime number in C# -using do-while loop -#1

In this program, the user initiates the values to variables and the program asked to enter value for num, then it will print prime nunbers 1 to n using do-while loop in the C# programming language.

Program 5

// Write a program for printing 1 to n prime numbers in C#
// using do-while loop

using System;
namespace primeNumber
{
  class print_1_to_N_Prime
  {
      static void Main(string[]args)
      {
      int i=1,num,count=0;
      //variable decalaration 
    Console.WriteLine("print 1 to n prime  numbers ");
    Console.WriteLine("Enter the number");
    num=int.Parse(Console.ReadLine());
    //reading the input
    int n=0;
  do{
    
    count=0;
    int j=1; 
    do{
      if(i%j==0)
          count++;
          j++;
    }while(j<=i);
      if(count==2){
        Console.Write(i+",");
        //print prime numbers
    }
    i++;
     n++;
  } while(n<num);
}
}
}

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

print 1 to n prime numbers
Enter the number
50
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,

 

Print the prime number in C# -using do-while loop – #2

In this program, the user is asked to enter value for variable num, then it will print prime numbers 1 to n using do-while loop in the C# programming language.

Program 2

// Write a program for printing 1 to n prime numbers in C#
// using do-while loop

using System;
namespace primeNumber
{
  class first1toNPrime
  {
    static void Main(string[]args)
      {
    Console.WriteLine("print 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;
    do{
      var j=2;
      
      do{
      if(i!=j && i%j==0){
          isPrime=false;
        break;
      }
      j++;
    }while(j<=num);
      if(isPrime){
        Console.Write(i+",");
    }
    isPrime=true;
    i++;
    
  }while(i<=num);
  Console.ReadKey();
}
}
}

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

print prime number between 1 to n
Enter the number
75
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,

 

Suggested post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

C# program to check whether a number is prime or not

C# function to check whether a number is prime or not

 

Code to print prime numbers from 1 to 100 or 1 to n in Java

Code to print prime numbers from 1 to 100 or 1 to n in C

Code to print prime numbers from 1 to 100 or 1 to n in C++

Code to print prime numbers from 1 to 100 or 1 to n in Python

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.