-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnswer.cs
More file actions
83 lines (80 loc) · 2.12 KB
/
Answer.cs
File metadata and controls
83 lines (80 loc) · 2.12 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
class Program
{
public static void Main(string[] args)
{
int[] num = new int[5];
for (int i = 0; i < 5; i++)
{
try
{
Console.Write("Enter num: ");
num[i] = Int32.Parse(Console.ReadLine());
if (!(num[i] > 1 && num[i] < 600))
{
Console.WriteLine(num[i] + " is not in range");
i--;
}
}
catch (Exception)
{
Console.WriteLine("INVALID");
i--;
}
}
Console.WriteLine();
for (int i = 0; i < 5; i++)
{
if (num[i] % 2 == 0)
{
Console.WriteLine(num[i] + " is even, it's nearest Prime number is " + nearestPrime(num[i]));
}
else
{
Console.WriteLine(num[i] + " is odd, it's nearest Prime number is " + nearestPrime(num[i]));
}
}
}
public static Boolean nPrime(int s)
{
int k = 0;
for (int j = 1; j <= s; j++)
{
if (s % j == 0)
{
k++;
}
}
if (k == 2)
return true;
else
return false;
}
public static int nearestPrime(int n)
{
int n1 = 0;
for (int i = n; ; i++) // nearest prime num to the left
{
if (nPrime(i))
{
n1 = i;
if (n == n1)
{
i++;
}
else {
break;
}
}
}
return n1;
}
}
}