0% found this document useful (0 votes)
35 views

Factorial of Number

1. This document contains 9 PL/SQL code snippets demonstrating various programming concepts like calculating factorials, determining the largest of two numbers, checking if a number is prime, swapping variables, determining a letter grade from a score, reversing a number, calculating the Fibonacci series, and finding the greatest of three numbers.

Uploaded by

Saloni Bhutada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Factorial of Number

1. This document contains 9 PL/SQL code snippets demonstrating various programming concepts like calculating factorials, determining the largest of two numbers, checking if a number is prime, swapping variables, determining a letter grade from a score, reversing a number, calculating the Fibonacci series, and finding the greatest of three numbers.

Uploaded by

Saloni Bhutada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Factorial of number
declare
n number;
fac number:=1;
i number;

begin
n:=&n;

for i in 1..n
loop
fac:=fac*i;
end loop;

dbms_output.put_line('factorial='||fac);
end;
/

2. Largest of two numbers

1. DECLARE
2. N NUMBER;
3. M NUMBER;
4. BEGIN
5. DBMS_OUTPUT.PUT_LINE('ENTER A NUMBER');
6. N:=&NUMBER;
7. DBMS_OUTPUT.PUT_LINE('ENTER A NUMBER');
8. M:=&NUMBER;
9. IF N<M THEN
10. DBMS_OUTPUT.PUT_LINE(''|| N ||' IS GREATER THAN '|| M ||'');
11. ELSE
12. DBMS_OUTPUT.PUT_LINE(''|| M ||' IS GREATER THAN '|| N ||'');
13. END IF;
14. END;
15. /
3. Prime Number

declare
n number;
i number;
flag number;

begin
i:=2;
flag:=1;
n:=&n;

for i in 2..n/2
loop
if mod(n,i)=0
then
flag:=0;
exit;
end if;
end loop;

if flag=1
then
dbms_output.put_line('prime');
else
dbms_output.put_line('not prime');
end if;
end;
/

4. Swapping with variable

declare
a number;
b number;
temp number;

begin
a:=5;
b:=10;

dbms_output.put_line('before swapping:');
dbms_output.put_line('a='||a||' b='||b);

temp:=a;
a:=b;
b:=temp;

dbms_output.put_line('after swapping:');
dbms_output.put_line('a='||a||' b='||b);

end;
/

5. Swapping without using third variable


declare
a number;
b number;

begin
a:=5;
b:=10;

dbms_output.put_line('before swapping:');
dbms_output.put_line('a='||a||' b='||b);

a:=a+b;
b:=a-b;
a:=a-b;

dbms_output.put_line('after swapping:');
dbms_output.put_line('a='||a||' b='||b);

end;
/

6. Determine the grade

DECLARE
2 variable_Score Number := 85;
3 variable_LetterGrade Char(1);
4 BEGIN
5 IF variable_Score >= 90 THEN
6 variable_LetterGrade := 'A';
7 ELSIF variable_Score >= 80 THEN
8 variable_LetterGrade := 'B';
9 ELSIF variable_Score >= 70 THEN
10 variable_LetterGrade := 'C';
11 ELSIF variable_Score >= 60 THEN
12 variable_LetterGrade := 'D';
13 ELSE
14 variable_LetterGrade := 'E';
15 END IF;
16 DBMS_OUTPUT.PUT_LINE('Your Letter Grade is: ' || variable_LetterGrade
);
17 END;
18 /

7. Reverse of a number
declare
n number;
i number;
rev number:=0;
r number;

begin
n:=&n;

while n>0
loop
r:=mod(n,10);
rev:=(rev*10)+r;
n:=trunc(n/10);
end loop;

dbms_output.put_line('reverse is '||rev);

end;
/

8. Fibonacci Series
declare
first number:=0;
second number:=1;
third number;
n number:=&n;
i number;

begin
dbms_output.put_line('Fibonacci series is:');
dbms_output.put_line(first);
dbms_output.put_line(second);

for i in 2..n
loop
third:=first+second;
first:=second;
second:=third;
dbms_output.put_line(third);
end loop;
end;

9. Greatest of three number

declare
a number;
b number;
c number;
begin
a:=&a;
b:=&b;
c:=&c;
if a=b and b=c and c=a then
dbms_output.put_line('ALL ARE EQUAL');
elsif a>b and a>c then
dbms_output.put_line('A IS GREATER');
elsif b>c then
dbms_output.put_line('B IS GREATER');
else
dbms_output.put_line('C IS GREATER');
end if;
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