-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExperiment09.txt
More file actions
145 lines (118 loc) · 2.04 KB
/
Copy pathExperiment09.txt
File metadata and controls
145 lines (118 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
1) To print hello world
SET SERVEROUTPUT ON
BEGIN
dbms_output.put_line('Hello World');
END;
/
2) To add two numbers
DECLARE
a number:= 8;
b number:= 2;
c number;
BEGIN
c := a + b;
dbms_output.put_line('Sum is: ' || c);
END;
/
3) To add two numbers read from the user
DECLARE
a number;
b number;
c number;
BEGIN
a := &a;
b := &b;
c := a + b;
dbms_output.put_line('Sum is: ' || c);
END;
/
4) To find the factorial of a number
DECLARE
n number;
f number;
BEGIN
n := &n;
f := 1;
WHILE n>0 LOOP
f := f*n;
n := n-1;
END LOOP;
dbms_output.put_line('Factorial is: ' ||f);
END;
/
5) To find the greatest of three numbers
DECLARE
a number := &a;
b number := &b;
c number := &c;
BEGIN
IF a>b AND a>c THEN
dbms_output.put_line('The greatest number is: ' || a);
ELSIF b>a AND b>c THEN
dbms_output.put_line('The greatest number is: ' || b);
ELSE
dbms_output.put_line('The greatest number is: ' || c);
END IF;
END;
/
6) Implement a simple calculator
DECLARE
a number := &a;
b number := &b;
s number;
BEGIN
s := &s;
IF s = 1 THEN
dbms_output.put_line('The sum is: ' || (a+b));
ELSIF s = 2 THEN
dbms_output.put_line('The difference is: ' || (a-b));
ELSIF s = 3 THEN
dbms_output.put_line('The product is: ' || (a*b));
ELSIF s = 4 THEN
dbms_output.put_line('The quotient is: ' || (a/b));
ELSE
dbms_output.put_line('Wrong Choice');
END IF;
END;
/
7) Program to generate fibonacci series
DECLARE
a number := 0;
b number := 1;
n number := &n;
i number;
t number :=0;
BEGIN
FOR i IN 1..n LOOP
dbms_output.put_line(a);
t := a;
a := b;
b := t +b;
END LOOP;
END;
/
8) Show divide by zero exception
DECLARE
a number := &a;
b number := &b;
s number;
BEGIN
s := a/b;
dbms_output.put_line('Quotient: '||s);
EXCEPTION
WHEN ZERO_DIVIDE THEN
dbms_output.put_line('Error: Division with zero');
END;
/
9) Show no data found exception
DECLARE
a number := 5;
b number := 4;
c number;
BEGIN
SELECT a INTO c FROM DUAL WHERE a<b;
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('Error: No data found');
END;
/