forked from MakeContributions/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabundant-number.cs
55 lines (53 loc) · 1.55 KB
/
abundant-number.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
/* Program to check whether a number is abundant or not */
using System;
namespace Abundant.Number
{
class Abundant
{
// function to check whether number is abundant or not
public static bool IsAbudant(int num)
{
int i, sum = 0;
// loop run until it is less than or equal to half of the number
for (i = 1; i <= num / 2; i++)
{
// if the remainder becomes 0 add that number to sum
if (num % i == 0)
sum += i;
}
// if the sum is greater than num that means it is an abundant number
if (sum > num)
{
return true;
}
else // it is not an abundant number
{
return false;
}
}
// driver code
static void Main(string[] args)
{
int num;
bool res;
Console.WriteLine("Enter a number: ");
// reading data and converting into into int to store that in num variable
num = Convert.ToInt32(Console.ReadLine());
res = IsAbudant(num);
if (res)
{
Console.WriteLine($"{num} is an abundant number");
}
else
{
Console.WriteLine($"{num} is not an abundant number");
}
}
}
}
/*
Input: Enter a number: 54
Output: 54 is an abundant number
Time complexity: O(N/2) where is the num given by user
Space complexity: O(1)
*/