SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE FUNCTION SumofOddNums (@input INT) RETURNS INT AS BEGIN -- Edge case for non-positive numbers IF @input <= 0 RETURN 0; -- Declare n to represent the count of odd numbers DECLARE @n INT; -- Adjust n based on whether the input is odd or even -- If input is even, use the largest odd number smaller than input IF (@input % 2 = 0) SET @n = @input / 2; ELSE -- If input is odd, include the input number itself SET @n = (@input + 1) / 2; -- The sum of the first n odd numbers is n^2 (sum of squares formula) RETURN @n * @n; END;

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear