DBNS Ex-10
--1.write a pl\sql block to add 2 numbers.
DECLARE
num1 NUMBER := 10;
num2 NUMBER := 20;
sum NUMBER;
BEGIN
sum := num1 + num2;
DBMS_OUTPUT.PUT_LINE('Sum: ' || sum);
END;
--2.area of rectangle,triangle,square
--Area of Rectangle
DECLARE
l NUMBER := 10;
b NUMBER := 20;
area_rect NUMBER;
BEGIN
area_rect:=lb;
DBMS_OUTPUT.PUT_LINE('Area of Rectangle: ' || area_rect);
END;
/
--Area of Triangle
DECLARE
b NUMBER := 10;
h NUMBER := 20;
area_tri NUMBER;
BEGIN
area_tri:=(bh)/2;
DBMS_OUTPUT.PUT_LINE('Area of Triangle: ' || area_tri);
END;
/
--Area of Square
DECLARE
a NUMBER := 10;
area_sq NUMBER;
BEGIN
area_sq:= a*a;
DBMS_OUTPUT.PUT_LINE('Area of Square: ' || area_sq);
END;
/
--block to find maximum 3 numbers
DECLARE
num1 NUMBER := 10;
num2 NUMBER := 20;
num3 NUMBER := 30;
max_num NUMBER;
BEGIN
-- Find the maximum of the three numbers.
IF num1 >= num2 AND num1 >= num3 THEN
max_num := num1;
ELSIF num2 >= num1 AND num2 >= num3 THEN
max_num := num2;
ELSE
max_num := num3;
END IF;
-- Display the maximum number.
DBMS_OUTPUT.PUT_LINE('Maximum Number: ' || max_num);
END;
/
--4.Write a PL/SQL Block to print sum of N Numbers using For Loop.
DECLARE
N NUMBER := 10; -- You can change this value to sum different numbers
sum_of_numbers NUMBER := 0;
BEGIN
FOR i IN 1 .. N LOOP
sum_of_numbers := sum_of_numbers + i;
END LOOP;
DBMS_OUTPUT.PUT_LINE('The sum of the first ' || N || ' natural numbers is: ' || sum_of_numbers);
END;
/
--5.Write a PL/SQL Block to generate Fibonacci series of N numbers
DECLARE
N NUMBER := 10; -- You can change this value to generate more or fewer Fibonacci numbers
first NUMBER := 0;
second NUMBER := 1;
next NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('Fibonacci series for the first ' || N || ' numbers:');
IF N >= 1 THEN
DBMS_OUTPUT.PUT_LINE(first); -- Print the first Fibonacci number
END IF;
IF N >= 2 THEN
DBMS_OUTPUT.PUT_LINE(second); -- Print the second Fibonacci number
END IF;
FOR i IN 3 .. N LOOP
next := first + second; -- Calculate the next Fibonacci number
DBMS_OUTPUT.PUT_LINE(next); -- Print the next number
-- Update the first and second numbers for the next iteration
first := second;
second := next;
END LOOP;
END;
/