assingment5

–1. Print “HELLO WORLD”
 
BEGIN
    DBMS_OUTPUT.PUT_LINE(‘HELLO WORLD’);
END;
/
 
— Output: HELLO WORLD
 
    
–2. Check if a number is even or odd
 
DECLARE
    num INTEGER := 10; — Change the number to be tested here
 
BEGIN
    IF MOD(num, 2) = 0 THEN
        DBMS_OUTPUT.PUT_LINE(num || ‘ is even’);
    ELSE
        DBMS_OUTPUT.PUT_LINE(num || ‘ is odd’);
    END IF;
END;
/
 
— Output: 10 is even
 
    
—-3. Check if a number is prime or not
 
 
 
DECLARE
    num INTEGER := 17; — Change the number to be tested here
    is_prime BOOLEAN := TRUE;
 
BEGIN
    FOR i IN 2..SQRT(num) LOOP
        IF MOD(num, i) = 0 THEN
            is_prime := FALSE;
            EXIT;
        END IF;
    END LOOP;
 
    IF is_prime AND num > 1 THEN
        DBMS_OUTPUT.PUT_LINE(num || ‘ is prime’);
    ELSE
        DBMS_OUTPUT.PUT_LINE(num || ‘ is not prime’);
    END IF;
END;
/
 
— Output: 17 is prime
–4. Insert a record into the accounts table
create table accounts (
    ac_no integer,
    nm integer,
    balance integer
);
 
declare
    ac_no integer:=1;
nm integer:=2;
balance integer:=3;
begin
INSERT INTO accounts values (ac_no, nm, balance);
end;
 
select * from accounts;
 
–5. Calculate factorial of a given number
 
DECLARE
    num INTEGER := 5; — Change the number for which you want to calculate factorial
    factorial NUMBER := 1;
 
BEGIN
    FOR i IN 1..num LOOP
        factorial := factorial * i;
    END LOOP;
 
    DBMS_OUTPUT.PUT_LINE(‘Factorial of ‘ || num || ‘ is ‘ || factorial);
END;
 
 
— Output: Factorial of 5 is 120
–6. Print the first N Fibonacci numbers
 
 
 
DECLARE
    n INTEGER := 10; — Change the number of Fibonacci numbers to generate
 
    fib1 NUMBER := 0;
    fib2 NUMBER := 1;
    next_fib NUMBER;
 
BEGIN
    DBMS_OUTPUT.PUT(fib1 || ‘ ‘ || fib2);
 
    FOR i IN 3..n LOOP
        next_fib := fib1 + fib2;
        DBMS_OUTPUT.PUT(‘ ‘ || next_fib);
 
        fib1 := fib2;
        fib2 := next_fib;
    END LOOP;
dbms_output.put_line(‘ ‘);
END;
/
 
— Output: 0 1 1 2 3 5 8 13 21 34
–7. Invert a given number
 
 
 
DECLARE
    num INTEGER := 1234; — Change the number to be inverted
    inverted_num INTEGER := 0;
    remainder INTEGER;
 
BEGIN
    WHILE num > 0 LOOP
        remainder := MOD(num, 10);
        inverted_num := inverted_num * 10 + remainder;
        num := num / 10;
    END LOOP;
 
    DBMS_OUTPUT.PUT_LINE(‘Inverted number: ‘ || inverted_num);
END;
/
 
— Output: Inverted number: 4321
 
    
–8. Calculate the maximum of three numbers
 
DECLARE
    num1 INTEGER := 10;
    num2 INTEGER := 20;
    num3 INTEGER := 15;
    max_num INTEGER;
 
BEGIN
    max_num := GREATEST(num1, num2, num3);
    DBMS_OUTPUT.PUT_LINE(‘Maximum of ‘ || num1 || ‘, ‘ || num2 || ‘, ‘ || num3 || ‘ is ‘ || max_num);
END;
/
 
— Output: Maximum of 10, 20, 15 is 20
 
    
–9. Calculate the area of a circle, update diameter, and count records
 
— Assuming you have a table named “Circle” with columns “radius”, “area”, and “diameter”
 
create table circle (
    radius integer,
    area integer
);
    
— i) Calculate the area of a circle and store it in the table
DECLARE
    radius INTEGER := 3; — Change the radius value
 
BEGIN
    for i in 3..7 loop
INSERT INTO Circle (radius, area)
    VALUES (radius, ROUND(3.14 * radius * radius, 2));
radius:=radius+1;
end loop;
END;
/
 
select * from circle;
    
— ii) Update the diameter column for each entry
alter table circle 
add diameter integer;
 
UPDATE Circle
SET diameter = 2 * radius;
 
select * from circle;
 
— iii) Print the number of records in the Circle table using an explicit cursor
DECLARE
    record_count INTEGER := 0;
    CURSOR c_circle_records IS SELECT COUNT(*) FROM Circle;
BEGIN
    OPEN c_circle_records;
    FETCH c_circle_records INTO record_count;
    CLOSE c_circle_records;
 
    DBMS_OUTPUT.PUT_LINE(‘Number of records in Circle table: ‘ || record_count);
END;

 

Scroll to Top