0% found this document useful (0 votes)
40 views4 pages

Dbms Assignment 3: Chinmoy Dinda

The document contains 4 PL/SQL code blocks that: 1. Print the first 20 odd numbers using a loop. 2. Print a series based on user input: a) alternating sum series 1-2+3-4+5...N b) triangle of increasing numbers over multiple lines. 3. Check if a user-input number is a palindrome and prime. 4. Check if a user-input string is a palindrome by reversing and comparing.

Uploaded by

Chinmoy Dinda
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)
40 views4 pages

Dbms Assignment 3: Chinmoy Dinda

The document contains 4 PL/SQL code blocks that: 1. Print the first 20 odd numbers using a loop. 2. Print a series based on user input: a) alternating sum series 1-2+3-4+5...N b) triangle of increasing numbers over multiple lines. 3. Check if a user-input number is a palindrome and prime. 4. Check if a user-input string is a palindrome by reversing and comparing.

Uploaded by

Chinmoy Dinda
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
You are on page 1/ 4

DBMS

Assignment 3

Chinmoy Dinda
Roll No - 30701018050
Assignment 3
1. Create PL/SQL BLOCK to print 1st twenty odd numbers.
Ans:
Code:

DECLARE
N NUMBER(2);
I NUMBER(2);
BEGIN
N:=0;
I:=1;
LOOP
DBMS_OUTPUT.PUT_LINE(I);
I:=i+2;
N:=N+1;
EXIT WHEN N=20;
END LOOP;
END;
/
2. Create PL/SQL BLOCK to print the following series:

i) 1-2+3-4+5…N = Result
[Value of N should be taken from user.]
ii) 1
1 2
1 2 3
[User has to provide number of line as input.]
i) Ans:
Code:

DECLARE
N NUMBER(2);
I NUMBER(2);
S NUMBER(5);
BEGIN
N:=&N;
I:=1;
S:=0;
LOOP
IF MOD(I,2)=0 THEN
S:=S-I;
DBMS_OUTPUT.PUT('-'||I);
ELSE
S:=S+I;
DBMS_OUTPUT.PUT('+'||I);
END IF;
I:=i+1;
EXIT WHEN I>N;
END LOOP;
DBMS_OUTPUT.PUT_LINE('='||S);
END;
/
ii) Ans:
Code:

DECLARE
N NUMBER;
BEGIN
N:=&N;
FOR I IN 1..N
LOOP
FOR J IN 1..I
LOOP
DBMS_OUTPUT.PUT(J||' ');
END LOOP;
DBMS_OUTPUT.PUT_LINE('');
END LOOP;
END;
/
3. Create PL/SQL BLOCK to accept an integer value and check whether it is
palindrome & prime or not.
Ans:
Code:
DECLARE
N NUMBER;
Z NUMBER;
S NUMBER;
R NUMBER;
T NUMBER;
BEGIN
N:=&N;
S:=0;
Z:=N;
WHILE N>0
LOOP
R:=MOD(N,10);
S:=(S*10)+R;
N:=TRUNC(N/10);
END LOOP;
IF Z=S THEN
DBMS_OUTPUT.PUT_LINE(Z||' IS A PALINDROME');
ELSE
DBMS_OUTPUT.PUT_LINE(Z||' IS NOT A PALINDROME');
END IF;
S:=0;
FOR I IN 2..Z/2
LOOP
IF MOD(Z,I)=0 THEN
S:=1;
EXIT;
END IF;
END LOOP;
IF S=0 THEN
DBMS_OUTPUT.PUT_LINE(Z||' IS A PRIME NUMBER');
ELSE
DBMS_OUTPUT.PUT_LINE(Z||' IS NOT A PRIME NUMBER');
END IF;
END;
/
4. Create PL/SQL BLOCK to accept a string value and check whether it is palindrome
or not.
Ans:
Code:

DECLARE
s VARCHAR2(10);
l VARCHAR2(20);
t VARCHAR2(10);
BEGIN
s:='&s';
FOR i IN REVERSE 1..Length(s)
LOOP
l:= Substr(s, i, 1);
t:= t||''||l;
END LOOP;
IF t=s THEN
dbms_output.Put_line(t||' is palindrome');
ELSE
dbms_output.Put_line(t||' is not palindrome');
END IF;
END;
/

You might also like