-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsPrimeNum.cs
61 lines (54 loc) · 1.26 KB
/
IsPrimeNum.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
58
59
60
61
/*Write a Boolean method IsPrime(n) that check whether a given integer number n is prime.
Examples:
n IsPrime(n)
0 false
1 false
2 true
3 true
4 false
5 true
323 false
337 true
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrimeNumbers
{
class IsPrimeNum
{
static void Main(string[] args)
{
Console.WriteLine("Enter number for n ");
Console.Write("n = ");
int n = int.Parse(Console.ReadLine());
bool isPrime = true;
Console.WriteLine("The number {0} is {1}", n, IsPrimeNumber(n, isPrime));
}
static bool IsPrimeNumber(int n, bool isPrime)
{
if (n == 0 || n == 1 || 0 > n)
{
isPrime = false;
return isPrime;
}
int counter = 0;
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
{
counter++;
}
}
if (counter > 2)
{
isPrime = false;
return isPrime;
}
isPrime = true;
return isPrime;
}
}
}