how to find sum of even digit in plsql? - oracle

declare
n number(4);
s number(4);
i number(4);
count number(4);
begin
n:=&n;
s:=0;
count:=0;
while (n>0 AND count MOD 2 = 0)
loop
i:= n mod 10;
s:=s+i;
n:=trunc(n / 10) ;
count:=count+1;
end loop;
dbms_output.put_line('Sum of digit = ' || s);
end;
I have tried it but getting error:
ERROR at line 12:
ORA-06550: line 12, column 23:
PLS-00204: function or pseudo-column 'COUNT' may be used inside a SQL statement
only
ORA-06550: line 12, column 8:
PL/SQL: Statement ignored

You must rename COUNT variable because it is reserved word in Oracle.
But I am not sure that your logic is right.
First step - cnt=0, mod(cnt , 2) = 0, you inside your loop
Second step - cnt=1, mod (cnt,2) = 1, you will not step inside cycle and while will be ended. Maybe you should do IF MOD(cnt,2) = 0 THEN ....
I renamed count to cnt

MOD is a function, not an operator. Try:
while (n > 0 AND MOD(count, 2) = 0)

count should be "count" bcz it's reserved word condition in while is not correct. if as in sample below n=150 result will be zero
declare
n number(4);
s number(4);
i number(4);
"count" number(4);
begin
n:=150;
s:=0;
"count":=0;
while (n>0)
loop
i:= n mod 10;
s:=s+i;
n:=trunc(n / 10) ;
if ("count" MOD 2 = 0)
then
"count":="count"+1;
end if;
end loop;
dbms_output.put_line('Sum of digit = ' || s);
end;

Related

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

TOP with index variable oracle

I was trying to use the variable 'ind' to get the ROWNUM on my select, but everytime I try to use the variable there I get a error like:
ORA-01008: not all variables bound
or these two:
ORA-01403: no data found
ORA-06512: at line 10
DECLARE
texto VARCHAR2(255);
ind NUMBER := 0;
BEGIN
LOOP
ind := ind + 1;
IF ind > 3 THEN
EXIT;
END IF;
SELECT TEXTO_LOG
INTO texto
from table WHERE REGEXP_LIKE(TEXTO_LOG, 'Alteração') AND ROWNUM >= :ind AND ROWNUM <= :ind ;
dbms_output.put_line(substr(trim(texto), 1, instr(texto, ' ')));
dbms_output.put_line(substr(texto, 0, 100));
END LOOP;
END;
/
I searched and found someone telling that not all variables bound is a bug, I'm not sure if it's.
I tried the ROWNUM with different opperators. Any suggestions?
Usage of rownum is the problem here apart from other issues already answered. a predicate like rownum>=2 and rownum <=2 (and so on...till exit point) yield no result and thus no_data_found error
But as workaround put the rownum inside from clause and restrict it outside should work,
DECLARE
texto VARCHAR2(255);
ind NUMBER := 0;
BEGIN
LOOP
ind := ind + 1;
IF ind > 3
THEN
EXIT;
END IF;
SELECT texto_log
INTO texto
FROM (SELECT texto_log
,rownum myrownum
FROM TABLE
WHERE regexp_like(texto_log
,'Alteração'))
WHERE myrownum >= ind
AND myrownum <= ind;
dbms_output.put_line(substr(TRIM(texto)
,1
,instr(texto
,' ')));
dbms_output.put_line(substr(texto
,0
,100));
END LOOP;
END;
/
The error is that you are referring to :ind as if it were a bind variable, when it is not. It is in fact a variable declared in your DECLARE section.
You might try this
** Update **
As your query looks like it is faling, the reason perhaps is in the query itself. Run this and get the output and running separately
set serveroutput on size unlimited
DECLARE
texto VARCHAR2(255);
ind NUMBER := 0;
BEGIN
for r in 1..4
LOOP
ind := r + 1;
dbms_output.put_line ( q'[SELECT TEXTO_LOG
INTO texto
from table WHERE REGEXP_LIKE(TEXTO_LOG, 'Alteração') AND ROWNUM <= ind ;
dbms_output.put_line(substr(trim(texto), 1, instr(texto, ' ')));
dbms_output.put_line(substr(texto, 0, 100));]');
exit when ind > 3;
END LOOP;
END;
/
Although you can build it like this which is easier
DECLARE
texto VARCHAR2(255);
ind NUMBER := 0;
BEGIN
for r in 1..4
LOOP
ind := r + 1;
dbms_output.put_line(ind);
SELECT TEXTO_LOG
INTO texto
from table WHERE REGEXP_LIKE(TEXTO_LOG, 'Alteração') AND ROWNUM >= ind AND ROWNUM <= ind ;
dbms_output.put_line(substr(trim(texto), 1, instr(texto, ' ')));
dbms_output.put_line(substr(texto, 0, 100));
exit when ind > 3;
END LOOP;
END;
/
EXample
SQL> DECLARE
texto VARCHAR2(255);
ind NUMBER := 0;
BEGIN
for r in 1..4
LOOP
ind := r + 1;
dbms_output.put_line(ind);
exit when ind > 3;
END LOOP;
END;
/ 2 3 4 5 6 7 8 9 10 11 12
2
3
4
PL/SQL procedure successfully completed.
SQL>
With implicit cursor
DECLARE
-- texto VARCHAR2(255);
ind NUMBER := 0;
BEGIN
FOR i IN (SELECT TEXTO_LOG FROM table WHERE REGEXP_LIKE(TEXTO_LOG, 'Alteração'))
LOOP
ind:=ind+1;
EXIT WHEN ind >3;
DBMS_OUTPUT.PUT_LINE(SUBSTR(TRIM(i.TEXTO_LOG), 1, INSTR(i.TEXTO_LOG, ' ')));
DBMS_OUTPUT.PUT_LINE(SUBSTR(i.TEXTO_LOG, 0, 100));
END LOOP;
END;

