We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f8397bf commit 7fd4681Copy full SHA for 7fd4681
src/main/java/com/thealgorithms/maths/SumOfOddNumbers.java
@@ -0,0 +1,26 @@
1
+package com.thealgorithms.maths;
2
+
3
+/**
4
+ * This program calculates the sum of the first n odd numbers.
5
+ *
6
+ * https://www.cuemath.com/algebra/sum-of-odd-numbers/
7
+ */
8
9
+public final class SumOfOddNumbers {
10
+ private SumOfOddNumbers() {
11
+ }
12
13
+ /**
14
+ * Calculate sum of the first n odd numbers
15
16
+ * @param n the number of odd numbers to sum
17
+ * @return sum of the first n odd numbers
18
19
20
+ public static int sumOfFirstNOddNumbers(final int n) {
21
+ if (n < 0) {
22
+ throw new IllegalArgumentException("n must be non-negative.");
23
24
+ return n * n;
25
26
+}
0 commit comments