Skip to content

Commit 7fd4681

Browse files
authored
Create SumOfOddNumbers.java
1 parent f8397bf commit 7fd4681

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)