Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Program
  6. {
  7. public bool IsPrimeNumber(int n)
  8. {
  9. for (int i = 2; i <= Math.Sqrt(n); i++)
  10. {
  11. if (n % i == 0)
  12. {
  13. return false;
  14. }
  15. }
  16.  
  17. return n > 1;
  18. }
  19.  
  20. public List<int> GetPrimeNumberLessThan(int n)
  21. {
  22. List<int> primeList = new List<int>();
  23. if (n < 2)
  24. {
  25. return primeList;
  26. }
  27.  
  28. for (int i = 2; i <= n; i++)
  29. {
  30. if (IsPrimeNumber(i))
  31. {
  32. primeList.Add(i);
  33. }
  34. }
  35.  
  36. return primeList;
  37. }
  38.  
  39. public int SumList(List<int> ln)
  40. {
  41. return ln.Sum();
  42. }
  43.  
  44. public void Main()
  45. {
  46. int n;
  47. List<int> primeList;
  48. Console.WriteLine("Enter your number: ");
  49. if (!Int32.TryParse(Console.ReadLine(), out n))
  50. {
  51. Console.WriteLine("It not a number");
  52. System.Environment.Exit(1);
  53. }
  54.  
  55. if (n < 1)
  56. {
  57. Console.WriteLine("{0} is not prime!", n.ToString());
  58. System.Environment.Exit(1);
  59. }
  60.  
  61. if (IsPrimeNumber(n))
  62. {
  63. Console.WriteLine("{0} is prime!", n.ToString());
  64. }
  65. else
  66. {
  67. Console.WriteLine("{0} is not prime!", n.ToString());
  68. }
  69.  
  70. primeList = GetPrimeNumberLessThan(n);
  71. if (primeList.Count > 0)
  72. {
  73. Console.Write("Prime number from 0 to {0} is: ", n);
  74. primeList.ForEach((number) => Console.Write("{0}, ", number));
  75. Console.WriteLine("");
  76. Console.WriteLine("Sum of prime number from 0 to {0} is: {1}", n, SumList(primeList));
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement