You are on page 1of 6

Q.

Check if a number is Palindrome in PL/SQL

declare
n number;
m number;
temp number:=0;
rem number;
begin
n:=5432112345;
m:=n;
while n>0
loop
rem:=mod(n,10);
temp:=(temp*10)+rem;
n:=trunc(n/10);
end loop;
if m = temp
then
dbms_output.put_line('true');
else
dbms_output.put_line('false');
end if;
end;

Q. Check whether a string is palindrome or not in PL/SQL?

DECLARE str varchar2(50):='&string';


counter int:=length(str);
BEGIN dbms_output.put_line(counter);
LOOP exit WHEN counter=0;
exit WHEN not(substr(str,counter,1)=substr(str,((length(str)+1)-counter),1));
counter:=counter-1;
END LOOP;
IF counter=0 THEN dbms_output.put_line(str||'is palindrom');
ELSE dbms_output.put_line(str||'is not palindrom');
END IF;
END;

Q. Sum Of Two Numbers in PL/SQL?

declare
x number(5);            
y number(5);           
z number(7);       
begin
x:=10;                
y:=20;                
z:=x+y;                
dbms_output.put_line('Sum is '||z);
end;     

Q. Factorial of a number in PL/SQL?

declare
 num number := 6;             
 fact number := 1;            
 temp number;        
 begin
 temp :=num;
 while( temp>0 )             
loop
fact := fact*temp;
temp := temp-1;
 end loop;
 dbms_output.put_line('factorial of '|| num || ' is ' || fact);
 end; 

Q. Sum of digits of a number in PL/ SQL?


DECLARE 
n  INTEGER; 
temp_sum INTEGER; 
r  INTEGER; 
BEGIN 
n := 123456; 
temp_sum := 0; 
WHILE n <> 0 LOOP 
r := MOD(n, 10); 
temp_sum := temp_sum + r; 
n := Trunc(n / 10); 
END LOOP; 
dbms_output.Put_line('sum of digits = ' || temp_sum); 
END; 

Q. Reverse a number in PL/SQL?


DECLARE
num NUMBER;
rev NUMBER;
BEGIN
num:=#
rev:=0;
WHILE num>0 LOOP
rev:=(rev*10) + mod(num,10);
num:=floor(num/10);
END LOOP;
DBMS_OUTPUT.PUT_LINE('Reverse of the number is: ' || rev);
END;
Q. GCD of two numbers in PL/SQL?

DECLARE 
num1 INTEGER; 
num2 INTEGER; 
t    INTEGER; 
BEGIN 
num1 := 8; 
num2 := 48; 
WHILE MOD(num2, num1) != 0 LOOP 
t := MOD(num2, num1); 
num2 := num1; 
num1 := t; 
END LOOP; 
dbms_output.Put_line('GCD of '||num1||' and '||num2 ||' is '||num1); 
END; 

Q. Check whether a given number is even or odd in PL/SQL?


DECLARE
n NUMBER := 1634;
r NUMBER;
BEGIN
r := MOD(n, 2);
IF r = 0 THEN
dbms_output.Put_line('Even');
ELSE
dbms_output.Put_line('Odd');
END IF;
END;

Q. Prime number in PL/SQL?

declare
n number;            
i number;           
temp number;       
begin
n := 13;                
i := 2;
temp := 1;
for i in 2..n/2
loop
if mod(n, i) = 0
then
temp := 0;
exit;
end if;
end loop;
if temp = 1
then
dbms_output.put_line('true');
else
dbms_output.put_line('false');
end if;
end;         

Q. Program for Fibonacci numbers in PL/SQL?

first number := 0;
second number := 1;
temp number;
n number := 5;
i number;
begin
dbms_output.put_line('Series:');
dbms_output.put_line(first);
dbms_output.put_line(second);
for i in 2..n
loop
temp:=first+second;
first := second;
second := temp;
dbms_output.put_line(temp);
end loop;
end;

Q. Reverse a string in PL/SQL?

DECLARE
str  VARCHAR(20) := 'skeegrofskeeg';
len  NUMBER;
str1 VARCHAR(20);
BEGIN
len := Length(str);
FOR i IN REVERSE 1.. len LOOP
str1 := str1|| Substr(str, i, 1);
END LOOP;
dbms_output.Put_line('Reverse of string is '|| str1);
END;

Q. Sum and average of three numbers in PL/SQL?

DECLARE 
a  NUMBER := 12; 
b  NUMBER := 14; 
c   NUMBER := 20; 
sumOf3 NUMBER; 
avgOf3 NUMBER; 
BEGIN 
sumOf3 := a + b + c; 
avgOf3 := sumOf3 / 3; 
dbms_output.Put_line('Sum = '||sumOf3); 
dbms_output.Put_line('Average = '||avgOf3); 
END; 

Q. Convert the given numbers into words in Pl/SQL?

DECLARE 
num         INTEGER; 
num_to_word VARCHAR2(100); 
str  VARCHAR2(100); 
len INTEGER; 
c     INTEGER; 
BEGIN 
num := 123456; 
len := Length(num); 
dbms_output.Put_line('Entered Number is: '||num); 
FOR i IN 1..len LOOP 
c := Substr(num, i, 1); 
SELECT Decode(c, 0, 'Zero ', 
                         1, 'One ', 
                         2, 'Two ', 
                         3, 'Three ', 
                         4, 'Four ', 
                         5, 'Five ', 
                         6, 'Six ', 
                         7, 'Seven ', 
                         8, 'Eight ', 
                         9, 'Nine ') 
INTO   str 
FROM   dual; 
num_to_word := num_to_word ||str; 
END LOOP; 
dbms_output.Put_line('Number to words: '||num_to_word); 
END; 

Q. Sum of digits equal to a given number in PL/SQL?

DECLARE 
n NUMBER; 
m NUMBER; 
s NUMBER := 0; 
BEGIN 
FOR i IN 1..999 LOOP 
n := i; 
WHILE n > 0 LOOP 
m := MOD(n, 10); 
s := s + m; 
n := Trunc(n / 10); 
END LOOP; 
IF s = 25 THEN 
dbms_output.Put_line(i||' '); 
END IF; 
s := 0; 
END LOOP; 
END; 

Q. Convert distance from km to meters and centimeters in PL/SQL?

DECLARE
km  NUMBER := 6.9;
met NUMBER := 0;
cem NUMBER := 0;
BEGIN
met := km * 1000;
cem := met * 100;
dbms_output.Put_line('6.9KM is equivalent to meters: ' ||met);
dbms_output.Put_line('6.9KM is equivalent to centimeters:' ||cem);
END;

  

  

                           

                 

You might also like