Repository: TheAlgorithms/Java
Pull request #5730 (2024-10-12 07:41:56)

Create SumOfOddNumbers.java
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5730?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `0%` with `3 lines` in your changes missing coverage. Please review.
> Project coverage is 63.73%. Comparing base [(`f8397bf`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/f8397bf09b3c7554da52470d6db3bf5b7b8d1e34?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`7fd4681`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/7fd4681adfd52eeb42d0ff4e5dfba4679b4b6730?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 1 commits behind head on master.

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5730?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [.../java/com/thealgorithms/maths/SumOfOddNumbers.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5730?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fmaths%2FSumOfOddNumbers.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9tYXRocy9TdW1PZk9kZE51bWJlcnMuamF2YQ==) | 0.00% | [3 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5730?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5730      +/-   ##
============================================
+ Coverage     63.71%   63.73%   +0.01%     
- Complexity     4130     4135       +5     
============================================
  Files           578      580       +2     
  Lines         16197    16212      +15     
  Branches       3117     3120       +3     
============================================
+ Hits          10320    10332      +12     
- Misses         5453     5456       +3     
  Partials        424      424              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5730?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/maths/SumOfOddNumbers.java b/src/main/java/com/thealgorithms/maths/SumOfOddNumbers.java
new file mode 100644
index 00000000..c0a1e782
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/SumOfOddNumbers.java
@@ -0,0 +1,25 @@
+package com.thealgorithms.maths;
+
+/**
+ * This program calculates the sum of the first n odd numbers.
+ *
+ * https://www.cuemath.com/algebra/sum-of-odd-numbers/
+ */
+
+public final class SumOfOddNumbers {
+    private SumOfOddNumbers() {
+    }
+
+    /**
+     * Calculate sum of the first n odd numbers
+     *
+     * @param n the number of odd numbers to sum
+     * @return sum of the first n odd numbers
+     */
+    public static int sumOfFirstNOddNumbers(final int n) {
+        if (n < 0) {
+            throw new IllegalArgumentException("n must be non-negative.");
+        }
+        return n * n;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/SumOfOddNumbersTest.java b/src/test/java/com/thealgorithms/maths/SumOfOddNumbersTest.java
new file mode 100644
index 00000000..6470d798
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/SumOfOddNumbersTest.java
@@ -0,0 +1,28 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.stream.Stream;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+public class SumOfOddNumbersTest {
+
+    @ParameterizedTest
+    @MethodSource("inputStream")
+    void sumOfFirstNOddNumbersTests(int expected, int input) {
+        Assertions.assertEquals(expected, SumOfOddNumbers.sumOfFirstNOddNumbers(input));
+    }
+
+    private static Stream<Arguments> inputStream() {
+        return Stream.of(Arguments.of(1, 1), Arguments.of(4, 2), Arguments.of(9, 3), Arguments.of(16, 4), Arguments.of(25, 5), Arguments.of(100, 10));
+    }
+
+    @Test
+    public void testSumOfFirstNOddNumbersThrowsExceptionForNegativeInput() {
+        assertThrows(IllegalArgumentException.class, () -> SumOfOddNumbers.sumOfFirstNOddNumbers(-1));
+    }
+}