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

Manual For

SYSTEMS LAB
Submitted in partial fulfillment of
the requirements for the award of the degree of

Master of Technology
In
Computer Science & Engineering

Submitted by
NAME ROLL NUM

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

GANDHIJI INSTITUTE OF SCIENCE AND


TECHNOLOGY
(Approved By AICTE, Affiliated to JNTUK)
Gattu Bhimavaram, Jaggayyapet 521 178
Website: www.gistech.co.in

UNIX COMMANDS
1. CAL:
Sytax: $cal
Description:
Cal displays a simple calendar. If arguments are not specified,
the current month is displayed.
Eg:[cs40@localhost chandus]$ cal
cal
November 2008
Su Mo Tu We Th Fr Sa
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
2. DATE:
Syntax: $ date +<option>
%m displays numerical form of month of date
%n displays name of the month
%M displays the current minutes
%H displays current hours
%B displays full name of the month
DESCRIPTION
Display the current time in the given FORMAT, or set the system
date.
Eg:
1.[cs40@localhost chandus]$ date
date
Mon Nov 24 11:54:54 IST 2008
2.[cs40@localhost chandus]$ date +"%H"
11
3.MAN:
Syntax: $ man <command>
DESCRIPTION

Man formats and displays the on-line manual pages. Or Help of


commands.
4.BC:
Syntax: $ bc
DESCRIPTION
Bc is a language that supports arbitrary precision calculator
language.
5. WHO:
Syntax: $ who
DESCRIPTION
who - show who is logged on
Eg:
[cs40@localhost chandus]$ who
root :0
2008-11-15 10:02
root pts/1
2008-11-15 10:03
cs40 pts/2
2008-11-24 11:16 (172.168.102.19)
6. UNAME:
Syntax: $ uname
DESCRIPTION
Print certain system information.
Eg:
[cs40@localhost chandus]$ uname
Linux
7. LOGOUT:
Syntax: $ logout (or) Press <ctrl+d>
8. LOGIN:
DESCRIPTION
This commands are used to enter a user into the UNIX
environment by providing the user with a login and logout id with a
password provided to him.
Eg:login: cs40
Password: pass1234
Last login: Mon Nov 24 11:16:37 from 172.168.102.19
[cs40@localhost ~]$

DIRECTORY COMMANDS:
9. PWD:
Syntax: $ pwd
DESCRIPTION
Print the full filename of the current working directory.
Eg:
[cs40@localhost chandus]$ pwd
/home/cs40/chandus
10.MKDIR:
Syntax: $ mkdir <directory name>
DESCRIPTION
Create the DIRECTORY(ies), if they do not already exist.
Eg: [cs40@localhost chandus]$ mkdir chandu
A new directory with specified name is created.
11.LS:
Syntax: $ ls
DESCRIPTION
List information about the FILEs (the current directory by
default).
Eg:
[cs40@localhost chandus]$ ls
ls
Aop copyrename
Gist
matrixmul
noofwords time2
balu fileordir
kiran
mulfileintodir power
upper
bujji findthefileindir lineremove multable
revmatrix uprcase
bujji1 greatest
logon
nooflines
time1
12.CD:
Syntax: $ cd <directory name>
DESCRIPTION
cd command is used to create a new directory with specified
name. The user can keep track of all the work programs he did in the
directory he created.
Eg:
[cs40@localhost ~]$ cd chandus
[cs40@localhost chandus]$

