Tags: accepts, block, create, database, determines, function, integer, mod, mysql, number, numbers, odd, oracle, sql, sqlplus, substituion, variable
Even/Odd MOD Function
Create 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: '
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);
evenoddnum := mynum2/2 (divides the number entered by the user by 2);
Return evenoddnum (the remainder from the division operation);
if (mynum) is NULL then
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)
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.
Leave a comment...
- 3 Comments
- The MOD(m,n) function takes two parameters:
Returns remainder of M divided by N. Returns M if N is 0.
Gio#1; Fri, 22 Feb 2008 16:53:00 GMT
- First off, creating a local MOD function is a bad idea when there is already a system-level MOD function. At best, you'll totally confuse people.
ACCEPT num1 NUMBER FORMAT 99 DEFAULT 0 PROMPT 'Enter a number (1-10): 'DECLARE num NUMBER := &num1;
BEGIN IF( &
num1 IS NULL ) THEN dbms_output.put_line( 'Null' );
num1, 2) = 0 ) THEN dbms_output.put_line( 'Even' );
ELSE dbms_output.put_line( 'Odd' );
Note that I don't check for values > 10 or <
1, you'll have to add that.
Distributed Database Consulting, Inc.#2; Fri, 22 Feb 2008 16:54:00 GMT
- You declared your own function, MOD, that is the same as Oracle built-in function, bad move. I also don't see where you check if the input number is between 1 and 10.#3; Fri, 22 Feb 2008 16:55:00 GMT