Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class Program
- {
- public bool IsPrimeNumber(int n)
- {
- for (int i = 2; i <= Math.Sqrt(n); i++)
- {
- if (n % i == 0)
- {
- return false;
- }
- }
- return n > 1;
- }
- public List<int> GetPrimeNumberLessThan(int n)
- {
- List<int> primeList = new List<int>();
- if (n < 2)
- {
- return primeList;
- }
- for (int i = 2; i <= n; i++)
- {
- if (IsPrimeNumber(i))
- {
- primeList.Add(i);
- }
- }
- return primeList;
- }
- public int SumList(List<int> ln)
- {
- return ln.Sum();
- }
- public void Main()
- {
- int n;
- List<int> primeList;
- Console.WriteLine("Enter your number: ");
- if (!Int32.TryParse(Console.ReadLine(), out n))
- {
- Console.WriteLine("It not a number");
- System.Environment.Exit(1);
- }
- if (n < 1)
- {
- Console.WriteLine("{0} is not prime!", n.ToString());
- System.Environment.Exit(1);
- }
- if (IsPrimeNumber(n))
- {
- Console.WriteLine("{0} is prime!", n.ToString());
- }
- else
- {
- Console.WriteLine("{0} is not prime!", n.ToString());
- }
- primeList = GetPrimeNumberLessThan(n);
- if (primeList.Count > 0)
- {
- Console.Write("Prime number from 0 to {0} is: ", n);
- primeList.ForEach((number) => Console.Write("{0}, ", number));
- Console.WriteLine("");
- Console.WriteLine("Sum of prime number from 0 to {0} is: {1}", n, SumList(primeList));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement