I am trying to check whether an entered number is odd or even I am getting an error
declare num:=:num if(num mod 2=0) then dbms_output.put_line(num|| 'is even'); else dbms_output.put_line(num||' is odd'); end if; end;
Advertisement
Answer
The documentation for the modulo function is here which tells you the syntax is MOD(n1, n2) and not n1 MOD n2 and you are missing the BEGIN statement:
You want:
declare
num:=:num
begin
if mod(num, 2)=0 then
dbms_output.put_line(num|| 'is even');
else
dbms_output.put_line(num||' is odd');
end if;
end;
You can simplify it by removing the intermediate variable and just using the bind variable throughout and can remove the IF statement and multiple output statements and use a single CASE (and handle NULL values):
BEGIN
DBMS_OUTPUT.PUT_LINE(
:num || CASE MOD(:num, 2)
WHEN 0 THEN ' is even'
WHEN 1 THEN ' is odd'
ELSE ' is neither odd nor even.'
END
);
END;
/
