Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/com/thealgorithms/maths/SumOfOddNumbers.java
Original file line number Diff line number Diff line change
@@ -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;
}
}