declare n number; i number; flag number; begin i:=2; flag:=1; n:=7; for i in 2..n/2 loop if mod(n,i)=0 then flag:=0; exit; end if; end loop; if flag=1 then dbms_output.put_line('prime'); else dbms_output.put_line('not prime'); end if; end; / Output is attached below:
declare
n number;
i number;
flag number;
begin
i:=2;
flag:=1;
n:=7;
for i in 2..n/2
loop
if mod(n,i)=0
then
flag:=0;
exit;
end if;
end loop;
if flag=1
then
dbms_output.put_line(‘prime’);
else
dbms_output.put_line(‘not prime’);
end if;
end;
/
DECLAREi number(3);j number(3);BEGINi := 2;LOOPj:= 2;LOOPexit WHEN ((mod(i, j) = 0) or (j = i));j := j +1;END LOOP;IF (j = i ) THENdbms_output.put_line(i || ' is prime ');END IF;i := i + 1;exit WHEN i = 40;END LOOP;END;/
DECLARE i number(3); j number(3); BEGIN i := 2; LOOP j:= 2; LOOP exit WHEN ((mod(i, j) = 0) or (j = i)); j := j +1; END LOOP; IF (j = i ) THEN dbms_output.put_line(i || ' is prime '); END IF; i := i + 1; exit WHEN i = 40; END LOOP; END; /
Write a program in PL/SQL to check whether a number is prime or not using goto statement with for loop.
declare n number; i number; flag number; begin i:=2; flag:=1; n:=7; for i in 2..n/2 loop if mod(n,i)=0 then flag:=0; exit; end if; end loop; if flag=1 then dbms_output.put_line('prime'); else dbms_output.put_line('not prime'); end if; end; / Output is attached below:
declare
n number;
i number;
flag number;
begin
i:=2;
flag:=1;
n:=7;
for i in 2..n/2
loop
if mod(n,i)=0
then
flag:=0;
exit;
end if;
end loop;
if flag=1
then
dbms_output.put_line(‘prime’);
else
dbms_output.put_line(‘not prime’);
end if;
end;
/
Output is attached below:
See lessWrite a program in PL/SQL to show the uses of nested loop.
DECLAREi number(3);j number(3);BEGINi := 2;LOOPj:= 2;LOOPexit WHEN ((mod(i, j) = 0) or (j = i));j := j +1;END LOOP;IF (j = i ) THENdbms_output.put_line(i || ' is prime ');END IF;i := i + 1;exit WHEN i = 40;END LOOP;END;/