13.CD ..:
Syntax: $ cd ..
DESCRIPTION
Cd .. command is used to exist out of the current displaying
directory into the home directory.
Eg:[cs40@localhost chandus]$ cd ..
[cs40@localhost ~]$
14.RMDIR:
Syntax: $ rmdir <directory name>
DESCRIPTION
Remove the DIRECTORY(ies), if they are empty.
Eg: [cs40@localhost ~]$ rm chandu
rm: cannot remove `chandu': Is a directory
[cs40@localhost ~]$ cd chandu
[cs40@localhost chandu]$ ls
whoislogon
[cs40@localhost chandu]$ rm whoislogon
[cs40@localhost chandu]$ cd ..
[cs40@localhost ~]$ rmdir chandu
[cs40@localhost ~]$
15.CP:
Syntax: $ cp <present file> <new file>
DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to
DIRECTORY.
Eg:
[cs40@localhost chandus]$ cp Gist Gist1
[cs40@localhost chandus]$

16.MV:
Syntax: $ mv
DESCRIPTION
Rename SOURCE to DEST, or move SOURCE(s) to
DIRECTORY.
Eg:
[cs40@localhost chandus]$ mv Gist1 Gist2
mv Gist1 Gist2
[cs40@localhost chandus]$
17.CAT:
Syntax: $ cat Gist2
DESCRIPTION
Concatenate FILE(s), or standard input, to standard output.
Eg:
[cs40@localhost chandus]$ cat> Gist2
this is Gist
It have so many colleges
The colleges contain so many branches
Press CTRL+X
[cs40@localhost chandus]$ cat Gist2
this is Gist
It have so many colleges
The colleges contain so many branches
18.COMM:
Syntax: $ comm. Bujji Gist
DESCRIPTION
Compare sorted files FILE1 and FILE2 line by line.
Eg:
[cs40@localhost chandus]$ comm bujji Gist
comm bujji Gist
this is gist
this is Gist
It have so many colleges
The colleges contain so many branches

19.DIFF:
Syntax: $ diff bujji Gist
DESCRIPTION
diff - find differences between two files
20.WC:
Syntax: $ wc <File name>
DESCRIPTION
Print newline, word, and byte counts for each FILE, and a total
line if
more than one FILE is specified.
Eg:
[cs40@localhost chandus]$ wc bujji
wc bujji
1 3 13 bujji
-l To give number of lines
-c To give number of characters
-w To give number of words
21.CHMOD:
Syntax: $ chmod <value> <filename>
DESCRIPTION
Chmod changes the
permissions of each given file according to mode,
Eg:
$ chmod 000 <file> - Useless file
22.UMASK:
Syntax: $ umask
DESCRIPTION
By using this command we can change the default file
permission for an existing file.
Eg: [cs40@localhost chandus]$ umask
Umask
0002

23.FIND:
Syntax: $ find <filename>
DESCRIPTION
find - search for files in a directory hierarchy
Eg: [cs40@localhost chandus]$ find bujji
find bujji
bujji
24.PS:
Syntax: $ ps
DESCRIPTION
ps displays information about a selection of the active processes.
Eg:
[cs40@localhost chandus]$ ps
PID TTY
TIME CMD
26589 pts/2 00:00:00 bash
26610 pts/2 00:00:00 man
26613 pts/2 00:00:00 sh
26614 pts/2 00:00:00 sh
26619 pts/2 00:00:00 less
26660 pts/2 00:00:00 man
26663 pts/2 00:00:00 sh
26664 pts/2 00:00:00 sh
25.DU:
Syntax: $ du
DESCRIPTION
Summarize disk usage of each FILE.
Eg:
[cs40@localhost chandus]$ du
40
./kiran
232

26.DF:
Syntax: $ df
DESCRIPTION
df displays the amount of disk space available on the file system
containing each file name argument.
Eg
[cs40@localhost chandus]$ df
Filesystem
1K-blocks Used Available Use% Mounted on
/dev/hda3
37767760 6965584 28852692 20% /
/dev/hda1
101086
9375 86492 10% /boot
tmpfs
122568
0 122568 0% /dev/shm
27.MOUNT:
Syntax: $mount
DESCRIPTION
All files accessible in a Unix system are arranged in one big tree,
the
file hierarchy, rooted at /. These files can be spread out over
several devices. The mount command serves to attach the file system
found
on some device to the big file tree.
Eg:
[cs40@localhost chandus]$ mount
mount
/dev/hda3 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/hda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
automount(pid1903) on /net type autofs
(rw,fd=4,pgrp=1903,minproto=2,maxproto=4)

28.ULIMIT:
Syntax: $ ulimit
DESCRIPTION
The ulimit call will get or set some limit for the current
processes.
Eg:
[cs40@localhost chandus]$ ulimit
ulimit
unlimited
29.FINGER:
Syntax: $ finger
DESCRIPTION
The finger displays information about the system users.
Eg:
[cs40@localhost chandus]$ finger
Login Name
Tty
Idle Login Time Office Office Phone
cs40
pts/2
Nov 25 09:41 (172.168.102.19)
root
root
*:0
Nov 15 10:02
root
root
pts/1 10d Nov 15 10:03
30.CUT:
Syntax: $ cut <file name>
DESCRIPTION
Print selected parts of lines from each FILE to standard output.
Eg:
[cs40@localhost chandus]$ cut -c 1-5 gist
this
It ha
The c
31.PAST:
Syntax : $ [-dls] <filename>
DESCRIPTION:
Merge lines of the files, write lines consisting of the
sequentially corresponding lines from each file, separated by tabs to
standard O/P.

32.SORT:
Syntax: $ sort [options] [output file] <file name.
Options:
-d
Alphanumeric sort
-f
folder uppercase into lowercase
-b
ignores loading blank spaces
-n
numeric sort
-c
check if files are solved
-r
reverse sort order
-kn sorts according to key val i.e. fields
-u
removes repeated lines
-tchar sorts according to delimiter
DESCRIPTION
Write sorted concatenation of all FILE.
Eg:
[cs40@localhost chandus]$ sort -d gist
It have so many colleges
The colleges contain so many branches
this is gist
33.GREP:
Syntax: $ grep <file name>
DESCRIPTION
Grep searches the named input FILEs
Eg: [cs40@localhost chandus]$ grep -1 gist1 gist2
this is GIST
It maintains values and standards.

SHELL PROGRAMS
1.TO FIND THE BIGGEST OF THREE NUMBERS.
Program
echo Enter 3 values
read a
read b
read c
if[$a gt $b]
then
if[$a gt $c]
then
echo $a is big
else
echo $c is big
fi
elif[$b gt $c]
then
echo $b is big
else
echo $c is big
fi
Output:
$ chmod +rwx big
$ ./big
Enter 3 values
4
9
3
9 is big.

2.TO FIND THE FACTORIAL OF THE GIVEN NUMBER.


Program
echo Enter a number
read n
fact=1
temp=$n
while[$n gt 0]
do
fact=expr $n\* $fact
n=expr $n -1
done
echo factorial of $temp is
echo $fact
Output:
$ chmod +rwx
$ ./fact
Enter a number
5
Factorial of 5 is 120.

3.TO FIND THE PRIME NUMBERS UP TO A GIVEN RANGE.


Program
echo enter range
read n
echo prime numbers till $n are
echo 1
i=2
while[$i le $n]
do
c=0
j=1
while[$j le $i]
do
if[expr $i \* $j eq 0]
then
c=expr $c +1
fi
j=expr $j+1
done
if[$c eq 2]
then
fcho $i
fi
i=expr $i+1
done

Output:
$chmod +rwx prime
$ ./prime
Enter range
10
Prime numbers till 10 are
1
2
3
5
7

4.TO FIND THE FIBONACCI NUMBERS UP TO A GIVEN RANGE.


Program
echo enter a value
read n
f1=0
f2=1
echo Fibonacci series is
echo $f2
i=1
while [$i lt $n]
do
f3=expr $f1 + $f2
fcho $f3
i=expr $i+1
i1=$f2
i2=$f3
done

Output:
$chnod +rwx fibo
$./fibo
Enter a value
6
Fibonacci series is
0
1
1
2
3
5

5.TO FIND WHETHER THE GIVEN NUMBER IS PALINDROME


OR NOT.
Program:
echo enter a number
read num
num1=$num
pal=0
while [$num gt 0]
do
dig=expr $num % 10
pal=expr $pal \*10 +$dig
num=expr $num / 10
done
if [$num1=$ pal]
then
echo $num1 is palindrome
else
echo $num1 is not a palindrome
fi

Output:
$ chmod +rwx pal
$ ./pal
Enter a number
121
121 is palindrome.
$./pal
Enter a number
203
203 is not a palindrome.

6.TO FIND A MULTIPLICATION TABLE.


Program
echo enter the number
read n
echo enter the range
read r
mul=1
i=1
while[$i le $r]
do
mul=expr $n \* $i
if[$i lt 10]
then
echo $n * $i = $mul
else
echo $i * $n= $mul
fi
i=expr $i+1
done

Output:
$ chmod +rwx mul
$ ./mul
Enter the number
9
Enter the range
10
9*1=9
9*2=18
9*3=27
9*4=36
9*5=45
9*6=54
9*7=63
9*8=72
9*9=81
9*10=90

7.TO DISPLAY THE TRANSPOSE OF A 2*2 MATRIX.


Program
echo enter number of rows
read m
echo enter number of columns
read n
size =expr $m \* $n
echo enter $size elements
j=1
while [$j le $size]
do
read b[$j]
j=expr $j+1
done
p=0
t=1
q=0
r=$n
echo the values after transpose
while [$p lt $n]
do
s=expr $r \* $q + $t
echo $[b[$s]]
q=expr $q +1
p=expr $p+1
done
l=1
while [$p lt $size]
do
s=expr $r \* $l
echo $[b[$s]]
l=expr $l+1
p=expr $p+1
done

Output:
$chmod +rwx tran
$./tran
Enter number of rows
2
Enter number of columns
2
Enter 4 elements
2
3
4
5
The values after transpose are
2
4
3
5

8.TO COUNT THE NUMBER OF LINES AND NUMBER OF WORDS


IN A GIVEN TEXT FILE.
Program
echo enter filename
read file
if [-f $file]
then
if [-s $file]
then
echo number of lines:
wc l $file
echo number of words:
wc w $file
else file is empty
fi
else
echo not a valid filename
fi

Output:
$ chmod +rwx countlw
$ ./countlw
Enter filename
Knl
Number of lines:
3 knl
Number of words:
12 knl.

9.WRITE A C PROGRAM TO ILLUSTRATE THE CAT UNIX


COMMAND.
Program
#include<stdio.h>
#include<fcntl.h>
main()
{
file *fp;
char c;
fp=fopen(fil1,r);
while((c=fgetc(fp)!=eof)
{
printf(%c,c);
}
fclose(fp);
}

Output:
$ cat fil1
Hello boys and girls
Have a good day
Thank u
$ cc cat.c
$ ./a.out
Hello boys and girls
Have a good day
Thank u.

10.WRITE A C PROGRAM THAT ILLUSTRATE THE CREATION


OF CHILD PROCESS USING FORK().
Program
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
int main(void)
{
pid_t pid;
pid =fork();
switch(pid)
{
case -1:printf(fork error);
exit(1);
case -0:printf(\n child process created.\n\n);
printf(the pid of child is %d \n,getpid());
printf(the parent pid of child is %d \n\n,getpid());
}
}
Output:
$ cc fork.c
$ ./a.out
Child process created
The Pid of child is 28690
The parent PID of child is 28689

DBMS PROGRAMS
1. Get The Description Of Emp Table.
SQL>desc emp;
Name
EMPNO
ENAME
JOB
MGR
HIREDATE
SAL
COMM
DEPTNO

Null?
NOT NULL

Type
NUMBER(4)
VARCHAR2(10)
VARCHAR2(9)
NUMBER(4)
DATE
NUMBER(7,2)
NUMBER(7,2)
NUMBER(2)

2.List All Employee Details.


SQL>select * from emp;
EMPNO ENAME
7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
7902 FORD
7934 MILLER

JOB
CLERK
SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER
MANAGER
ANALYST
PRESIDENT
SALESMAN
CLERK
CLERK
ANALYST
CLERK

MGR HIREDATE
7902 17-DEC-80
7698 20-FEB-81
7698 22-FEB-81
7839 02-APR-81
7698 28-SEP-81
7839 01-MAY-81
7839 09-JUN-81
7566 19-APR-87
17-NOV-81
7698 08-SEP-81
7788 23-MAY-87
7698 03-DEC-81
7566 03-DEC-81
7782 23-JAN-82

14 rows selected.

2. List All Jobs Available In Employee Table.

SAL COMM DEPTNO


800
20
1600
300
30
1250
500
30
2975
20
1250
1400
30
2850
30
2450
10
3000
20
5000
10
1500
0
30
1100
20
950
30
3000
20
1300
10

SQL>select distinct job from emp;


JOB
ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN

5. List All Employees Who Belongs To The Department 10 Or 20.


INPUT SQL>select ename from emp where deptno in (10,20);
ENAME
SMITH
JONES
CLARK
SCOTT
KING
ADAMS
FORD
MILLER

8 rows selected.

6. List All Employee Names , Salary And 15% Rise In Salary.


INPUT SQL>select ename , sal , sal+0.15* sal from emp;
ENAME
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER

SAL

SAL+0.15*SAL
800
1600
1250
2975
1250
2850
2450
3000
5000
1500

920
1840
1437.5
3421.25
1437.5
3277.5
2817.5
3450
5750
1725

ADAMS
JAMES
FORD
MILLER

1100
950
3000
1300

1265
1092.5
3450
1495

14 rows selected.

7. List Minimum , Maximum , Average Salaries Of Employee.


SQL>select min(sal),max(sal),avg(sal) from emp;

MIN(SAL)

MAX(SAL)
800

5000

AVG(SAL)
2073.21429

8. Find How Many Job Titles Are Available In Employee Table.


SQL>select count (distinct job) from emp;
COUNT(DISTINCTJOB)
5

9. What is the difference between maximum and minimum salaries of


employees in the organization?
INPUT SQL>select max(sal)-min(sal) from emp;

MAX(SAL)-MIN(SAL)
4200

10.List all employee names and their salaries, whose salary lies between
1500/- and 3500/- both inclusive.
INPUT
SQL>select ename from emp where sal between 1500 and 3500;
ENAME
ALLEN

JONES
BLAKE
CLARK
SCOTT
TURNER
FORD

7 rows selected.

11. Find how much amount the company is spending towards salaries.
INPUT SQL>select sum (sal) from emp;
SUM(SAL)
29025

12. Display name of the dept. with deptno 20.


INPUT SQL>select ename from emp where deptno = 20;
ENAME
SMITH
JONES
SCOTT
ADAMS
FORD

13. List ename whose commission is NULL.


INPUT SQL>select ename from emp where comm is null;
ENAME
SMITH
JONES
BLAKE
CLARK
SCOTT
KING
ADAMS
JAMES

FORD
MILLER

10 rows selected.

14. Find no.of dept in employee table.


INPUT SQL>select count (distinct ename) from emp;
COUNT(DISTINCTENAME)
14

15. List ename whose manager is not NULL.


INPUT SQL>select ename from emp where mgr is not null;
ENAME
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
TURNER
ADAMS
JAMES
FORD
MILLER

13 rows selected.

16. Display total salary spent for each job category.


INPUT SQL>select job,sum (sal) from emp group by job;
JOB

SUM(SAL)

ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN

17. Display lowest paid employee details under each manager.

6000
4150
8275
5000
5600

INPUT SQL>select ename, sal from emp where sal in (select min(sal) from
emp group by mgr);
ENAME

SAL

SMITH
JAMES
ADAMS
MILLER
CLARK
SCOTT
FORD
KING

800
950
1100
1300
2450
3000
3000
5000

8 rows selected.

18. Display number of employees working in each department and their


department name.
INPUT SQL> select dname, count (ename) from emp, dept where
emp.deptno=dept.deptno group by dname;
DNAME

COUNT(ENAME)

ACCOUNTING
RESEARCH
SALES

3
5
6

CREATING TABLE FOR VARIOUS RELATIONS(IN SQL).


1.SQL>create table students(no number(5),name varchar2(20),rno
varchar2(20) primary key);
Table created.
2.SQL>desc students;
Name
NO
NAME

Null?

Type
NUMBER(5)
VARCHAR2(20)

RNO

NOT NULL

VARCHAR2(20)

3. SQL>desc marks;
Name

Null?

RNO
ADS
MFCS
CO
DBMS
OS
JAVA
TOTAL

Type
VARCHAR2(20)
NUMBER(3)
NUMBER(3)
NUMBER(3)
NUMBER(3)
NUMBER(3)
NUMBER(3)
NUMBER(4)

4.SQL> select * from students;


NO

NAME
1 kalyan
2 sunitha
3 padma
4 siva
5 varma
6 divya
7 khan
8 rajesh
8 prasad
9 sandhya
10 supriya

RNO
mtech14
mtech12
mtech05
mtech01
mtech02
mtech03
mtech04
mtech06
mtech07
mtech13
mtech15

11 rows selected.

5.SQL> update students set name='varma' where no=5;


1 row updated.
6.SQL> delete from students where name='siva';
1 row deleted.
7.SQL> select * from students;
NO

NAME
1 kalyan

RNO
mtech14

2 sunitha
3 padma
5 varma
6 divya
7 khan
8 rajesh
8 prasad
9 sandhya
10 supriya

mtech12
mtech05
mtech02
mtech03
mtech04
mtech06
mtech07
mtech13
mtech15

10 rows selected.

IMPLEMENTING SINGLE ROW FUNCTIONS


Single row functions:
1.Sqrt();
SQL>select sqrt(16) from dual;
SQRT(16)
4

2.power();
SQL>select power(3,2) from dual;
POWER(3,2)
9

3.Round();
SQL>select round(34.56732,2) from dual;

ROUND(34.56732,2)
34.57

4.cos();
SQL>Select cos(0) from dual;

COS(0)
1

5.floor();
SQL>select floor(34.7) from dual;
FLOOR(34.7)
34

6.ceil();
SQL>select ceil(34.3) from dual;
CEIL(34.3)
35

7.abs();
SQL>select abs(-7) from dual;
ABS(-7)
7

WRITING PL/SQL BLOCK FOR INSERTION INTO A


TABLE.
1. TO WRITE A PL/SQL BLOCK TO CHECK WHETHER A
GIVEN NUMBER IS EVEN OR ODD.
PROGRAM:
DECLARE
NUM NUMBER(5);
REM NUMBER(5);

BEGIN
NUM:=&NUM;
REM:=MOD(NUM,2);
IF (REM=0)THEN
DBMS_OUTPUT.PUT_LINE(NUM||' IS EVEN');
ELSE
DBMS_OUTPUT.PUT_LINE(NUM||' IS ODD');
END IF;
END;

OUTPUT:
ENTER VALUE FOR NUM: 3
OLD 6: NUM:=&NUM;
NEW 6: NUM:=3;
NUMBER 3 IS ODD
PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.

2.TO WRITE A PL/SQL PROGRAM TO IMPLEMENT THE SCOPE


OF THE VARIABLE.
PROGRAM:
DECLARE
X REAL:=205;
BEGIN
DBMS_OUTPUT.PUT_LINE('THE VALUE OF OUTER X:='||X);
DECLARE
X REAL:=405;
BEGIN
DBMS_OUTPUT.PUT_LINE('THE VALUE OF INNER X:='||X);
END;
DBMS_OUTPUT.PUT_LINE('THE VALUE OF OUTER X:='||X);
END;

OUTPUT:
THE VALUE OF OUTER X:=205
THE VALUE OF INNER X:=405
THE VALUE OF OUTER X:=205

3.WRITE PL/SQL BLOCK TO CHECK WHETHER GIVEN


NUMBER IS ARMSTRONG OR NOT.
PROGRAM:

DECLARE
NUM NUMBER(5);
REM NUMBER(5);
S NUMBER(5):=0;
NUM1 NUMBER(5);
BEGIN
NUM:=&NUM;
NUM1:=NUM;
WHILE(NUM>0)
LOOP
REM:=MOD(NUM,10);
S:=S+POWER(REM,3);
NUM:=TRUNC(NUM/10);
END LOOP;
IF(S=NUM1)THEN
DBMS_OUTPUT.PUT_LINE(NUM1||'IS ARMSTRONG NUMBER');
ELSE
DBMS_OUTPUT.PUT_LINE(NUM1||'IS NOT ARMSTRONG NUMBER');
END IF;
END;

OUTPUT:
ENTER VALUE FOR NUM: 153
OLD 7: NUM:=&NUM;
NEW 7: NUM:=153;
153 IS ARMSTRONG NUMBER
PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.

SQL> /
ENTER VALUE FOR NUM: 123
OLD 7: NUM:=&NUM;
NEW 7: NUM:=123;
123 IS NOT ARMSTRONG NUMBER
PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.

4.TO WRITE A PL/SQL BLOCK TO CHECK THE GIVEN STRING


IS PALINDROME OR NOT.

PROGRAM:
DECLARE
NAME1 VARCHAR2(20);
NAME2 VARCHAR2(20);
L NUMBER(5);
BEGIN
NAME1:='&NAME1';
L:=LENGTH(NAME1);
WHILE L>0 LOOP
NAME2:=NAME2||SUBSTR(NAME1,L,1);
L:=L-1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('REVERSE OF STRING IS:'||NAME2);
IF(NAME1=NAME2) THEN
DBMS_OUTPUT.PUT_LINE(NAME1||'IS PALINDROME');
ELSE
DBMS_OUTPUT.PUT_LINE(NAME1||'IS NOT PALINDROME');
END IF;
END;

OUTPUT:
ENTER VALUE FOR NAME1: MADAM
OLD 6: NAME1:='&NAME1';
NEW 6: NAME1:='MADAM';
REVERSE OF STRING IS:MADAM
MADAM IS PALINDROME

5.TO WRITE A PL/SQL BLOCK TO FIND SUM OF DIGITS OF A


GIVEN NUMBER.
PROGRAM:
DECLARE
NUM NUMBER(5);
REM NUMBER(5);
SM NUMBER(5):=0;
NUM1 NUMBER(5);
BEGIN
NUM:=&NUM;
NUM1:=NUM;

WHILE(NUM>0)
LOOP
REM:=MOD(NUM,10);
SM:=SM+REM;
NUM:=TRUNC(NUM/10);
END LOOP;
DBMS_OUTPUT.PUT_LINE(NUM1||' SUM OF DIGITS OF A GIVEN
NUMBERIS: '||SM);
END;

OUTPUT:
ENTER VALUE FOR NUM: 123
OLD 7: NUM:=&NUM;
NEW 7: NUM:=123;
SUM OF DIGITS OF 123 IS: 6
PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.

6.TO WRITE A PL/SQL BLOCK TO GENERATE FIBONACCI


SERIES
PROGRAM:
DECLARE
NUM NUMBER(5);
F1 NUMBER(5):=0;
F2 NUMBER(5):=1;
F3 NUMBER(5);
I NUMBER(5):=3;
BEGIN
NUM:=&NUM;

DBMS_OUTPUT.PUT_LINE('THE FIBONACCI SERIES IS:');


DBMS_OUTPUT.PUT_LINE(F1);
DBMS_OUTPUT.PUT_LINE(F2);
WHILE(I<=NUM)
LOOP
F3:=F1+F2;
DBMS_OUTPUT.PUT_LINE(F3);
F1:=F2;
F2:=F3;
I:=I+1;
END LOOP;
END;

OUTPUT:
ENTER VALUE FOR NUM: 10
OLD 8: NUM:=&NUM;
NEW 8: NUM:=10;
THE FIBONACCI SERIES IS:
0
1
1
2
3
5
8
13
21
34
PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.

7. WRITE PL/SQL PROGRAM TO FIND FACTORIAL VALUE


USING REVERSE FOR LOOP
PROGRAM:
DECLARE
V_NUMFACT NUMBER:=&GIVENNUMBER;
V_FACTORIAL NUMBER:=1;
BEGIN
FOR INDEXI IN REVERSE 1..V_NUMFACT LOOP

V_FACTORIAL:=V_FACTORIAL*INDEXI;
DBMS_OUTPUT.PUT_LINE('THE INDEXI NUMBER IS '||INDEXI);
END LOOP;
DBMS_OUTPUT.PUT_LINE('THE FACTORIAL OF '||V_NUMFACT||'IS :'||
V_FACTORIAL);
END;

OUTPUT:
GIVEN NUMBER 8
OLD 2: V_NUMFACT NUMBER:=&GIVENNUMBER;
NEW 2: V_NUMFACT NUMBER:=9;
THE INDEXI NUMBER IS 8
THE INDEXI NUMBER IS7
THE INDEXI NUMBER IS6
THE INDEXI NUMBER IS5
THE INDEXI NUMBER IS4
THE INDEXI NUMBER IS3
THE INDEXI NUMBER IS2
THE INDEXI NUMBER IS1
THE FACTORIAL OF 9 IS 51840
PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.

8.TO WRITE A TRIGGER TO ENSURE THAT DEPT TABLE


DOESNT CONTAIN DUPLICATE OF NULL VALUES IN DEPTNO
COLUMN.
PROGRAM:
CREATE OR REPLACE TRIGGER TRIG1 BEFORE INSERT ON DEPT FOR
EACH ROW
DECLARE
A NUMBER;
BEGIN
IF(:NEW.DEPTNO IS NULL)THEN

RAISE_APPLICATION_ERROR(-20001,'ERROR::DEPTNO CANNOT BE
NULL');
ELSE
SELECT COUNT(*) INTO A FROM DEPT WHERE DEPTNO=:NEW.DEPTNO;
IF(A=1) THEN
RAISE_APPLICATION_ERROR(-20002,'ERROR:: CANNOT HAVE
DUPLICATE DEPTNO');
END IF;
END IF;
END;

OUTPUT:
TRIGGER CREATED.
SQL>SELECT * FROM DEPT;
DEPTNO

DNAME
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS

LOC
NEW YORK
DALLAS
CHICAGO
BOSTON

SQL> INSERT INTO DEPT VALUES(&DEPTNP,'&DNAME','&LOC');


OLD 1: INSERT INTO DEPT VALUES(&DEPTNP,'&DNAME','&LOC')
NEW 1: INSERT INTO DEPT VALUES(50,'MARKETING','HYDERABAD')
1 ROW CREATED.

SQL>SELECT * FROM DEPT;


DEPTNO

DNAME
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS
50 MARKETING

LOC
NEW YORK
DALLAS
CHICAGO
BOSTON
HYDERABAD

9.WRITE A PL/SQL TRIGGER TO TRANSATIONAL AUDITING ON


SALARY COLUMN IN EMP TABLE
PROGRAM:
CREATE OR REPLACE TRIGGER CHKSALARY
BEFORE UPDATE OF SAL ON EMP
FOR EACH ROW
WHEN(NEW.SAL<OLD.SAL)
BEGIN
RAISE_APPLICATION_ERROR(-20500,'SALARY CANNOT BE
DECREASED..');
END;

OUTPUT:
TRIGGER CREATED.
UPDATE EMP54 SET SAL=7000 WHERE SAL=8000;
'SALARY CANNOT BE DECREASED

You might also like