# C# Program to Find Prime Numbers within Given Range

The Program in C# Program to Find Prime Numbers within Given Range in the world is given below:

using System;

{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family,enter the lower limit: ");

Console.WriteLine("Similarly, enter the upper limit: ");

Console.WriteLine("Okay, the prime numbers in the given range are: ");
for (int i = lower; i <= upper; i++)
{
if (IsPrime(i))
{
Console.WriteLine(i);
}
}
}

static bool IsPrime(int num)
{
if (num <= 1)
{
return false;
}

for (int i = 2; i <= Math.Sqrt(num); i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
}
}

## Output:

Hello Codeauri Family,enter the lower limit:
1
Similarly, enter the upper limit:
10
Okay, the prime numbers in the given range are:
2
3
5
7

## Pro-Tips💡

Here are the step by step execution of above program:

1. The program begins by importing the “System” namespace, which provides access to various types and methods that are used in the program.
2. The code defines a namespace named “PrimeNumbersInRange”. The namespace is a way to organize and group related classes.
3. Within the namespace, the class named “Program” is defined. The Main method is defined inside this class and serves as the entry point for the program.
4. The Main method first asks the user to input the lower limit for the range of numbers. This input is read from the console and stored in the integer variable named “lower”.
5. Similarly, the Main method asks the user to input the upper limit for the range of numbers. This input is read from the console and stored in the integer variable named “upper”.
6. The Main method then prints “Okay, the prime numbers in the given range are: ” to the console.
7. The Main method then uses a for loop to iterate over the numbers in the specified range, from “lower” to “upper”.
8. For each number in the range, the Main method calls the IsPrime method and passes the number as an argument.
9. The IsPrime method checks if the passed number is a prime number. It starts by checking if the number is less than or equal to 1. If it is, it returns false, as 1 is not considered a prime number.
10. The IsPrime method then uses another for loop to check if the passed number is divisible by any number from 2 to the square root of the number. If it finds a number that the passed number is divisible by, it returns false, as the number is not prime.
11. If the for loop completes without finding any divisible numbers, the IsPrime method returns true, indicating that the passed number is a prime number.
12. Back in the Main method, if the IsPrime method returns true for a number in the range, the Main method prints that number to the console.
13. Finally, the Main method waits for the user to press the enter key, which allows the console window to remain open until the user closes it.

### Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

### Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

### Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

## Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

## C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

## C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

## C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

## C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

## C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X