Custom Search

Wednesday, 30 November 2011

Write a function in PL/SQL to check whether the given number is prime or not.


Write a function in PL/SQL to check whether the given number is prime or not.

set serveroutput on

create or replace function prime(n in number) return varchar2 is

i number(10);

pr number(10);

res varchar2(10);

begin

pr:=1;

for i in 2..n/2 loop

if mod(n,i)=0 then

pr:=0;

end if;

end loop;


if pr=1 then

res:='prime';

else

res:='Not Prime';

end if;

return res;

end;

To run:= select prime(5) from dual;

No comments:

Post a Comment

Laptops