chore(csharp): Abundant Number (#357)
parent
595992b77a
commit
2910334673
|
@ -12,3 +12,6 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/
|
|||
|
||||
## Search
|
||||
- [Binary Search](src/Search/binary-search.cs)
|
||||
|
||||
## Maths
|
||||
- [Abundant Number](src/Maths/abundant-number.cs)
|
||||
|
|
|
@ -0,0 +1,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)
|
||||
*/
|
|
@ -0,0 +1,15 @@
|
|||
using NUnit.Framework;
|
||||
namespace Abundant.Test
|
||||
{
|
||||
[TestFixture]
|
||||
public class Testcase
|
||||
{
|
||||
[TestCase(54, true)]
|
||||
[TestCase(5, false)]
|
||||
public void PassNumber_ShouldGetExpectedResult(int num,bool expected)
|
||||
{
|
||||
var result = Abundant.Number.Abundant.IsAbudant(num);
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue