Advertisement
dimipan80

Advanced Topics 3. Primes in Given Range

Jul 1st, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. // Write a method that calculates all prime numbers in given range and returns them as list of integers.Write a method to print a list of integers. Write a program that enters two integer numbers (each at a separate line) and prints all primes in their range, separated by a comma.
  2.  
  3. namespace _03.PrimesInGivenRange
  4. {
  5.     using System;
  6.     using System.Collections.Generic;
  7.  
  8.     public class PrimesInGivenRange
  9.     {
  10.         public static void Main(string[] args)
  11.         {
  12.             checked
  13.             {
  14.                 int startNum;
  15.                 do
  16.                 {
  17.                     Console.Write("Enter a non-negative Integer number: ");
  18.                 }
  19.                 while (!int.TryParse(Console.ReadLine(), out startNum) || startNum < 0);
  20.  
  21.                 int endNum;
  22.                 do
  23.                 {
  24.                     Console.Write("Enter other non-negative Integer number, Bigger from the First: ");
  25.                 }
  26.                 while (!int.TryParse(Console.ReadLine(), out endNum) || endNum <= startNum);
  27.  
  28.                 List<int> primes = FindPrimesInGivenRangeAndPutThemInTheList(startNum, endNum);        
  29.                 PrintListOfIntegers(primes);
  30.             }
  31.         }
  32.  
  33.         private static void PrintListOfIntegers(List<int> numsList)
  34.         {
  35.             checked
  36.             {
  37.                 if (numsList.Count > 0)
  38.                 {
  39.                     Console.WriteLine("The Prime Numbers In Given Range are:");
  40.                     for (int i = 0; i < numsList.Count; i++)
  41.                     {
  42.                         Console.Write(numsList[i]);
  43.                         if (i < numsList.Count - 1)
  44.                         {
  45.                             Console.Write(", ");
  46.                         }
  47.                     }
  48.  
  49.                     Console.WriteLine();
  50.                 }
  51.                 else
  52.                 {
  53.                     Console.WriteLine("In Given Range No Any Prime Number!");
  54.                     Console.WriteLine("(empty list)");
  55.                 }
  56.             }
  57.         }
  58.  
  59.         private static List<int> FindPrimesInGivenRangeAndPutThemInTheList(int start, int end)
  60.         {
  61.             checked
  62.             {
  63.                 List<int> primes = new List<int>();
  64.                 for (int num = start; num <= end; num++)
  65.                 {
  66.                     bool isPrimeNum = CheckTheIntegerNumberIsPrimeOrNot(num);
  67.                     if (isPrimeNum)
  68.                     {
  69.                         primes.Add(num);
  70.                     }
  71.                 }
  72.  
  73.                 return primes;
  74.             }
  75.         }
  76.  
  77.         private static bool CheckTheIntegerNumberIsPrimeOrNot(int num)
  78.         {
  79.             checked
  80.             {
  81.                 if (num < 2)
  82.                 {
  83.                     return false;
  84.                 }
  85.                 else
  86.                 {
  87.                     bool isPrime = true;
  88.                     int maxDivider = (int)Math.Sqrt(num);
  89.                     for (int i = 2; i <= maxDivider; i++)
  90.                     {
  91.                         if (num % i == 0)
  92.                         {
  93.                             isPrime = false;
  94.                             break;
  95.                         }
  96.                     }
  97.  
  98.                     return isPrime;
  99.                 }                
  100.             }
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement