fibonacci series program using functions in pl/sql - plsql

I've to create a function which print the Fibonacci series as its result. I've used a varray in the program below but it is giving me an error saying "PLS-00201: identifier 'ARRAY' must be declared" on line no. 2.
create function fibonacci7(x int)
return VARRAY
is
type fib IS VARRAY(25) OF VARCHAR(10);
a number(3):=1;
b number(3):=1;
c number(3);
i number(3):=1;
begin
while a<=n
loop
fib(i) := a;
c:=a+b;
a:=b;
b:=c;
i:=i+1;
end loop;
return codes_;
end ;
/
select fibonacci7(5) from dual;

I think this will do what you want. VARRAY's have a little different syntax than what you were using.
set serveroutput on size 1000000
create or replace type fibtype AS VARRAY(25) OF NUMBER;
/
create or replace function fibonacci7(n number)
return fibtype
is
fib fibtype := fibtype();
a number:=1;
b number:=1;
c number;
i number:=1;
begin
fib.extend(n);
while i<=n
loop
fib(i) := a;
c:=a+b;
a:=b;
b:=c;
i:=i+1;
end loop;
return fib;
end ;
/
declare
i number;
fib fibtype := fibtype();
begin
fib := fibonacci7(6);
for i in 1..fib.count loop
dbms_output.put_line(to_char(fib(i)));
end loop;
end;
/
Here is the output.
1
1
2
3
5
8
Bobby
p.s. Fixed to work with fib(6) and made array numbers

Related

what is program in plsql to check whether it is prime using function and procedure

declare
K number;
Lnumber;
f number;
function prime(n in number, I in number) return is flag
begin
i:=2;
flag:=1;
n:=&n;
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;
return flag;
end;
begin
k:=4;
L:=1;
f:= prime(n,i);
dbms_output.put_line(given number is ||flag);
end;
/
I am getting output as this I wonder what is wrong with my code:
Enter value for n: 4
old 9: n:=&n;
new 9: n:=4;
declare
*
ERROR at line 1:
ORA-06540: PL/SQL: compilation error
ORA-06553: PLS-906: Compilation is not possible
q2
SQL> DECLARE
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;
.
in this 2nd code the result is not getting displayed , not only for this even for other codes it is showing the line PL/SQL procedure successfully completed. but the results are not displaying what is wrong with it
A fixed version of q1 might be something like this (comments inline):
declare
-- k number; -- not used
-- l number; -- previously "lnumber;" not used anyway
f number;
function prime
( n in number ) -- "i" parameter never used
return number -- added return type
is
flag number := 1; -- added datatype, initial value and semicolon
begin
-- i := 2; -- Can't modify an IN parameter
-- flag := 1; -- not needed, can assign a value when declaring
-- n := &n; -- Can't modify an IN parameter
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;
return flag;
end prime; -- added closing tag for clarity
begin
-- k := 4; -- not used
-- l := 1; -- not used
-- f := prime(n, i); -- n and i are not declared
f := prime(123);
dbms_output.put_line('prime check returns '|| f); -- added quotes, changed "flag" to "f"
end;
Also I formatted the code to make it more readable. This also helps when writing code, as it keeps the structure clearly visible and makes some errors more obvious.
q2 works for me, so you probably just need to check how whatever tool you are using displays dbms_output. In some tools there is a checkbox, others require a set server output on command.
You can also provide a function implementation of your factorial procedure:
declare
num number;
c number;
procedure fact
( num in number
, f out number )
is
begin
f := 1;
for i in 2 .. num loop
f := f * i;
end loop;
end fact;
function fact
( num number )
return number
is
f number;
begin
fact(num, f);
return f;
end fact;
begin
num := 6;
fact(num, c);
dbms_output.put_line('Factorial of ' || num || ' is ' || c);
dbms_output.put_line('Factorial of ' || num || ' is ' || fact(num));
end;

How to use recursion to add the product of two Integers together?

In this code, I am asking the user to input two integers (Index, Mindex) and then I display all the integers between 1..Index and 1..Mindex. What my problem is here that I do not know how to multiply the values of Integers in Index and Integers in `Mindex and then add up the product of these two together
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Add is
Index, Mindex : Integer;
procedure calc (Item : in Integer) is
New_Value : Integer;
begin
Put ("The value of the index is now");
Put (Item);
New_Line;
New_Value := (Item - 1);
if New_Value > 0 then
calc (New_Value);
end if;
end calc;
begin
Get (Index);
Get (Mindex);
calc (Index);
New_Line;
calc (Mindex);
end Add;
A factorial keeps chaining multiplication with each decreasing value: 5! = 5 * 4 * 3 * 2 * 1 = 120. In order to do the recursion, you'll need to have two cases inside your recursive function: If your value is above 1, then multiply that value with the next smallest number. That's the recursive part where you will call Factorial(N-1) inside of Factorial(N). Otherwise just return 1 (factorial of 0 is 1 mathematically, so both 1! and 0! equal 1).
The way this works in Ada is:
function Factorial(Value : Natural) return Natural is
begin
if Value > 1 then
-- Keep chaining the multiplication with recursion
return Value * Factorial(Value - 1);
else
-- No need to chain as the result is always 1
return 1;
end if;
end Factorial;
You can then call that Factorial function on each of your numbers and add the results.

Print sum of n numbers in plsql

CREATE OR REPLACE FUNCTION printsum(n IN number) IS
res number:=0;
BEGIN
while(n>0)
LOOP
res:=res+n;
n:=n-1;
EXIT WHEN n=0;
END LOOP;
dbms_output.put_line(' result of sum: '||res);
END;
/
I'm trying to print sum of n numbers, but I'm getting the following error:
Warning: Function created with compilation errors.
Two things:
number strictly speaking is not necessarily integral so best to use pls_integer or at least check in the code that the passed in argument is integral
if the question is asking how to return the sum of the first n positive integers, the correct answer is to used the closed form formula: n * (n + 1)/2
Using the formula gives you a constant time answer.
function printsum(n pls_integer) return n is
begin
if(n < 0) then raise value_error; end if; -- or a more meaningful exception
return (n * (n + 1) / 2);
end;
There are few errors in your code.
1) Function must have a Return but your code had missing Return statement at beginning and at end.
2) IN parameter cannot be reassigned inside the code. So you need to copy the IN parameter to a variable to iterate.
Try this:
CREATE OR REPLACE FUNCTION printsum( n IN NUMBER)
RETURN NUMBER
IS
res NUMBER:=0;
v_num NUMBER:=n;
BEGIN
WHILE(v_num>0)
LOOP
res := res + v_num;
v_num := v_num -1;
EXIT WHEN v_num=0;
END LOOP;
dbms_output.put_line(' result of sum: '||res);
RETURN(res);
END;
/
Output:
SQL> select printsum(10) from dual;
PRINTSUM(10)
------------
55
The following code snippet is the solution:
declare
sm number;
tmp number;
n number;
i number;
function sumn(n integer)
return number
is
sm number;
begin
sm:=0;
tmp:=n;
for i in 1..tmp LOOP
sm:=sm+i;
END LOOP;
return sm;
END;
begin
n:=10;
sm:=sumn(n);
dbms_output.put_line('sum is',sm);
end;
/

generating prime numbers in pl/sql

Please tell me the problem in the code. I have written this code and its not working. Tell me the mistakes or if there is any other and easy method to generate prime numbers till 1000.
declare
i number;
prime number;
j number;
begin
for i in 2 .. 1000 loop
prime := 0;
for j in 2 .. i/2 loop
if mod(i,j)=0 then prime := 1
end if;
end loop;
if prime = 0 then dbms_output.put_line(i||'&');
end if;
end loop;
end;
You already have your answer (missing semicolon), but just for fun:
The i variable declared at the top is not used.
In theory j would be more efficient as a pls_integer (as i is implicitly). Possibly even a simple_integer, but then you'd need to restructure the loop to make i a simple_integer as well, and it's barely worth it for the tiny fraction of a second you might gain, if the compiler hasn't already optimised it.
You might as well exit the inner loop at the first match, rather than checking every single number.
prime would be more readable as a Boolean.
On the subject of readability, it is standard practice to align end loop under its opening loop statement.
I'm not seeing the point of appending & to every line of output.
This gives me:
declare
j pls_integer;
prime boolean;
begin
for i in 2 .. 1000 loop
prime := true;
for j in 2 .. i/2 loop
if mod(i,j) = 0 then
prime := false;
exit;
end if;
end loop;
if prime then
dbms_output.put_line(i);
end if;
end loop;
end;
You have missed one semicolon and try to put set server output on then run it
set serveroutput on
declare
i number;
prime number;
j number;
begin
for i in 2 .. 1000 loop
prime := 0;
for j in 2 .. i/2 loop
if mod(i,j)=0 then prime := 1;
end if;
end loop;
if prime = 0 then dbms_output.put_line(i||'&');
end if;
end loop;
end;
/

Oracle PLSQL case statement

Why won't this CASE statement work in pl/sql? I'm trying to assign multiple values in one go. Is there a faster way to assign multiple values based on the below? Thanks.
DECLARE
x number := 5;
a CHAR;
b CHAR;
c CHAR;
BEGIN
CASE
WHEN x = 5 THEN a :='rx' AND b := 'rt' AND c :='ry';
WHEN ...
WHEN ...
END
END;
Try this:
DECLARE
x NUMBER := 5;
a VARCHAR2(2);
b VARCHAR2(2);
c VARCHAR2(2);
BEGIN
IF(x = 5)
THEN
a :='rx';
b := 'rt';
c :='ry';
END IF;
END;
You should use case keyword inside sql queries, but in this scenario when you want to set varaibels you need to use If Then Statement.

Resources