Home » SQL & PL/SQL » SQL & PL/SQL » prime no through pl/sql
prime no through pl/sql [message #212839] Mon, 08 January 2007 09:00 Go to next message
ashishkumarmahanta80
Messages: 231
Registered: October 2006
Location: kolkatta
Senior Member
hello team

i m using pl/sql to get prime no while accepting any number.

i m getting error while running the block.

my block is like this-

declare
n number;
x number:=1;
a number;
begin
n:=#
for i in 1..n/2
loop
x:=x+1;
a:=mod(n,i);
if a=0 then
dbms_output.put_line(n||' is not prime no');
else
dbms_output.put_line(n||' is prime no');
end if;
end loop;
end;
/


Enter value for num:=9

i m getting error like this-

9 is prime no
9 is not prime no
9 is prime no
9 is prime no
9 is prime no
9 is prime no
9 is prime no

please tell me what i had done wrong?

if is there any other process to get prime no. then please
provide me .

regards
Re: prime no through pl/sql [message #212840 is a reply to message #212839] Mon, 08 January 2007 09:03 Go to previous messageGo to next message
MarcL
Messages: 455
Registered: November 2006
Location: Connecticut, USA
Senior Member
Your output statements should be after the end loop command for one thing.

[Updated on: Mon, 08 January 2007 09:06]

Report message to a moderator

Re: prime no through pl/sql [message #212867 is a reply to message #212840] Mon, 08 January 2007 11:44 Go to previous messageGo to next message
anilsinare
Messages: 22
Registered: December 2005
Location: ipswich, uk
Junior Member
Just add exit after printing the result.

declare
n number;
x number:=1;
a number;
begin
n:=#
for i in 1..n/2
loop
x:=x+1;
a:=mod(n,i);
if a=0 then
dbms_output.put_line(n||' is not prime no');
exit;
else
dbms_output.put_line(n||' is prime no');
exit;
end if;
end loop;
end;
/
Re: prime no through pl/sql [message #212869 is a reply to message #212867] Mon, 08 January 2007 12:10 Go to previous messageGo to next message
Frank
Messages: 7880
Registered: March 2000
Senior Member
Did you actually try this code?
Re: prime no through pl/sql [message #212872 is a reply to message #212839] Mon, 08 January 2007 12:27 Go to previous messageGo to next message
Bill B
Messages: 1482
Registered: December 2004
Senior Member
declare
  n number;
  found varchar2(1);
begin
  n:=#
  found := 'Y';
  for i in 1..n/2 loop
    if mod(n,i) = 0 then
      dbms_output.put_line(to_char(n)||' is not prime no');
      found := 'N';
      exit;
    end if;
  end loop;
  if found = 'Y' then
    dbms_output.put_line(to_char(n)||' is prime no');
  end if;
end;
/

[Updated on: Mon, 08 January 2007 12:28]

Report message to a moderator

Re: prime no through pl/sql [message #212897 is a reply to message #212872] Mon, 08 January 2007 15:10 Go to previous messageGo to next message
Littlefoot
Messages: 20891
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
You might also be interested in viewing exactly the same problem posted only a few days ago.
Re: prime no through pl/sql [message #212929 is a reply to message #212897] Mon, 08 January 2007 19:38 Go to previous messageGo to next message
rleishman
Messages: 3724
Registered: October 2005
Location: Melbourne, Australia
Senior Member
And here's an old solution using SQL.

Ross Leishman
Re: prime no through pl/sql [message #212935 is a reply to message #212872] Mon, 08 January 2007 20:05 Go to previous messageGo to next message
ebrian
Messages: 2794
Registered: April 2006
Senior Member
Bill B wrote on Mon, 08 January 2007 13:27
declare
  n number;
  found varchar2(1);
begin
  n:=#
  found := 'Y';
  for i in 1..n/2 loop
    if mod(n,i) = 0 then
      dbms_output.put_line(to_char(n)||' is not prime no');
      found := 'N';
      exit;
    end if;
  end loop;
  if found = 'Y' then
    dbms_output.put_line(to_char(n)||' is prime no');
  end if;
end;
/


This will not provide the correct answer. You should refer to Ross's solution above.
Re: prime no through pl/sql [message #212983 is a reply to message #212872] Tue, 09 January 2007 01:00 Go to previous message
hari.choprala
Messages: 65
Registered: November 2006
Location: Mumbai
Member

Hi Bill,

You have to change the loop

for i in 2..n/2 loop

then it will work


rgds
Hari Krishna
Previous Topic: Date query
Next Topic: Nested Table And UTL_FILE ??
Goto Forum:
  


Current Time: Sun Dec 04 06:47:47 CST 2016

Total time taken to generate the page: 0.12796 seconds