Day 6

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

1)Write PL/SQL block to declare constant value PI and calculate area of circle.

->
SQL> DECLARE
2 PI NUMBER := 3.14;
3 nradius NUMBER;
4 narea NUMBER;
5 BEGIN
6 nradius := &enter_radius;
7 narea := PI * (nradius * nradius);
8 DBMS_OUTPUT.PUT_LINE('The area of the circle with radius ' || nradius || '
is: ' || narea);
9 END;
10 /
Enter value for enter_radius: 2
old 6: nradius := &enter_radius;
new 6: nradius := 2;
The area of the circle with radius 2 is: 12.56

PL/SQL procedure successfully completed.

2)Write PL/SQL block to find maximum number of two numbers.


->
DECLARE
num1 NUMBER;
num2 NUMBER;
max NUMBER;
BEGIN
num1 := &enter_num1;
num2 := &enter_num2;
IF num1 > num2 THEN
max := num1;
ELSE
max := num2;
END IF;
DBMS_OUTPUT.PUT_LINE('The maximum number between ' || num1 || ' and ' || num2
|| ' is: ' || max );
END;

3)Write a PL/SQL block that will accept number from the user , check if the users
balance is less than the minimum balance (5000), only deduct Rs. 100/- from the
balance. The process is fired on the account table.
->
SQL> DECLARE
2 accno NUMBER;
3 minbal NUMBER := 5000;
4 bal NUMBER;
5 BEGIN
6 accno := &enter_account_number;
7 SELECT bal INTO bal FROM Account WHERE ano = accno;
8 IF bal < minbal THEN
9 UPDATE Account SET bal = bal - 100 WHERE ano = accno;
10 COMMIT;
11 DBMS_OUTPUT.PUT_LINE('Rs. 100/- has been deducted from account number
' || accno);
12 ELSE
13 DBMS_OUTPUT.PUT_LINE('The balance for account number ' || accno || '
is greater than the minimum balance');

14 END IF;
15 END;
16 /
Enter value for enter_account_number: 103
old 6: accno := &enter_account_number;
new 6: accno := 103;
Rs. 100/- has been deducted from account number 103

PL/SQL procedure successfully completed.

SQL> select * from account;

ANO BAL BNAME


---------- ---------- --------------------------------------------------
101 1000 John
102 3500 Jane
103 2900 Bob

4)Write PL/SQL block to display grade from percentage.


Per>=80 A+ Per>=70 A Per>=60 B
Per>=50 C Per>=35 D Otherwise Fail
->
SQL> DECLARE
2 per NUMBER;
3 grade VARCHAR2(2);
4 BEGIN
5 per := &enter_percentage;
6 IF per >= 80 THEN
7 grade := 'A+';
8 ELSIF per >= 70 THEN
9 grade := 'A';
10 ELSIF per >= 60 THEN
11 grade := 'B';
12 ELSIF per >= 50 THEN
13 grade := 'C';
14 ELSIF per >= 35 THEN
15 grade := 'D';
16 ELSE
17 grade := 'F';
18 END IF;
19 DBMS_OUTPUT.PUT_LINE('The grade for ' || per || '% is: ' || grade);
20 END;
21 /
Enter value for enter_percentage: 46
old 5: per := &enter_percentage;
new 5: per := 46;
The grade for 46% is: D

PL/SQL procedure successfully completed.

5)Write a PL/SQL block to find enter number is pos, neg or zero.


->
SQL> DECLARE
2 num NUMBER;
3 result VARCHAR2(10);
4 BEGIN
5 num := &enter_num;
6 IF num > 0 THEN
7 result := 'positive';
8 ELSIF num < 0 THEN
9 result := 'negative';
10 ELSE
11 result := 'zero';
12 END IF;
13 DBMS_OUTPUT.PUT_LINE(num || ' is ' || result);
14 END;
15 /
Enter value for enter_num: -2
old 5: num := &enter_num;
new 5: num := -2;
-2 is negative

6)Implement PL/SQL Block which performs Menu driven


Mathematical operations as shown below:
Enter X and Y and perform the following operations.
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Power
6. EXIT
->
DECLARE
x NUMBER;
y NUMBER;
choice NUMBER;
result NUMBER;
BEGIN
x := &enter_x;
y := &enter_y;
DBMS_OUTPUT.PUT_LINE('Menu:');
DBMS_OUTPUT.PUT_LINE('1. Addition');
DBMS_OUTPUT.PUT_LINE('2. Subtraction');
DBMS_OUTPUT.PUT_LINE('3. Division');
DBMS_OUTPUT.PUT_LINE('4. Multiplication');
DBMS_OUTPUT.PUT_LINE('5. Power');
DBMS_OUTPUT.PUT_LINE('6. Exit');
DBMS_OUTPUT.PUT_LINE('Enter your choice: ');
choice := &enter_choice;
CASE choice
WHEN 1 THEN
result := x + y;
DBMS_OUTPUT.PUT_LINE(x || ' + ' || y || ' = ' || result);
WHEN 2 THEN
result := x - y;
DBMS_OUTPUT.PUT_LINE(x || ' - ' || y || ' = ' || result);
WHEN 3 THEN
result := x / y;
DBMS_OUTPUT.PUT_LINE(x || ' / ' || y || ' = ' || result);
WHEN 4 THEN
result := x * y;
DBMS_OUTPUT.PUT_LINE(x || ' * ' || y || ' = ' || result);
WHEN 5 THEN
result := POWER(x,y);
DBMS_OUTPUT.PUT_LINE(x || ' ^ ' || y || ' = ' || result);
WHEN 6 THEN
DBMS_OUTPUT.PUT_LINE('Exiting...');
EXIT;
ELSE
DBMS_OUTPUT.PUT_LINE('Invalid choice. Please try again.');
END CASE;
END;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy