This seems ok ,please correct me - oracle

n number := &n;
c number;
i number;
function isprime(x in number)
RETURN number
IS
begin
count number:=0;
for i in 2..x/2 loop
if mod(x,i)=0 then
count := count+1;
end if;
end loop;
return count;
end;
begin
c:=isprime(n);
if c=0 then
dbms_output.put_line(n||'is a prime number');
else
dbms_output.put_line(n||'is not prime');
end if;
end;
/
ORA-06550: line 11, column 7:
PLS-00103: Encountered the symbol "NUMBER" when expecting one of the following:
:= . ( # % ;
The symbol "." was substituted for "NUMBER" to continue.

Don't use column names that match Oracle's built-in functions (count is one of them). Declare variables in declaration section, not just anywhere.
SQL> DECLARE
2 n NUMBER := &par_n;
3 c NUMBER;
4 i NUMBER;
5
6 FUNCTION isprime (x IN NUMBER)
7 RETURN NUMBER
8 IS
9 l_count NUMBER := 0;
10 BEGIN
11 FOR i IN 2 .. x / 2
12 LOOP
13 IF MOD (x, i) = 0
14 THEN
15 l_count := l_count + 1;
16 END IF;
17 END LOOP;
18
19 RETURN l_count;
20 END;
21 BEGIN
22 c := isprime (n);
23
24 IF c = 0
25 THEN
26 DBMS_OUTPUT.put_line (n || ' is a prime number');
27 ELSE
28 DBMS_OUTPUT.put_line (n || ' is not prime');
29 END IF;
30 END;
31 /
Enter value for par_n: 6
6 is not prime
PL/SQL procedure successfully completed.
SQL> /
Enter value for par_n: 7
7 is a prime number
PL/SQL procedure successfully completed.
SQL>

Related

PLS-00103: Encountered the symbol "+" when expecting one of the following: (

declare
i number;
sum number;
begin
i:=1;
sum:=0;
for i in 1..100 loop
if MOD(i,2) != 0 then
sum:= sum + i;
dbms_output.put_line(i);
end if;
end loop;
dbms_output.Put_line(sum);
end;
sum is a reserved word, reserved for built-in function. Rename variable to v_sum (for example).
SQL> set serveroutput on
SQL>
SQL> DECLARE
2 v_sum NUMBER;
3 BEGIN
4 v_sum := 0;
5 FOR i IN 1 .. 100 LOOP
6 IF MOD (i, 2) != 0 THEN
7 v_sum := v_sum + i;
8 -- DBMS_OUTPUT.put_line (i);
9 END IF;
10 END LOOP;
11 DBMS_OUTPUT.Put_line (v_sum);
12 END;
13 /
2500
PL/SQL procedure successfully completed.
SQL>

what is wrong with the way I write procedures and functions in plsql?

1 declare
2 a number;
3 b number;
4 c number;
5 d number;
6 PROCEDURE findMin(x IN number, y IN number, z IN number , L out number) IS
7 BEGIN
8 IF x > y&& x>z then
9 L:= x;
10 ELSE if y>z&&y>x then
11 L:= y;
12 else
13 L:=z
14 END IF;
15 End if;
16 END;
17 BEGIN
18 a:= 23;
19 b:= 45;
20 c:=36;
21 findMin(a, b, c,d);
22 dbms_output.put_line(' Minimum of (23, 45,36) : ' || d);
END;
this is the first one I couldnt unerstand what is wrong with this code it is showing
*
ERROR at line 1:
ORA-06540: PL/SQL: compilation error
ORA-06553: PLS-906: Compilation is not possible
the second one is
2. DECLARE
3. num number;
4. c number;
5. PROCEDURE fact(x IN number, f out number) IS
6. BEGIN
7. IF x = 0 THEN
8. f:= x;
9. ELSE
10. f:= x*fact(x-1);
11. END IF;
12. END;
13. BEGIN
14. c:=f;
15. num:=6
16. fact(num,c);
17. dbms_output.put_line(' Factorial: ' ||'is'||c);
18. END;
19. /
i am getting output as
z:= x*fact(x-1) ;
*
ERROR at line 9:
ORA-06550: line 9, column 14:
PLS-00306: wrong number or types of arguments in call to 'FACT'
ORA-06550: line 9, column 8:
PL/SQL: Statement ignored
this is the second procedure i wrote but i couldnt get the problem in it
1 create or replace function tables(n in number) return number is s number;
2 begin
3 i number;
4 for i in 1...10 loop
5 s:=n*i;
6 end loop;
7 return s ;
8* end;
this is the multiplication table function what is wrong with my codes they are showing output as
Warning: Function created with compilation errors.
As of your 1st code: you should use AND, not && and terminate statements with a colon. When fixed, it runs (and produces wrong result, though, but I'll leave it to you):
SQL> DECLARE
2 a NUMBER;
3 b NUMBER;
4 c NUMBER;
5 d NUMBER;
6
7 PROCEDURE findMin (x IN NUMBER,
8 y IN NUMBER,
9 z IN NUMBER,
10 L OUT NUMBER)
11 IS
12 BEGIN
13 IF x > y
14 AND x > z
15 THEN
16 L := x;
17 ELSE
18 IF y > z
19 AND y > x
20 THEN
21 L := y;
22 ELSE
23 L := z;
24 END IF;
25 END IF;
26 END;
27 BEGIN
28 a := 23;
29 b := 45;
30 c := 36;
31 findMin (a,
32 b,
33 c,
34 d);
35 DBMS_OUTPUT.put_line (' Minimum of (23, 45,36) : ' || d);
36 END;
37 /
Minimum of (23, 45,36) : 45
PL/SQL procedure successfully completed.
SQL>
All that (37 lines of code) could be shortened to only one (which actually works):
SQL> select least(23, 45, 36) minimum from dual;
MINIMUM
----------
23
SQL>
As of your 2nd code: procedure is wrong as fact can't be used that way and lacks in 2nd parameter. One option to fix it is
SQL> DECLARE
2 num NUMBER;
3 c NUMBER;
4
5 PROCEDURE fact (x IN NUMBER, f OUT NUMBER)
6 IS
7 l_var NUMBER := 1;
8 BEGIN
9 FOR i IN 1 .. x
10 LOOP
11 l_var := l_var * i;
12 END LOOP;
13
14 f := l_var;
15 END;
16 BEGIN
17 num := 6;
18 fact (num, c);
19 DBMS_OUTPUT.put_line (' Factorial of ' || num || ' is ' || c);
20 END;
21 /
Factorial of 6 is 720
PL/SQL procedure successfully completed.
SQL>
Finally, the 3rd code: I have no idea what you meant to say with it, it's full of errors. I tried to salvage it, can't tell whether I succeeded.
SQL> CREATE OR REPLACE FUNCTION tables (n IN NUMBER)
2 RETURN NUMBER
3 IS
4 s NUMBER := 0;
5 BEGIN
6 FOR i IN 1 .. 10
7 LOOP
8 s := s + n * i;
9 END LOOP;
10
11 RETURN s;
12 END;
13 /
Function created.
SQL> SELECT tables (3) FROM DUAL;
TABLES(3)
----------
165
SQL>

Write PL/SQL code to generate Armstrong number from 1 to 500

I am getting output as a = 1. I have taken a for loop from 1 to 500 and while loops inside the outer for loop.
declare
n number;
s number:=0;
r number;
len number;
m number;
begin
for a in 1..500 loop
m:=a;
n:=a;
len:=length(to_char(n));
while(n>0) loop
r:=mod(n,10);
s:=s+power(r,len);
n:=trunc(n/10);
end loop;
if m=s then
dbms_output.put_line('a='||to_char(a)');
end if;
end loop;
end;
How about this?
substr splits i to 3 separate digits
nvl is here to avoid adding null value if those digits don't exist (yet)
power function calculates i's cube
display i if it is equal to r (as "result")
SQL> declare
2 r number;
3 begin
4 for i in 1 .. 500 loop
5 r := power(to_number(substr(to_char(i), 1, 1)), 3) +
6 nvl(power(to_number(substr(to_char(i), 2, 1)), 3), 0) +
7 nvl(power(to_number(substr(to_char(i), 3, 1)), 3), 0);
8
9 if r = i then
10 dbms_output.put_line(i);
11 end if;
12 end loop;
13 end;
14 /
1
153
370
371
407
PL/SQL procedure successfully completed.
SQL>
You never reset s and you do not need the final IF statement (unless you are only interested in the Armstrong numbers which equal the original number):
declare
n number;
s number:=0;
r number;
len number;
m number;
begin
for a in 1..500 loop
m:=a;
n:=a;
s:=0; -- Reset s for each loop
len:=length(to_char(n));
while(n>0) loop
r:=mod(n,10);
s:=s+power(r,len);
n:=trunc(n/10);
end loop;
dbms_output.put_line(a || '=' || s); -- Output values for every loop.
end loop;
end;
/
db<>fiddle here

Print the sum of even digits of a number in Pl/sql

I cant seem to resolve this issue
As of the error itself, it is because you're trying to put the whole row (temp, which is declared as a cursor variable - c_nm%rowtype) into an integer variable (i). It won't work, although temp actually contains only one column - num.
So, line #15 on your screenshot:
No : i := temp;
Yes: i := temp.num;
Once you fix it, your code works but - unfortunately, produces wrong result. In input number 121974553, even digits are 2, 9, 4 and 5 whose sum equals 20, not 6 (as your result suggests):
SQL> set serveroutput on;
SQL> declare
2 ad int := 0;
3 i int;
4 b int;
5 cursor c_nm is select * From numb;
6 temp c_nm%rowtype;
7 begin
8 open c_nm;
9 loop
10 fetch c_nm into temp;
11 exit when c_nm%notfound;
12 i := temp.num; --> this
13
14 while i > 0 loop
15 b := mod(i, 10);
16 if mod(b, 2) = 0 then
17 ad := b + ad;
18 end if;
19 i := trunc(i/10);
20 end loop;
21 end loop;
22 dbms_output.put_line('ad = ' ||ad);
23 close c_nm;
24 end;
25 /
ad = 6
PL/SQL procedure successfully completed.
SQL>
A simpler option might be this: split number into digits (each in its separate row) so that you could apply SUM function to its even digits:
SQL> select * from numb;
NUM
----------
121974553 --> sum of even digits = 2 + 9 + 4 + 5 = 20
253412648 --> = 5 + 4 + 2 + 4 = 15
SQL> with
2 split as
3 -- split number into rows
4 (select num,
5 substr(num, column_value, 1) digit,
6 case when mod(column_value, 2) = 0 then 'Y'
7 else 'N'
8 end cb_odd_even
9 from numb cross join table(cast(multiset(select level from dual
10 connect by level <= length(num)
11 ) as sys.odcinumberlist))
12 )
13 -- final result: summary of digits in even rows
14 select num,
15 sum(digit)
16 from split
17 where cb_odd_even = 'Y'
18 group by num;
NUM SUM(DIGIT)
---------- ----------
253412648 15
121974553 20
SQL>
If it must be PL/SQL, slightly rewrite it as
SQL> declare
2 result number;
3 begin
4 for cur_r in (select num from numb) loop
5 with
6 split as
7 -- split number into rows
8 (select substr(cur_r.num, level, 1) digit,
9 case when mod(level, 2) = 0 then 'Y'
10 else 'N'
11 end cb_odd_even
12 from dual
13 connect by level <= length(cur_r.num)
14 )
15 -- final result: summary of digits in even rows
16 select sum(digit)
17 into result
18 from split
19 where cb_odd_even = 'Y';
20
21 dbms_output.put_line(cur_r.num || ' --> ' || result);
22 end loop;
23 end;
24 /
121974553 --> 20
253412648 --> 15
PL/SQL procedure successfully completed.
SQL>
You could do something like this. A few things of note: first, you can use an implicit cursor (the for rec in (select ...) loop construct). You don't need to declare the data type of rec, you don't need to open the cursor - and then remember to close it when you are finished - etc. Implicit cursors are a great convenience, best to learn about it as early as possible. Second, note that rec (in my notation) is just a pointer; to access the value from it, you must reference the table column (as in, rec.num) - Littlefoot has already shown that in his reply. Third, a logic mistake in your attempt: if the table has more than one row, you must initialize ad to 0 for each new value - you can't just initialize it to 0 once, at the beginning of the program.
So, with all that said - here is some sample data, then the procedure and its output.
create table numb (num int);
insert into numb values(121975443);
insert into numb values(100030000);
insert into numb values(null);
insert into numb values(113313311);
insert into numb values(2020);
commit;
. . . . .
declare
ad int;
i int;
begin
for rec in (select num from numb) loop
ad := 0;
i := rec.num;
while i > 0 loop
if mod(i, 2) = 0 then
ad := ad + mod(i, 10);
end if;
i := trunc(i/10);
end loop;
dbms_output.put_line('num: ' || nvl(to_char(rec.num), 'null') ||
' sum of even digits: ' || ad);
end loop;
end;
/
num: 121975443 sum of even digits: 10
num: 100030000 sum of even digits: 0
num: null sum of even digits: 0
num: 113313311 sum of even digits: 0
num: 2020 sum of even digits: 4
PL/SQL procedure successfully completed.

Prime number code - please help me in solving this error ('missing if'?)

SQL> ed
Wrote file afiedt.buf
1 declare
2 n number;
3 i number;
4 counter number;
5 begin
6 n:=&n;
7 i:=1;
8 counter:=0;
9 if n=1
10 then dbms_output.put_line('1 is a prime No.');
11 else if n=2
12 then dbms_output.put_line('2 is even prime');
13 else
14 for i in 1..n loop
15 if mod(n,i)=0
16 then counter:=counter+1;
17 end if;
18 end loop;
19 end if;
20 if counter=2
21 then dbms_output.put_line(n||' is a prime No.');
22 else
23 dbms_output.put_line(n||' is a not prime No.');
24 end if;
25* end
I get the following error which I don't understand. Can anyone explain what's causing it?
SQL> /
Enter value for n: 8
old 6: n:=&n;
new 6: n:=8;
end
*
ERROR at line 25:
ORA-06550: line 25, column 3:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the
following:
if
It's because of this code segment, formatted to give you a better understnding why:
9 if n=1
10 | then dbms_output.put_line('1 is a prime No.');
11 else
| if n=2
12 | | then dbms_output.put_line('2 is even prime');
13 | else
14 | | for i in 1..n loop
15 | | | if mod(n,i)=0
16 | | | | then counter:=counter+1;
17 | | | end if;
18 | | end loop;
19 | end if;
?
In other words, that section is missing an end if. That's why, when you finish your file on line 25 with end, it complains about not having an if after it (to make end if).
Just whack an end if immediately following line 19 and that should fix it.
That should fix your immediate problem but I have a couple of quick comments as well.
if you use for i in 2..n loop (start at 2 instead of 1), and test counter being greater than 1 (instead of equal to 2), that should save some work. mod(n,1) is zero for all n.
in any case, the original test should have been for greater than 2, not equal: the number 12 would have given you a counter of 5 (one each for 1, 2, 3, 4 and 6) and so would be considered non-prime.
for efficiency, you should remember that you only ever have to check up to trunc(sqrt(n))+1 (or the equivalent in whatever language you're using) since, if a number higher than that is a factor, you would have found its pair already: 12 mod 4 is zero but you've already found its pair of 3 (12 mod (12/4) is zero).
make sure that the 2..n does not include n since the mod operation there will also increment counter (I don't know how your specific language handles that loop construct) - it may have to be 2..n-1.
I think you're looking for the PL/SQL ELSIF keyword. I took your code, replaced
else if n=2
on line 11 with
elsif n=2
and it worked.
For every IF we should close it by using END IF;
But when we use ELSEIF one is enough.
IF
......
ELSEIF
......
END IF;
or
IF
......
ELSE IF
.......
END IF;
END IF;
1) You need a semicolon after the final end at line 25. 2) Then you get another error, (See note 1 below) and need to correct with by substituting elsif for else if at line 11. 3) Finaly, 1 not being prime, line 10 needs correction also, then dbms_output.put_line('1 is neither prime nor composite.); So the corrected code is:
SQL> declare
2 n number;
3 i number;
4 counter number;
5 begin
6 n:=&n;
7 i:=1;
8 counter:=0;
9 if n=1
10 then dbms_output.put_line('1 is neither prime nor composite.');
11 elsif n=2
12 then dbms_output.put_line('2 is even prime');
13 else
14 for i in 1..n loop
15 if mod(n,i)=0
16 then counter:=counter+1;
17 end if;
18 end loop;
19 end if;
20 if counter=2
21 then dbms_output.put_line(n||' is a prime No.');
22 else
23 dbms_output.put_line(n||' is a not prime No.');
24 end if;
25 end;
26 /
Enter value for n: 3
old 6: n:=&n;
new 6: n:=3;
3 is a prime No.
PL/SQL procedure successfully completed.
Also, see paxdiablo's answer for additional notes on improving this code with respect to prime numbers.
Note 1: The second syntax error looks like:
SQL> declare
2 n number;
3 i number;
4 counter number;
5 begin
6 n:=&n;
7 i:=1;
8 counter:=0;
9 if n=1
10 then dbms_output.put_line('1 is a prime No.');
11 else if n=2
12 then dbms_output.put_line('2 is even prime');
13 else
14 for i in 1..n loop
15 if mod(n,i)=0
16 then counter:=counter+1;
17 end if;
18 end loop;
19 end if;
20 if counter=2
21 then dbms_output.put_line(n||' is a prime No.');
22 else
23 dbms_output.put_line(n||' is a not prime No.');
24 end if;
25 end;
26 /
Enter value for n: 10
old 6: n:=&n;
new 6: n:=10;
end;
*
ERROR at line 25:
ORA-06550: line 25, column 4:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
if
DECLARE
N NUMBER:=&N;
V_COUNT NUMBER:=0;
BEGIN
FOR I IN 1..N LOOP
IF MOD(N,I)=0 THEN
V_COUNT:=V_COUNT+1;
END IF;
END LOOP;
IF V_COUNT<=2 THEN
DBMS_OUTPUT.PUT_LINE(N||' ' ||'IS PRIME NUMBER');
ELSE
DBMS_OUTPUT.PUT_LINE(N||' '||' IS NOT PRIME NUMBER');
END IF;
END;
declare
n number;
i number;
counter number;
begin
n := &n;
i := 1;
counter := 0;
if n = 1 then
dbms_output.put_line('1 is a prime No.');
elsif n = 2 then
dbms_output.put_line('2 is even prime');
else
for i in 1 .. n loop
if mod(n, i) = 0 then
counter := counter + 1;
end if;
end loop;
end if;
if counter = 2 then
dbms_output.put_line(n || ' is a prime No.');
else
dbms_output.put_line(n || ' is a not prime No.');
end if;
end;
prime number checking.
DECLARE
N NUMBER :=&N;
I NUMBER;
COUNTER NUMBER :=0;
BEGIN
FOR I IN 1..N LOOP
IF(MOD(N,I) =0) THEN
COUNTER :=COUNTER+1;
END IF;
END LOOP;
IF (COUNTER =2) THEN
DBMS_OUTPUT.PUT_LINE(N ||' '||'IS PRIME NUMBER');
ELSE
DBMS_OUTPUT.PUT_LINE(N ||' '||'IS NOT A PRIME NUMBER');
END IF;
END;

Resources