The Program in C# Program to Find Prime Numbers within Given Range in the world is given below:
using System;
namespace PrimeNumbersInRange
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family,enter the lower limit: ");
int lower = int.Parse(Console.ReadLine());
Console.WriteLine("Similarly, enter the upper limit: ");
int upper = int.Parse(Console.ReadLine());
Console.WriteLine("Okay, the prime numbers in the given range are: ");
for (int i = lower; i <= upper; i++)
{
if (IsPrime(i))
{
Console.WriteLine(i);
}
}
Console.ReadLine();
}
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:
- The program begins by importing the “System” namespace, which provides access to various types and methods that are used in the program.
- The code defines a namespace named “PrimeNumbersInRange”. The namespace is a way to organize and group related classes.
- 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.
- 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”.
- 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”.
- The Main method then prints “Okay, the prime numbers in the given range are: ” to the console.
- The Main method then uses a for loop to iterate over the numbers in the specified range, from “lower” to “upper”.
- For each number in the range, the Main method calls the IsPrime method and passes the number as an argument.
- 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.
- 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.
- If the for loop completes without finding any divisible numbers, the IsPrime method returns true, indicating that the passed number is a prime number.
- 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.
- 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.