0% found this document useful (0 votes)
54 views6 pages

Assignment 7

The document contains a series of PL/SQL code snippets that perform various operations such as checking for leap years, palindromes, Armstrong numbers, calculating GCD and LCM, identifying perfect numbers, counting vowels and consonants in a string, updating account balances, adding employees, and deleting records from a database. Each snippet includes variable declarations, control structures, and output statements using DBMS_OUTPUT. The code is structured to handle specific input values and perform the corresponding calculations or database operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views6 pages

Assignment 7

The document contains a series of PL/SQL code snippets that perform various operations such as checking for leap years, palindromes, Armstrong numbers, calculating GCD and LCM, identifying perfect numbers, counting vowels and consonants in a string, updating account balances, adding employees, and deleting records from a database. Each snippet includes variable declarations, control structures, and output statements using DBMS_OUTPUT. The code is structured to handle specific input values and perform the corresponding calculations or database operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASSIGNMENT-7 DBMS

1) DECLARE

year NUMBER := &year;

BEGIN

IF (MOD(year, 4) = 0 AND MOD(year, 100) <> 0) OR (MOD(year, 400) = 0) THEN

DBMS_OUTPUT.PUT_LINE(year || ' is a Leap Year.');

ELSE

DBMS_OUTPUT.PUT_LINE(year || ' is not a Leap Year.');

END IF;

END;

2) DECLARE

num NUMBER := &num;

rev NUMBER := 0;

temp NUMBER;

digit NUMBER;

BEGIN

temp := num;

WHILE temp > 0 LOOP

digit := MOD(temp, 10);

rev := rev * 10 + digit;

temp := TRUNC(temp / 10);

END LOOP;

IF num = rev THEN

DBMS_OUTPUT.PUT_LINE(num || ' is a Palindrome.');

ELSE

DBMS_OUTPUT.PUT_LINE(num || ' is not a Palindrome.');


END IF;

END;

3) DECLARE

num NUMBER := &num;

temp NUMBER;

sum NUMBER := 0;

digit NUMBER;

BEGIN

temp := num;

WHILE temp > 0 LOOP

digit := MOD(temp, 10);

sum := sum + POWER(digit, 3);

temp := TRUNC(temp / 10);

END LOOP;

IF num = sum THEN

DBMS_OUTPUT.PUT_LINE(num || ' is an Armstrong Number.');

ELSE

DBMS_OUTPUT.PUT_LINE(num || ' is not an Armstrong Number.');

END IF;

END;

4) DECLARE

a NUMBER := &a;

b NUMBER := &b;

c NUMBER := &c;

gcd_ab NUMBER;

gcd_abc NUMBER;
lcm NUMBER;

FUNCTION GCD(x NUMBER, y NUMBER) RETURN NUMBER IS

BEGIN

WHILE y <> 0 LOOP

x := x MOD y;

EXIT WHEN x = 0;

y := y MOD x;

END LOOP;

RETURN CASE WHEN x = 0 THEN y ELSE x END;

END;

BEGIN

gcd_ab := GCD(a, b);

gcd_abc := GCD(gcd_ab, c);

lcm := (a * b * c) / gcd_abc;

DBMS_OUTPUT.PUT_LINE('GCD: ' || gcd_abc);

DBMS_OUTPUT.PUT_LINE('LCM: ' || lcm);

END;

5) DECLARE

num NUMBER := &num;

sum NUMBER := 0;

i NUMBER;

BEGIN

FOR i IN 1..(num/2) LOOP

IF MOD(num, i) = 0 THEN

sum := sum + i;

END IF;
END LOOP;

IF sum = num THEN

DBMS_OUTPUT.PUT_LINE(num || ' is a Perfect Number.');

ELSE

DBMS_OUTPUT.PUT_LINE(num || ' is not a Perfect Number.');

END IF;

END;

6 ) DECLARE

str VARCHAR2(100) := '&word';

vowels NUMBER := 0;

consonants NUMBER := 0;

i NUMBER;

ch CHAR;

BEGIN

FOR i IN 1..LENGTH(str) LOOP

ch := UPPER(SUBSTR(str, i, 1));

IF ch IN ('A', 'E', 'I', 'O', 'U') THEN

vowels := vowels + 1;

ELSIF ch BETWEEN 'A' AND 'Z' THEN

consonants := consonants + 1;

END IF;

END LOOP;

DBMS_OUTPUT.PUT_LINE('Vowels: ' || vowels);

DBMS_OUTPUT.PUT_LINE('Consonants: ' || consonants);

END;

/
7)DECLARE

acc_no NUMBER := &acc_no;

balance NUMBER;

BEGIN

SELECT amount INTO balance FROM deposit WHERE account_no = acc_no;

IF balance < 6000 THEN

UPDATE deposit SET amount = amount + 3000 WHERE account_no = acc_no;

DBMS_OUTPUT.PUT_LINE('Balance updated successfully.');

ELSE

DBMS_OUTPUT.PUT_LINE('Balance is already 6000 or more.');

END IF;

COMMIT;

END;

8) DECLARE

emp_code NUMBER := &emp_code;

emp_name VARCHAR2(50);

count NUMBER;

BEGIN

SELECT COUNT(*) INTO count FROM emp WHERE emp_id = emp_code;

IF count > 0 THEN

DBMS_OUTPUT.PUT_LINE('Employee Code already exists.');

ELSE

INSERT INTO emp(emp_id, emp_name) VALUES (emp_code, '&emp_name');

DBMS_OUTPUT.PUT_LINE('Employee added successfully.');

END IF;
COMMIT;

END;

9) DECLARE

branch VARCHAR2(50) := '&branch';

row_count NUMBER;

BEGIN

DELETE FROM borrow WHERE branch_name = branch;

row_count := SQL%ROWCOUNT;

DBMS_OUTPUT.PUT_LINE(row_count || ' rows deleted.');

COMMIT;

END;

You might also like