From 2910334673cb83ba064f4f5473e316738ba24734 Mon Sep 17 00:00:00 2001 From: Ujjwal <75884061+UG-SEP@users.noreply.github.com> Date: Wed, 16 Jun 2021 23:35:26 +0530 Subject: [PATCH] chore(csharp): Abundant Number (#357) --- algorithms/CSharp/README.md | 3 + .../CSharp/src/Maths/abundant-number.cs | 55 +++++++++++++++++++ algorithms/CSharp/test/Maths/AbundantTestUnit | 15 +++++ 3 files changed, 73 insertions(+) create mode 100644 algorithms/CSharp/src/Maths/abundant-number.cs create mode 100644 algorithms/CSharp/test/Maths/AbundantTestUnit diff --git a/algorithms/CSharp/README.md b/algorithms/CSharp/README.md index 21e1a469..b58f6b80 100644 --- a/algorithms/CSharp/README.md +++ b/algorithms/CSharp/README.md @@ -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) diff --git a/algorithms/CSharp/src/Maths/abundant-number.cs b/algorithms/CSharp/src/Maths/abundant-number.cs new file mode 100644 index 00000000..c7bf1f99 --- /dev/null +++ b/algorithms/CSharp/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) +*/ diff --git a/algorithms/CSharp/test/Maths/AbundantTestUnit b/algorithms/CSharp/test/Maths/AbundantTestUnit new file mode 100644 index 00000000..9bc2a48b --- /dev/null +++ b/algorithms/CSharp/test/Maths/AbundantTestUnit @@ -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); + } + } +}