Hello, i made a function on T-SQL that checks if the number is a kaprekar one CREATE FUNCTION [dbo].[kaprekar](@n int)
RETURNS bit
WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE @result bit;
DECLARE @sq_n INT;
DECLARE @count_digits INT;
DECLARE @r_digits INT;
DECLARE @eq_parts INT;
DECLARE @sum INT;
`
SET @sq_n = @n * @n;`
SET @count_digits=0;
WHILE @sq_n <> 0
BEGIN
SET @count_digits= @count_digits + @count_digits;
SET @sq_n=@sq_n/10;
END;
SET @sq_n = @n*@n;
WHILE @r_digits<@count_digits
BEGIN
SET @eq_parts=POWER(10,@r_digits);
SET @sum = @sq_n/@eq_parts + @sq_n% @eq_parts;
END;
`
IF @n = 1
BEGIN
SET @result=1;
END;
ELSE IF @eq_parts = @n
BEGIN
SET @result=0;
`
END;`ELSE IF @sum=@n
BEGIN
SET @result=1;
`
END;`ELSE
BEGIN
SET @result=0;
END;
`
RETURN @result;`
END;
``
But it don't work. when i insert any number except 1, it prints 0, which is not correct. i tried to analyze my code, and made a conclusion that the problem is with in the main code block, not with the massive IF ELSE block. can someone help?