## Introduction to Prime Numbers in C#

Prime Numbers are the whole numbers greater than 1 that can be the factors for only the number 1 and for itself, i.e. the number ‘n’ can be divisible only by 1 or by ‘n’ itself. A few of the well-known prime numbers are 2, 3, 5, 7, 9, 11, 13, 17, 19, 23, etc. C# programs, in the subject of prime numbers, can be used for finding if the given number is a prime number or not, and for displaying all the prime numbers within a given range. This can be achieved in C# programs by defining logics using various loops and conditional statements, such as the for loop, if condition, if else condition, while loop, etc.

### Examples of Prime Numbers in C#

Let us try to conceptualize prime numbers with the following programming examples.

#### Example #1

C# program to print the list of all prime numbers between 1 to 100.

**Code:**

`using System;`

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args) // this function defines the entry point

{

bool Prime = true;

Console.WriteLine("Prime Numbers between 1 to 100 : ");

for (int a = 2; a <= 100; a++) //upper limit and lower limit are defined

{

for (int b = 2; b <= 100; b++)// base logic for the primality

{

if (a != b && a % b == 0) //modulo operators employed

{

Prime = false;

break;

}

}

if (Prime)

{

Console.Write("\t" + a); //printing the correct variable

}

Prime = true;

}

Console.ReadKey(); //hold the output screen

}

}

}

**Output:**

**Program Explanation:** The above program is a classic example of the use of loops and conditional operators for determining the prime numbers in a fixed range of numbers. The program above uses a lower limit of Natural number i.e.2 defining ‘a’ as a natural number in the range of 2 ranging to 99 with the post-operation increment of 1, the next step uses a variable ‘b’ with the similar range but bound to a condition in its upper limit is always less than ‘a’. The loop then passes through the range and uses the natural operation of modulo on the variable a by the divisor b.

The modulo operator returns 0 if a is perfectly divisible by b indicating the fact that b as a smaller natural number is a factor for the composite number a. We use a Boolean parameter Prime for a flag in case we are receiving the value of a % b not equal to zero. Now we employed the conditional operator to print the number in the output console in case we received the number as a prime number.

4.6 (7,921 ratings)

View Course

#### Example #2

Checking for prime numbers with for loop

**Code:**

`using System;`

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApplication1

{

class Program

{

public static void Main(string[] args)

{

int n, a, m = 0, PrimeIndicator = 0;

Console.Write("Please enter the number to check for Primality: ");

n = int.Parse(Console.ReadLine());

m = n / 2;

for (a = 2; a <= m; a++)

{

if (n % a == 0)

{

Console.Write("The entered number is not a Prime number .");

PrimeIndicator = 1;

break;

}

}

if (PrimeIndicator == 0)

Console.Write("The entered number is a Prime number . ");

}

}

}

**Output:**

**Program Explanation:** The program described above uses for loop for defining the conditions for primality. The input read field captures the user input an assigns to the variable n, the counter parses from the value of 2 to a value of n-1 and tests the conditions of divisibility to determine the number is prime or not.the additional feature involved in the program employs the use of a variable m with value of n/2 or exactly half of the initial user input, the program parses the loop over only up to a value of m.

#### Example #3

Primality using a while loop.

**Code:**

`using System;`

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

int n, i, a;

Console.Write("Enter any number: ");

n = Convert.ToInt32(Console.ReadLine()); // read the user input

a = 0;

i = 2;

while (i <= n / 2)

{

if (n % i == 0)

{

a = 1;

break;

}

i++;

}

if (a == 0)

Console.WriteLine(n + " Prime Number as there are no factors");

else

Console.WriteLine(n + " not a Prime Number ");

Console.ReadLine();

}

}

}

**Output:**

**Program Explanation:** The program above illustrates the process of determining the primality of a number using while loop in C#. The program above reads the user input from using the control reading command and parses over the range 2 to a value of user input divided by 2 to determine the value of a flag used to test the primality of a number. The value is assigned inside the loop and displays the result according to the value of a.

### Conclusion

Prime numbers thus are natural numbers greater than 1 with only factors being 1 and itself. A composite number can be broken down as a factor of primes, these numbers are called prime factors. The various programs illustrated above provide us with ways of implementing the primality of any number using the loops like do, for, while loops. Although the logic behind implementation of all the above programs is by finding the factors of a number by operating it with modulo operator yet the implementation of the same is controlled at various points depending upon the choice of the loop.

### Recommended Articles

This is a guide to Prime Numbers in C#. Here we discuss the Introduction and examples of prime numbers in c# along with code implementation. You may also look at the following articles to learn more –