Codeforcoding

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

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

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

In this post, we will learn how to print prime numbers between numbers of 1 to n  using  do-while loop in 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 1 to n

Prime numbers between 1 to n using do-while loop

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

Program 1

using System; 
namespace primeNumber {
    class first1toNPrimewhie { static void Main(string[]args) { 
        int num=25; 
        Console.WriteLine("print prime numbers between 1 to"+num); 
        
        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 numbers between 1 to25
2,3,5,7,11,13,17,19,23,

 

Prime numbers between 1 to n using do-while loop- Entered by user

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

Program 2

// C# code to print 1 to  n prime numbers using do-while loop
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;
        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 1 to n prime number between 1 to n
Enter the number
15
2,3,5,7,11,13,

 

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 1 to n prime numbers using for loop in C#
C# code to print flrst n prime numbers using do-while loop
Exit mobile version