Skip to content

Commit 6c3203b

Browse files
author
Lê Nam Khánh
authored
Add algorithm SumOfDigits (#571)
1 parent 8f05b22 commit 6c3203b

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Algorithms.Numeric;
2+
using NUnit.Framework;
3+
using System;
4+
5+
namespace Algorithms.Tests.Numeric;
6+
7+
/// <summary>
8+
/// Tests for the SumOfDigits class.
9+
/// </summary>
10+
public static class SumOfDigitsTests
11+
{
12+
/// <summary>
13+
/// Tests the calculation of the sum of digits for various non-negative integers.
14+
/// </summary>
15+
/// <param name="input">The input number.</param>
16+
/// <param name="expectedSum">The expected sum of its digits.</param>
17+
[TestCase(0, 0)]
18+
[TestCase(7, 7)]
19+
[TestCase(10, 1)]
20+
[TestCase(42, 6)]
21+
[TestCase(12345, 15)]
22+
[TestCase(9999, 36)]
23+
[TestCase(8675309, 38)]
24+
[TestCase(2147483647, 46)] // Max value for int
25+
public static void GetsCorrectSumOfDigits(int input, int expectedSum)
26+
{
27+
// Act
28+
var result = SumOfDigits.Calculate(input);
29+
30+
// Assert
31+
Assert.That(result, Is.EqualTo(expectedSum));
32+
}
33+
34+
/// <summary>
35+
/// Tests that the method throws an ArgumentException when a negative number is provided.
36+
/// </summary>
37+
/// <param name="num">The negative input number.</param>
38+
[TestCase(-1)]
39+
[TestCase(-100)]
40+
[TestCase(-54321)]
41+
public static void ThrowsExceptionForNegativeNumbers(int num)
42+
{
43+
// Act
44+
void Act() => SumOfDigits.Calculate(num);
45+
46+
// Assert
47+
_ = Assert.Throws<ArgumentException>(Act);
48+
}
49+
}

Algorithms/Numeric/SumOfDigits.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
3+
namespace Algorithms.Numeric;
4+
5+
/// <summary>
6+
/// Provides functionality to calculate the sum of the digits of an integer.
7+
/// </summary>
8+
public static class SumOfDigits
9+
{
10+
/// <summary>
11+
/// Calculates the sum of the digits of a non-negative integer.
12+
/// The method iteratively uses the modulus operator (%) to get the last digit
13+
/// and the division operator (/) to drop the last digit until the number is 0.
14+
/// </summary>
15+
/// <param name="number">The non-negative integer whose digits are to be summed.</param>
16+
/// <returns>The sum of the digits of the input number.</returns>
17+
/// <exception cref="ArgumentException">Thrown if the input number is negative.</exception>
18+
public static int Calculate(int number)
19+
{
20+
if (number < 0)
21+
{
22+
throw new ArgumentException("Input must be a non-negative integer.", nameof(number));
23+
}
24+
25+
if (number == 0)
26+
{
27+
return 0;
28+
}
29+
30+
int sum = 0;
31+
int currentNumber = number;
32+
33+
// Loop until the number becomes 0
34+
while (currentNumber > 0)
35+
{
36+
// Get the last digit (e.g., 123 % 10 = 3)
37+
int digit = currentNumber % 10;
38+
39+
// Add the digit to the sum
40+
sum += digit;
41+
42+
// Remove the last digit (e.g., 123 / 10 = 12)
43+
currentNumber /= 10;
44+
}
45+
46+
return sum;
47+
}
48+
}

0 commit comments

Comments
 (0)