Add SumOfOddNumbers (#5730)

This commit is contained in:
Byte Bender
2024-10-15 01:32:40 +05:30
committed by GitHub
parent 6ef1f7ca01
commit 3af4cfd7c8
2 changed files with 53 additions and 0 deletions

View File

@ -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;
}
}