PL/SQL Function to Check Number is Prime or Not

PL/SQL Function to Check Number is Prime or Not:

create or replace function check_prime(no in number) return varchar
is ans varchar(15);
n number;
begin
if(no = 2)then
 ans:='Prime';
else
  n:=no/2;
  for i in 2..n
  loop
 if(mod(no,i)=0) then
  ans:='Not prime';
  exit;
 else
  ans:='Prime';
 end if;
  end loop;
end if;
return(ans);
end;

Calling Function:

begin
dbms_output.put_line(check_prime(11));
end;

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.