Error in PL/SQL code and cannot understand the error and mistake in the code

declare
sum number:=0;
count number:=0;
pnum number:=0;
temp number;
begin
for i in 1..25
loop
temp:=i;
count:=0;
for j in 1..25
loop
if mod(i,j)=0 then
count:=count+1;
end if;
end loop;
if count=2 then
sum:=sum+temp;
pnum:=pnum+1;
end if;
exit when pnum=10;
end loop;
dbms_output.put_line(sum);
end;
Error encountered on Oracle server:
ORA-06550: line 21, column 17: PLS-00103: Encountered the symbol "+"
when expecting one of the following:
(
ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_180200", line 548 ORA-06550: line
28, column 25: PLS-00103: Encountered the symbol ")" when expecting
one of the following:
(
You are using reserved words SUM and COUNT; if you edit the name of your variables your code will work:
DECLARE
vSUM NUMBER := 0;
vCOUNT NUMBER := 0;
pnum NUMBER := 0;
temp NUMBER;
BEGIN
FOR i IN 1 .. 25
LOOP
temp := i;
vCOUNT := 0;
FOR j IN 1 .. 25
LOOP
IF MOD(i, j) = 0
THEN
vCOUNT := vCOUNT + 1;
END IF;
END LOOP;
IF vCOUNT = 2
THEN
vSUM := vSUM + temp;
pnum := pnum + 1;
END IF;
EXIT WHEN pnum = 10;
END LOOP;
DBMS_OUTPUT.put_line(vSUM);
END;

increment function in plsql varchar2

I want use this in a varchar2. Ex.:
declare
num number := &Number;
serie varchar2(200) := 'S = ';
begin
for x in 1 .. num loop
serie += x, ' + ';
end loop; `
end;
/
In the end I want that the serie be like "S = 1 + 2 + 3 ..." How can i make that work?
That would be something like this:
SQL> set serveroutput on
SQL> declare
2 num number := &Number;
3 serie varchar2(200) := 'S = ';
4 begin
5 for x in 1 .. num loop
6 serie := serie || to_char(x) || ' + ';
7 end loop;
8
9 -- remove the trailing "+"
10 serie := rtrim(serie, ' +');
11 dbms_output.put_Line(serie);
12 end;
13 /
Enter value for number: 5
S = 1 + 2 + 3 + 4 + 5
PL/SQL procedure successfully completed.
SQL>
A few comments:
line 6: you have to concatenate (concatenation operator is a double pipe sign, ||) previous value of SERIE; otherwise, you'd have only the last number in it
line 10: remove the trailing "+" sign
What could be done in pure SQL, usually should be done in pure SQL:
declare
num number := &Number;
serie varchar2(200);
begin
select 'S = ' || listagg(rownum, ', ') within group (order by rownum)
into serie
from dual
connect by level <= num;
dbms_output.put_line(serie);
end;
/
The result for num = 10:
S = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Note, listagg function was introduced in Oracle version 11.2.

PL SQL wrong output

I am trying to write a code which checks whether a number is binary or not.
Here is my code.
declare
n INTEGER:=&num;
ch number:=0;
begin
loop
exit when n=0;
if(mod(n,10)!=0 or mod(n,10)!=1) then
ch:=1;
exit;
end if;
n:=n/10;
end loop;
if ch=1 then
dbms_output.put_line('Not a Binary number.');
else
dbms_output.put_line('Binary!!!');
end if;
end;
/
I am using Oracle 11g SQL Plus.
Sometimes it is giving error at line 2. Here is the snippet of error.
old 2: n INTEGER:=&num;
new 2: n INTEGER:=;
n INTEGER:=;
*
ERROR at line 2:
ORA-06550: line 2, column 12:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
( - + case mod new not null <an identifier>
And if it is running correctly then for every number it is giving the same output as 'Not a Binary number.'.
Change the OR by AND in the IF statement. It seems to do what you want.
DECLARE
n INTEGER := &num;
ch NUMBER := 0;
BEGIN
LOOP
EXIT WHEN n = 0;
IF MOD(n, 10) != 0
AND MOD(n, 10) != 1
THEN
ch := 1;
EXIT;
END IF;
n := n / 10;
END LOOP;
IF ch = 1
THEN
dbms_output.put_line('Not a Binary number.');
ELSE
dbms_output.put_line('Binary!!!');
END IF;
END;
/

Resources