jae-l
1/9/2017 - 5:29 PM

Check prime numbers in an interval or range [N...M]. From Fundamentals of Computer Programming with C# http://www.introprogramming.info/wp-c

Check prime numbers in an interval or range [N...M]. From Fundamentals of Computer Programming with C# http://www.introprogramming.info/wp-content/uploads/2013/07/Books/CSharpEn/Fundamentals-of-Computer-Programming-with-CSharp-Nakov-eBook-v2013.pdf

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

Console.Write("m = ");
int m = int.Parse(Console.ReadLine());

for (int num = n; num <= m; num++)
{
  bool prime = true;
  int divider = 2;
  int maxDivider = (int) Math.Sqrt(num);
  
  while (divider <= maxDivider)
  {
    if (num % divider == 0)
    {
      prime = false;
      break;
    }
    divider++;
  }
  
  if (prime)
  {
    Console.Write(" " + num);
  }
}

//  n = 3
//  m = 9
//   3 5 7