-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimeNumbers.cs
57 lines (45 loc) · 1.36 KB
/
PrimeNumbers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
*In this code, the function takes an integer as input and returns a boolean value
*indicating whether the input number is prime or composite. We start by testing if
*the input number is less than 2 (in which case it is neither prime nor composite).
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FiveDaysTrainings
{
internal class PrimeNumbers
{
public void prime_numbers()
{
Console.Write("Prime Numbers");
Console.Write("\nEnter a number: ");
int n = int.Parse(Console.ReadLine());
if (IsPrime(n))
{
Console.WriteLine(n + " is a prime number.");
}
else
{
Console.WriteLine(n + " is a composite number.");
}
Console.WriteLine("--------------------------------");
}
private static bool IsPrime(int n)
{
if (n < 2) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i <= Math.Sqrt(n); i += 6)
{
if (n % i == 0 || n % (i + 2) == 0)
{
return false;
}
}
return true;
}
}
}