Advertisement
desislava777

07. Primes in Given Range

Jun 11th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication10
  8. {
  9. class Program
  10. {
  11. static bool isPrime(int n)
  12. {
  13. bool prime = true;
  14. if (n == 0 || n == 1)
  15. prime = false;
  16. for (int i = 2; i <= n/2; i++)
  17. {
  18. if (n % i == 0)
  19. {
  20. prime = false;
  21. }
  22. else
  23. {
  24. prime = true;
  25. }
  26. }
  27. //if (prime) Console.WriteLine("True");
  28. //else Console.WriteLine("False");
  29. return prime;
  30. }
  31. private static List<int> FindPrimesInRange(int startNum, int endNum)
  32. {
  33. var primes = new List<int>();
  34. for (int currentNum=startNum; currentNum<=endNum; currentNum++)
  35. {
  36. if (isPrime(currentNum))
  37. {
  38. primes.Add(currentNum);
  39. }
  40. }
  41. return primes;
  42. }
  43. static void Main(string[] args)
  44. {
  45. var startNum = int.Parse(Console.ReadLine());
  46. var endNum = int.Parse(Console.ReadLine());
  47. var primes = FindPrimesInRange(startNum, endNum);
  48. Console.WriteLine(string.Join(", ",primes));
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement