-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathPerfectNumbers.cs
59 lines (57 loc) · 2 KB
/
PerfectNumbers.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IntegerSequencesOpenSourceContributing
{
class Program
{
static void Main(string[] args)
{
while (true) // repeating until user input is letter "q"
{
Console.WriteLine("Enter the value of n.\nTo quit type 'q'.");
string input = Console.ReadLine(); // Reading input value
if (input == "q") // if input is letter "q" canceling the program
break;
if (!Int32.TryParse(input, out int n)) // start new loop, if user input can't be parsed to int
{
Console.WriteLine("Invalid input: Please enter an integer input.");
continue;
}
bool res;
try
{
res = IsPerfectNumber(n); // compute result
Console.WriteLine("Result = " + res);
}
catch(ArgumentOutOfRangeException ex) //start new step of loop, if input value isn't a positive number
{
Console.WriteLine($"Invalid input, please enter a positive number. Your input is {ex.ActualValue}");
continue;
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
Console.WriteLine();
}
}
// perfect number computing function
static bool IsPerfectNumber(int n)
{
if (n <= 0)
{
throw new ArgumentOutOfRangeException("n", n, "");
}
int sum = 0;
for (int i = n - 1; i > 0; i--)
{
if (n % i == 0)
{
sum += i;
}
}
return sum == n;
}
}
}