Even/Odd MOD Function
430055Sep 24 2004 — edited Sep 24 2004Create an anonymous PL/SQL block that accepts an integer number N through SQL*Plus substituion variable and then determines for each of the numbers in the range 1 through N inclusive whether it is odd or even. Use the MOD function to determine whether a number is odd or even. For example, MOD (10, 2) = 0 and MOD (11,2)=1. Print the results on the screen.
Your program should handle NULL values. N should be set to 0 if a NULL value is entered.
Here is the code I generated for this problem:
ACCEPT num1 PROMPT 'Please enter a number between 1 and 10: '
DECLARE
mynum NUMBER := &num1 (stores the user input into a PL/SQL variable);
mynum1 NUMBER (stores the value returned from the MOD function);
function MOD(mynum2 NUMBER)
Return NUMBER (remainder datatype) is
evenoddnum NUMBER (returns a remainder number from the calculation);
BEGIN
evenoddnum := mynum2/2 (divides the number entered by the user by 2);
Return evenoddnum (the remainder from the division operation);
END;
BEGIN
if (mynum) is NULL then
mynum :=0;
end if ; (sets all NULL values entered by the user to 0)
mynum1 := MOD(mynum) (stores the remainder from the MOD calucation after the function is call. The user's input is passed as a parameter to the function) ;
if (mynum1 = 0) then
dbms_output.put_line ('You entered an even number');
elsif (mynum1 = 1) then
dbms_output.put_line ('You entered an odd number');
end if; (test whether the remainder returned in mynum1 is 0 or 1)
END;
/
Error message received:
SQL> start prog2c.sql
Please enter a number between 1 and 10: 5
mynum1 := MOD(mynum);
*
ERROR at line 20:
ORA-06550: line 20, column 18:
PLS-00306: wrong number or types of arguments in call to 'MOD'
ORA-06550: line 20, column 8:
PL/SQL: Statement ignored
Does anyone have any idea what this error message means???? I am totally confused.
Thanks!