73
KRS COLLEGE OF ENGINEERING 185253 COMPUTER PRACTICE LABORATORY – II L T P C 0 1 2 2 LIST OF EXPERIMENTS 1. UNIX COMMANDS 15 Study of Unix OS - Basic Shell Commands - Unix Editor 2. SHELL PROGRAMMING 15 Simple Shell program - Conditional Statements - Testing and Loops 3. C PROGRAMMING ON UNIX 15 Dynamic Storage Allocation-Pointers-Functions-File Handling TOTAL : 45 PERIODS HARDWARE / SOFTWARE REQUIREMENTS FOR A BATCH OF 30 STUDENTS Hardware 1 1 UNIX Clone Server 2 33 Nodes (thin client or PCs) 3 Printer – 3 Nos. Software 4 OS – UNIX Clone (33 user license or License free Linux) 5 Compiler - C CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE 1

COMPUTER PRACTICE LABORATORY – II.pdf

Embed Size (px)

Citation preview

Page 1: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

185253 COMPUTER PRACTICE LABORATORY – II L T P C

0 1 2 2

LIST OF EXPERIMENTS

1. UNIX COMMANDS 15

Study of Unix OS - Basic Shell Commands - Unix Editor 2. SHELL PROGRAMMING

15 Simple Shell program - Conditional Statements - Testing and Loops 3. C PROGRAMMING ON UNIX

15 Dynamic Storage Allocation-Pointers-Functions-File Handling

TOTAL : 45 PERIODS

HARDWARE / SOFTWARE REQUIREMENTS FOR A BATCH OF 30 STUDENTS

Hardware

1 1 UNIX Clone Server 2 33 Nodes (thin client or PCs) 3 Printer – 3 Nos.

Software

4 OS – UNIX Clone (33 user license or License free Linux) 5 Compiler - C

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

1

Page 2: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

INDEX

EX. NO

DATE EXPERIMENT NAMEPAGE

NOSIGNATURE

I UNIX COMMANDS

II.SHELL PROGRAMMING

1.a BIGGEST OF TWO NUMBERS

1.bMULTIPLICATION TABLE

1.cSWAP PROGRAM

2.a SUM OF THE NUMBERS

2.bODD OR EVEN

3REVERSE OF A NUMBER

4SIMPLE CALCULATOR

5FACTORIAL OF A NUMBER

6FIBONACCI SERIES

7ARMSTRONG NUMBER

8 PRIME NUMBER

9PALINDROME

10 PAYROLL GENERATION

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

2

Page 3: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX. NO DATE EXPERIMENT NAME

PAGE NO

SIGNATURE

III.C PROGRAMMING ON UNIX

FUNCTIONS

1LINEAR SEARCH USING FUNCTION

2BINARY SEARCH USING FUNCTION

3FACTORIAL USING RECURSION

4FIBONACCI SERIES USING

RECURSIVE FUNCTION

POINTERS

5PROGRAM TO SORT THE DIGITS OF

AN INTEGER USING POINTER

6

PROGRAM TO ACCESS ELEMENTS OF AN ARRAY & COUNT THE NO OF OCCURANCES OF THE GIVEN NO IN

THE ARRAY USING POINTERS

FILES

7 COPY THE CONTENTS OF ONE FILE TO ANOTHER

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

3

Page 4: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

INTRODUCTION TO UNIX

STUDY OF UNIX OS

AN OVERVIEW OF THE UNIX OPERATING SYSTEM

The UNIX* operating system was designed to let a number of programmers access the computer at the same time and share its resources.

The operating system coordinates the use of the computer's resources, allowing one person, for example, to run a spell check program while another creates a document, lets another edit a document while another creates graphics, and lets another user format a document -- all at the same time, with each user oblivious to the activities of the others.

The operating system controls all of the commands from all of the keyboards and all of the data being generated, and permits each user to believe he or she is the only person working on the computer.

This real-time sharing of resources make UNIX one of the most powerful operating systems ever.

Although UNIX was developed by programmers for programmers, it provides an environment so powerful and flexible that it is found in businesses, sciences, academia, and industry. Many telecommunications switches and transmission systems also are controlled by administration and maintenance systems based on UNIX.

While initially designed for medium-sized minicomputers, the operating system was soon moved to larger, more powerful mainframe computers. As personal computers grew in popularity, versions of UNIX found their way into these boxes, and a number of companies produce UNIX-based machines for the scientific and programming communities.

UNIX HISTORY:

UNIX HISTORY, VERSIONS, AND SHELLSHISTORY OF UNIX• Developed in 1969 at AT&T Bell Laboratories• Became widely available in 1975• Distributed at low cost to universities--graduating studentsbrought UNIX into commercial world• Designed to run on a wide range of systemsVersions of UNIX• Berkeley UNIX (BSD)

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

4

Page 5: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

• AT&T UNIX (System V)

SHELLS (USER INTERFACES)• Serve as a command language interpreter, providing a customizableuser interface and programming language. Twocommon shells are:

C shell Commonly used by general usersBourne shell Commonly used for system administration

The C shell is covered in this course, although the same conceptsgenerally apply to other shells as well.

THE UNIQUENESS OF UNIX

The features that made UNIX a hit from the start are:

• Multitasking capability • Multiuser capability • Portability • UNIX programs • Library of application software

MULTITASKING

Many computers do just one thing at a time, as anyone who uses a PC or laptop can attest. Try logging onto your company's network while opening your browser while opening a word processing program. Chances are the processor will freeze for a few seconds while it sorts out the multiple instructions.

MULTIUSER

The same design that permits multitasking permits multiple users to use the computer. The computer can take the commands of a number of users -- determined by the design of the computer -- to run programs, access files, and print documents at the same time.

SYSTEM PORTABILITY

A major contribution of the UNIX system was its portability, permitting it to move from one brand of computer to another with a minimum of code changes. At a time when different computer lines of the same vendor didn't talk to each other -- yet alone machines of multiple vendors -- that meant a great savings in both hardware and software upgrades.

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

5

Page 6: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

UNIX TOOLS

UNIX comes with hundreds of programs that can divided into two classes:

• Integral utilities that are absolutely necessary for the operation of the computer, such as the command interpreter, and

• Tools that aren't necessary for the operation of UNIX but provide the user with additional capabilities, such as typesetting capabilities and e-mail.

THE THREE LEVELS OF THE UNIX SYSTEM: KERNEL, SHELL, AND TOOLS AND APPLICATIONS.

THE KERNEL

The heart of the operating system, the kernel controls the hardware and turns part of the system on and off at the programer's command. If you ask the computer to list (ls) all the files in a directory, the kernel tells the computer to read all the files in that directory from the disk and display them on your screen.

THE SHELL

There are several types of shell, most notably the command driven Bourne Shell and the C Shell (no pun intended), and menu-driven shells that make it easier for beginners to use. Whatever shell is used, its purpose remains the same -- to act as an interpreter between the user and the computer.

The shell also provides the functionality of "pipes," whereby a number of commands can be linked together by a user, permitting the output of one program to become the input to another program.

TOOLS AND APPLICATIONS

There are hundreds of tools available to UNIX users, although some have been written by third party vendors for specific applications. Typically, tools are grouped into categories for certain functions, such as word processing, business applications, or programming.

ADVANTAGES OF UNIX

Unix is avery stable operating system

Unix supports the multi user feature

Unix is a multitasking operating system

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

6

Page 7: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

UNIX COMMANDS

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

7

Page 8: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

AIM: To execute the following UNIX commands.

DATE

SYNTAX : $ date [+ format specifiers] %a =week day %h =month%d=the day of month %y=last two digit of the year

USES : The date command is used to display the current date and time

OUTPUT :

1.CALANDER

SYNTAX : cal

USES : The cal command is used to display the calendar for the specified year or month

OUTPUT :

SYNTAX : cal 2005

USES : It displays the mentioned year calendar OUTPUT : It will displays the given year calendar

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

8

Page 9: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

2.ECHO

SYNTAX : echo “UNIX”

USES : Used to displaying the given text OUTPUT : UNIX

TYPING A CONTENT MORE THAN ONE LINE

SYNTAX : echo “the command >line exceeds >a single line”

OUTPUT : This command Line exceeds A single line

USES : Used to display the multiple line as we entered while

executing the Command

SYNTAX : echo This command\ >line exceeds\ >a single line

OUTPUT : This command line exceeds a single line

USES : The backslash character at the end of each line(followed by pressing

the ENTER KEY)informs the shell that the user wants to continue the command on the next line.it is used to display the multiple multiplelines

into single line

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

9

Page 10: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

3.BC

SYNTAX : bc

USES : Used to perform simple mathematical calculations. by default this

Command accepts decimal numbers and also perform mathematical

calculations on octal and hexadecimal numbers.

OUTPUT :

4.WHO

SYNTAX :who

USES : Used to display the login details for all user using the UNIX

system.

OUTPUT : Student pts/1 May 4 15:50(192.168.2.3)Student pts/1 1 May 4 10 :47(192.168.2.19)root :0 May 4 14:52(consale)

5.WHO AM I

SYNTAX : who am i

USES : Used to displays th eloign of the current users of the

system,who invokes the command.

OUTPUT : student pts/1 May 4 15:50(192.168.2.3)

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

10

Page 11: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

6.TTY

SYNTAX : tty(tele type)

USES : It gives the file name of the terminal that you are using.tell you

the device name and the name of the that you currently

OUTPUT : /dev/pts/1

7.MAN

SYNTAX : man any command

USES : It offers online help facilities and it gives all details for the particular

Commands that which user can type file and the director commands

DIRECTORY COMMANDS

8.MAKE DIRECTORY

SYNTAX : mkdir directoy name

USES : Used to create a new directory

OUTPUT : mkdir srpc

9.CHANGE DIRECTORY

SYNTAX : cd directory name USES : Used to change from one working directory to another directory Specified by its argument.

OUTPUT : cd srpc

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

11

Page 12: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

10.REMOVE DIRECTORY

SYNTAX : rmdir

USES : Used to remove the directory

OUTPUT : rmdir srpc

11.PWD

SYNTAX : pwd

USES : Used to display the path that we are working.

OUTPUT : /home/student/vs

FILE & REDIRECTION COMMANDS

12.CAT

SYNTAX : cat>file name

USES : Used to create a new file & insert a content into that file.press CTRL+D to exit from that file.

OUTPUT :

Cat>sampleFlower Jasmine RosePress CTRL + D

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

12

Page 13: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

13.SYNTAX : cat file name

USES : Used to display the content of that file

OUTPUT :

Cat sampleFlower Jasmine Rose

14.SYNTAX : cat<filename1>filename2

USES : It will copy the content of filename1 to filename2,the content of filename to is creased

OUTPUT :

ABCD

Cat<sample>sample1Flower jasminerose

cat sample1flower jasminerose

15.SYNTAX : cat< filename1 filename2>filename3

USES :The output of filename1 andfilename2 are concatenated and stored in

The filename3,and it is not displayed in the terminal.here filename3

Is already exits.to view the contents of the file use cat command.

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

13

Page 14: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

OUTPUT :

Cat sample1 cat sample2Flower abJasmine bcRose d EfCat sample3Flower Jasmine Rose Ab Bc D Ef

16.SYNTAX : any command>filename

USES : The output of any commands is stored in the file (filename)and it

is not Displayed in the command prompt.to view the output use cat

command use command

OUTPUT :

Who>sample4

Cat sample4Student pts/1 may 4 15:50(192.168.2.3)Student pts/1 1 may 4 10:47(192.168.2.19) Root 0 may 4 14:52(console)

APPEND THE FILE CONTENTS

17.SYNTAX : cat file name1>>filename2

USES : Used to append the contents of file name1 to file name2

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

14

Page 15: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

OUTPUT :

Cat sample1 cat sample2Flower abJasmine bcRose e Ef Cat sample1>>sample2

Cat sample3AbBcDEfFlowerJasmine Rose

18.SYNTAX : cat file name1 filename2>>filename3 USES : The output of filename1and filename2 are appened in the filename3

and it is not displsayed in the terminal.here filename3 is to view the

content of the file use cat command.

RENAMING THE FILE

19.SYNTAX : mv source file name destination filename

USES :Used to rename the file

LIST OUT COMMANDS

20.SYNTAX : Is

USES : Used to list out all files ina directory

21.SYNTAX : ls –a

USES : Used to list out all files including hidden files(files that begin with(…..)

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

15

Page 16: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

22.SYNTAX : ls –i

USES : Used to list out all files with its i-node number in the first

23.SYNTAX : Is –r

USES : Used to list out all files in reverse alphabetical order

24.SYNTAX : Is –t

USES : Used to list out all files in the order of their lastmodificaton time

25.SYNTAX : Is –u

USES :Used to list out all files in the order of their last access time

26.SYNTAX : Is –i

USES : Used to list out all files in long format(one entry per line giving its mode

number of links,owner,group,size in bytes,the timeeach filr was last modified

27.SYNTAX : Is –it

USES : Used to list out all files in long format with their last modification time

FILTER COMMANDS

28.SYNTAX : head-n filename

USES : Used to display the Top ‘n’ lines of the file

29.SYNTAX : tail –n filename

USES : Used to display the button ‘n’lines of the file

30.SYNTAX : more filename

USES :To see the content of the file name on the screen one page at a time

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

16

Page 17: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

WORD COUNT COMMAND

31.SYNTAX : we filename

USES : It will display the no of lines no of words and used to display no

of characters.

32.SYNTAX : we –Ifilename

USES : Used to display no of lines in a filename

33.SYNTAX : we –w filename

USES : Used to display no of words in a filename

34.SYNTAX : we –c filename

USES : Used to display no of character in a filename

COMMON COMMAND

35.SYNTAX : comm..filename1 filename2

USES : This command each line of first file its corresponding line in the second

file.output contains 3 columns.first column contains lines unique to

filename1.second column contains lines unique to filename2.third

columncontains lines common to both

EX:

$cat nl $cat nithiSrpec srpecRam baluBabu kumar Jeganprabu

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

17

Page 18: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

OUTPUT :

$comm nl nithi Srpec ram babujeganprabuprabu balu vimal

OPTIONS

36.SYNTAX : comm. -1 filename

USES : It does not include the first column in output

37.SYNTAX : comm. -2 filename

USES : It does not include the third column in output

COMPARE COMMAND

38.SYNTAX : cmp filename1 filename2

USES : Two files are compared byte by byte and the location of the first mismatch is enhoed in the screen

OUTPUT :

Cmp sample1 sample5

Sample1 sample5 differ,byte 7,line1

39.SYNTAX : cmp -1 file1,file2

USES : This option gives the detailed list of the byte number and the the differing bytes in octal for each character that differ in both files

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

18

Page 19: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

OUTPUT :

Cat sample1 cat sample2Flower flower Jasmine carrotRose papaya Banana

Cmp -1 sample1 sample5

7 12 40 8 152 12 9 141 14310 163 14111 155 16212 151 162 13 156 15714 145 16416 162 160 17 157 141 18 163 16019 145 141 20 12 171Cmp:EOF on sample1

CUT COMMAND

40.SYNTAX : cut –d “any character” –f2 filename

USES : Used to cut the character fromfirst to the specified character

from the file OUTPUT : Cat sample1Flower Jasmine Rose Cut-d”w”-f2 sample

Er JasmineRose

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

19

Page 20: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

41.SYNTAX :cut-c l-3 filename

USES : It will command the character which are defind in numbers and

it will Displays the character upto that specified number. OUTPUT : Cat sample5Flower CarrotPapayaBanan

Cut –c 4 sample5

COPY COMMAND

42.SYNTAX :cp source filename destination filename

USES : Used to copy the source file contents to destination file

contents

PASTE COMMAND

43.SYNTAX : paste file1 file2

USES : This command merges the content of 2 files in into a single file it reads a line from file in the file list specified and combines them

into a single file.OUTPUT :

Paste sample1 sample2Flower ab Jasmine bc Rose d Ef Flower Jasmine Rose

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

20

Page 21: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

UNIQUE COMMAND

44.SYNTAX : uniq filename

USES : Used to display the unique lines present in the file

EX:

OUTPUT :$cat n l Sridhar Ramamoorthy Mahalashmi Jegan Prabu Prabu

$ uniq n lSridhar Ramrmoorthy Maha lashmi Jegan Prabu OPTIONS

45.SYNTAX : uniq –u file name

USES : Used to display only unique lines present in the file

OUTPUT :

$uniq –u n lSridhar Ramamoorthy Mahalashmi Jegan

46.SYNTAX : uniq-d filename

USES : Used to display the duplicate lines in the file

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

21

Page 22: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

OUTPUT :

$uniq –d n lPrabu

47.SYNTAX : uniq-c filename

USES : Used to count the duplicate lines in the file

OUTPUT :

$uniq –c n lSridhar Ramamoorthy mahalashmijegan

RESULT : Thus the above shell commands have been executed and the output is verified successfully

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

22

Page 23: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

SHELL PROGRAMMING

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

23

Page 24: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:1

DATE: 1.a BIGGEST OF TWO NUMBERS

AIM: Write a shell program to find the biggest of two numbers

ALGORITHM: Step1: start the program Step2: read the numbers Step3:compare the two numbers by using if condition statement Step4:print the biggest numbers Step5: print the biggest numbers Step6:terminate the program

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

24

Page 25: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

Program:

echo "Enter the first number"read xecho "Enter the second number"read yif [ $x -gt $y ]thenecho "$x is greater"elseecho "$y is greater"fi

OUTPUT:

"sen.sh" 12L, 149C written[root123@localhost]$ sh sen.sh

Enter the first number5Enter the second number99 is greater

[root123@localhost]$ sh sen.shEnter the first number6Enter the second number46 is greater

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

25

Page 26: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

1.b MULTIPLICATION TABLE Program:

echo "Enter the number"read necho "The multiplication table for number $n:"for k in 1 2 3 4 5 6 7 8 9 10dos=`expr $k \* $n`echo "$k* $n= $s"done

OUTPUT:

[root123@localhost]$ sh multiplication.shEnter the number4The multiplication table for number 4:1* 4= 42* 4= 83* 4= 124* 4= 165* 4= 206* 4= 247* 4= 288* 4= 329* 4= 3610* 4= 40[root123@localhost]$

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

26

Page 27: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

1.c SWAP PROGRAM

PROGRAM:

echo "Enter the first number"read xecho "Enter the second number"read yt=$xx=$yy=$techo "The swapped no. is:"echo "X=$x"echo "Y=$y"

OUTPUT:

"swap.sh" 11L, 138C written[root123@localhost]$ sh swap.sh

Enter the first number4Enter the second number8The swapped no. is:X=8Y=4

RESULT: Thus the swapping of two numbers are executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

27

Page 28: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:2

DATE: 2.a SUM OF THE NUMBERS

AIM: Write a shell program to print the following sum of series

ALGORITHM:

Step1:start the program Step2:read the numbers N Step3:initialize sum as 0 Step4:assign 1toi Step5:repeat while less than or equal number Step6:add sum by I and store in sum Step7:increment I by 1 Step8:print the sum Step9:terminate the program

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

28

Page 29: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:

echo "enter the number"read ni=1sum=0while [ $i -le $n ]dosum=`expr $i + $sum`i=`expr $i + 1`doneecho "sum of the number is $sum:"

OUTPUT:

"sum.sh" 11L, 141C written[root123@localhost]$ sh sum.sh

enter the number5sum of the number is 15:

[root123@localhost]$

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

29

Page 30: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

2.b ODD OR EVENPROGRAM:echo enter the numberread nf=`expr $n % 2`if [ $f -eq 0 ]thenecho the number is evenelseecho the number is oddfi

OUTPUT:"eo.sh" 9L, 122C written[root123@localhost]$ sh eo.shenter the number5the number is odd[root123@localhost]$ sh eo.shenter the number6the number is even

[root123@localhost]$

RESULT: Thus the odd or even program is executed and vrified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

30

Page 31: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:3

DATE: REVERSE OF A NUMBER

AIM: Write a shell program to print the reversed number

ALGORITHM: Step1:start the program Step2:read the number x Step3:initialize c=0 and a=0 Step4:repeat while not equal to 0 Step5:take x made by 10 and store it c Step6:add c with a multiply by 10 and store in a Step7:divide the value x by 10 and store in x Step8:print the value a Step9:terminate the program

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

31

Page 32: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:

echo enter the numberread nsum=0while [ $n -gt 0 ]dor=`expr $n % 10`sum=`expr $sum \* 10 + $n=`expr $n / 10`doneecho the result is $sum

OUTPUT:

"si.sh" 10L, 148C written[root123@localhost]$ sh si.shenter the number54394the result is 49345[root123@localhost]$

RESULT: Thus the reverse the number program is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

32

Page 33: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:4

DATE: SIMPLE CALCULATOR

AIM: To write a shell program that simulates a simple calculator

ALGORITHM: Step1:start the program Step2:read the program Step3:display the menu choices 1.addition 2.subtraction 3.multiplication 4.division Step4:read the choice Step5:if choice is 1 Compare sum of two numbers Step6:if choice is 2 Compare difference of two numbers Step7:if choice is 3 Compare product of two numbers Step8:if choice is 4 Compare divide of two numbers Step9:print the result Step10:terminate the program

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

33

Page 34: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:

echo "Enter the first number"read aecho "Enter the second number"read becho "1.add"echo "2.sub"echo "3.mul"echo "4.div"echo "5.exit"echo "Enter the option"read opcase $op in(1)c=`expr $a + $b`echo "The result is $c";;(2)c=`expr $a - $b`echo "The result is $c";;(3)c=`expr $a \* $b`echo "The result is $c";;(4)c=`expr $a \/ $b`echo "The result is $c";;(5)exitesac

OUTPUT:

"cal.sh" 23L, 385C written[root123@localhost]$ sh cal.sh

Enter the first number5Enter the second number31.add2.sub3.mul4.div5.exitEnter the option3

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

34

Page 35: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

The result is 15

[root123@localhost]$ sh cal.shEnter the first number5Enter the second number51.add2.sub3.mul4.div5.exitEnter the option4The result is 1

[root123@localhost]$ sh cal.sh

Enter the first number5Enter the second number31.add2.sub3.mul4.div5.exitEnter the option2The result is 2

[root123@localhost]$ sh cal.sh

Enter the first number5Enter the second number31.add2.sub3.mul4.div5.exit

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

35

Page 36: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

Enter the option1The result is 8

[root123@localhost]$

RESULT:

Thus the shell program for simple calculator is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

36

Page 37: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:5

DATE: FACTORIAL NUMBER

AIM:

Write a shell program that prints the factorial of two numbers

ALGORITHM: Step1:start the program Step2:read the mumbers Step3:initialize factorial as1 Step4:assign ito1 Step5:multiply factorial by store i an in factorial Step6:increrment i by 1 Step7:repeat while i<=number Step8:print the factorial number Step9:terminate the program

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

37

Page 38: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:

echo "Enter the number"read ni=1fact=1while [ $i -le $n ]dofact=`expr $fact \* $i`i=`expr $i \+ 1`doneecho "The factorial of $n is: $fact"

OUTPUT:

"fact.sh" 12L, 150C written[root123@localhost]$ sh fact.sh

Enter the number5The factorial of 5 is: 120[root123@localhost]$

RESULT:

Thus the shell program for factorial number is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

38

Page 39: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:6

DATE: FIBONACCI SERIES

AIM: Write a shell program that print the fibonaci series of numbers

ALGORITHM:

Step1:start the program Step2:read the numbers Step3:initialize a=-1,b=1,i=1,c=0 Step4:repeat at while i<=number Step5:calculate c=a+b and print the value c Step6:assign value b to a and c to b Step7:increment I value by 1 Step8:terminate the program

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

39

Page 40: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:

echo "Enter the limit"read necho "The fibonacci series:"c=0a=-1b=1i=1while [ $i -le $n ]doc=`expr $a + $b`echo $ca=$bb=$ci=`expr $i + 1`done

OUTPUT:

"fibo.sh" 15L, 155C written[root123@localhost]$ sh fibo.shEnter the limit5The fibonacci series:01123[root123@localhost]$

RESULT: Thus the shell program for Fibonacci series is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

40

Page 41: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:7

DATE: ARMSTRONG NUMBER

AIM: Write a shell program that print the armstorng numbers

ALGORITHM:

Step1:start the program Step2:read the numbers Step3:initialize sum as 0 Step4:repeat n while greater than or equal to 0 Step5:take number mod by 10 and store it in R Step6:dividethe number by 10 Step7:calculate sum with cube of R Step8:if the num equal to sum and print armstorg number Step9:else the num is not an armstorng number Step10:terminate the program

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

41

Page 42: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:

echo "Enter the number"read nnum=$nsum=0while [ $n -gt 0 ]dor=`expr $n % 10`n=`expr $n \/ 10`sum=`expr $sum + $r \* $r \* $r`doneif [ $num -eq $sum ]thenecho "The number $num is armstrong number"elseecho "The number $num is not armstrong number"fi

OUTPUT: "arms.sh" [New] 16L, 263C written[root123@localhost]$ sh arms.sh

Enter the number153The number 153 is armstrong number

[root123@localhost]$ sh arms.shEnter the number234The number 234 is not armstrong number

RESULT: Thus the shell program for armstrong number is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

42

Page 43: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:8

DATE: PRIME NUMBER

AIM: Write a shell program that print the prime number

ALGORITHM:

Step1:start the program Step2:read the numbers Step3:assign i=2 Step4:repeat I whule less than n Step5:take number n mod I and store it in Y Step6:if Y equal to 0- print not prime number Step7:indrement the value of i Step8:else print prime number Step9:terminate the program

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

43

Page 44: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:

echo "Enter the number"read ni=2while [ $i -lt $n ]doy=`expr $n \% $i`if [ $y -eq 0 ]thenecho "$n is not a prime number"exitfii=`expr $i + 1`doneecho "$n is prime number"

OUTPUT: "prime.sh" 15L, 186C written[root123@localhost]$ sh prime.sh

Enter the number153153 is not a prime number

[root123@localhost]$ sh prime.sh

Enter the number1111 is prime number

[root123@localhost]$

RESULT: Thus the shell program for prime number is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

44

Page 45: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:9

DATE: PALINDROME

AIM: Write a shell program to check whether the string is palindrome or not

ALGORITHM:

Step1:start the program Step2:get the string from the user Step3:calculate the len of the substr fuction Step4:remove the string by using substr fuction Step5:check wheather the given string and reversed strings are equal Step6:if both the strings are equal then the given string is palindrome Step7:else print the given string is not a palindrome

Step8:terminate the program

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

45

Page 46: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:

echo "Enter the string"

read strlen=`expr length $str`i=$lenwhile [ $i -ge 1 ]dosub=`expr substr $str $i 1`res=`expr $res$sub`i=`expr $i - 1`doneecho "The reversed string is $res"if [ $str = $res ]thenecho "The string is a palindrome"elseecho "The string is not a palindrome"fi

OUTPUT:"palindrome.sh" 18L, 295C written[root123@localhost]$ sh palindrome.shEnter the stringammaThe reversed string is ammaThe string is a palindrome[root123@localhost]$ sh palindrome.shEnter the stringsenthilThe reversed string is lihtnesThe string is not a palindrome

RESULT: Thus the shell program for palindrome is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

46

Page 47: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:10

DATE: PAYROLL GENERATION

AIM: Write a shell program to payroll generation

ALGORITHM:

Step1:start the program Step2:read eno ename bsalary designation Step3:display the menu choices 1.bsalary(5000to 8000) 2.bsalary(800to10000) 3.bsalary(10000toabove) Step4:read the choice Step5:if the choice is 1 Calculate the suitable hra dap f lic,gross,ded,netsalary. Step6: if the choice is 2 Calculate the suitable hra dap f lic,gross,ded,netsalary.

Step7: if the choice is 3 Calculate the suitable hra dap f lic,gross,ded,netsalary. Step8: if the choice is 4 Calculate the suitable hra dap f lic,gross,ded,netsalary.

Step9:display eno,ename,displayed Step10:display the values of above displayed

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

47

Page 48: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:

echo enter the employee detailsecho Eno Ename Bsalary Designationread Eno Ename Bsalary Designationecho “1.Bsalary(5000 to 8000)”echo”2.Bsalary(8000to10000)”echo”3.Bsalaty(10000 and above)”echo enter the choiceread ncase $n in1)hra=’expr $Bsalary \* 5 / 100’da=’expr $Bsalary \* 50 / 100’pf=’expr $Bsalary \* 8 / 100’lic=’expr $Bsalary \* 6 /100’gross=’expr $Bsalary + $hra + $da’ded=’expr $pf + $lic’ netsalary=’expr $gross - $ded’;;2)hra=’expr $Bsalary \* 10 / 100’da=’expr $Bsalary \* 60 / 100’pf=’expr $Bsalary \* 13 / 100’lic=’expr $Bsalary \* 10 /100’gross=’expr $Bsalary + $hra + $da’ded=’expr $pf + $lic’ netsalary=’expr $gross - $ded’;;3)hra=’expr Bsalary \* 15 / 100’da=’expr $Bsalary \* 70 / 100’pf=’expr $Bsalary \* 18 / 100’lic=’expr $Bsalary \* 12 /100’gross=’expr $Bsalary + $hra + $da’ded=’expr $pf + $lic’ netsalary=’expr $gross - $ded’;;4)exitesac

echo Payroll Generationecho Eno:$Enoecho Ename: $Enameecho Basicsalary: $Bsalaryecho Designation:$Designationecho Grossalary:$grossecho Deduction:$ded

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

48

Page 49: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

echo Netsalary:$netsalary

OUTPUT:

[root123@localhost]$sh.es.sh enter the employee details Eno Ename Bsalary Designation 101 test 8000 ECE 1.Bsalary(5000 to 8000) 2.Bsalary(8000 to 10000) 3.Bsalary(10000 and above) enter the choice 2 Payroll Generation Eno: 101 Ename: test Basicsalary: 8000 Designation:ECE Grosssalary:13600 Deduction:1840 Netsalary:11760

RESULT: Thus the shell program for payroll generation is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

49

Page 50: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

C PROGRAMMING ON UNIX

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

50

Page 51: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

FUNCTIONS

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

51

Page 52: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:1

DATE: 1. LINEAR SEARCH USING FUNCTION

AIM: To write a C program to per form linear search usiong function

ALGORITHM:

Main() program Step1:start the program Step2:read the value of n Step3:assign i=0 Step4:repeat the step 5 to 6 until i<n Step5:read a[i] Step6:compute i=i+1 Step7:read the value of x Step8:call linear(a,n,x) function Stap9:stop

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

52

Page 53: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:

#include<stdio.h>void linear(int [],int ,int );main(){ int n,i,a[50],x; printf("Enter the limit\n"); scanf("%d",&n); printf("\nEnter the elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nEnter the elements to be searched:"); scanf("%d",&x); linear(a,n,x);}void linear(int a[],int n, int x){ int i,flag; for(i=0;i<n;i++) { if(x==a[i]) { flag=1; break; } } if(flag==1) printf("\nThe element is %d in the position of %d\n", a[i],i+1); else printf("\nThe element is not found in the list");}

OUTPUT:linear.c" 32L, 508C written

[root123@localhost]$ cc linear.c[root123@localhost]$ ./a.outEnter the limit5

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

53

Page 54: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

Enter the elements10204010080Enter the elements to be searched:100

The element is 100 in the position of 4

[root123@localhost]$ ./a.outEnter the limit5

Enter the elements10204010080

Enter the elements to be searched:90

The element is not found in the list

RESULT: Thus the C program for linear search is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

54

Page 55: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:2

DATE: BINARY SEARCH USING FUNCTION

AIM: To write a C program to binary search using fuction

ALGORITHM: Step1:start the program Step2:read the value of n Step3:assign i=0 Step4:repeat the steps 5 to 6 until i<n Step5:read a[i] Step6:compare i=i+1 Step7:read the value of s Step8:call binary (q,n,s,)fuction Step9:stop

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

55

Page 56: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:

#include<stdio.h>void binary(int [],int ,int );void sort(int [],int );main(){ int n,i,a[100],x,s; printf("enter the limit"); scanf("%d",&n); printf("enter the elements:"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } sort(a,n); printf("\n enter the element to be searched:"); scanf("%d",&s); binary(a,n,s);}void binary(int a[],int n,int s){ int f,l,u,m; l=0; u=n-1; while(l<=u) { m=(u+l)/2; if(s<a[m]) u=m-1; if(s>a[m]) l=m+1; else { f=1; break; } } if(f==1) printf("\n the element is found"); else printf("\n the element is not found\n"); }

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

56

Page 57: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

void sort(int a[],int n){ int i,j,temp; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(a[i]>a[j+1]) { temp=a[i]; a[i]=a[j+1]; a[j+1]=temp; } } } }

OUTPUT:

"binary.c" 64L, 762C written[root123@localhost]$ cc binary.c[root123@localhost]$ ./a.outEnter the no of elements:5

Enter the element:1030204050sorted element1020304050Enter the element to be searched:50

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

57

Page 58: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

Element is found[root123@localhost]$

RESULT: Thus the C program for binary search using function is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

58

Page 59: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:3

DATE: FACTORIAL USING RECURSION

AIM: To write a C program to find the factotial of a given number using recursive fuction

ALGORITHM:

main()program

Step1:start the program Step2:read the value of num Step3:assign fact=cal fact (num) fuction Step4:print the fact 1 Step5:stop

fact() fuction Step1: start the program Step2: if (n==1 || n==0)then step3 else step 4 Step3: return() Step4:return(n *fact(n-1))

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

59

Page 60: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:Int rec(int);main(){ int n,result; printf("enter the value \n"); scanf(“%d”,&n);result=rec(n); printf(“the result is %”,result);}Int rec(int n){Int f;If(n==1)Return(1);ElseF=n * rec(n-1);Return(f);}

OUTPUT:

[root123@localhost]$ cc sum1.c[root123@localhost]$ ./a.outenter the value5The result is 120

RESULT: Thus the C program for factorial using recursion is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

60

Page 61: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:4

DATE: FIBONACCI SERIES USING RECURSIVEFUNCTION

AIM: To write a C program to print the Fibonacci series using recursive program

ALGORITHM:

main()program

Step1:start the program Step2:read the value of num Step3:call fib (0,1,n)fuction Step4:stop fib()fuction Step1:start the program Step2:if (num!=0)then step 3 else step 5 Step3:print n Step4:call fib (n2,n2+n,num-1) Step5:return

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

61

Page 62: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:

#include<stdio.h>void fib(int n1,int n2,int num);main(){int n;printf(“enter the limit\n”);scanf(“%d”,&n);fib(0,1,n);}void fib(int n1.int n2,int num){if (num!=0)printf(“%d”,n);fib(n2,n2+n1,num-1);}}

OUTPUT:

"swapv.c" 18L, 216C written[root123@localhost]$ cc swapv.c[root123@localhost]$ ./a.outEnter the limit501123

RESULT: Thus the C program for fibonacci series using recursive function is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

62

Page 63: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

POINTERS

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

63

Page 64: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:5

DATE: 5. PROGRAM TO SORT THE DIGITS OF AN INTEGER USING POINTER

AIM: To write a C program to sort the digit of an integer using pointer

ALGORITHM:

main()program

Step1:start the program Step2:read the number ‘n’ Step3:declare n in the pointer variable p Step4:call the fuction digit call fuction(p,x) Step5:stop digit()fuction Step1:assign i=0 and count=0 Step2:repeat the steps 3 to 5 until n!=0 Step3:take n by mod by 10 and store in (p+i) Step4:divide the n by 10 and store in n Step5:assign i=i+1 and count=count+1 Stap6:return the value of count

PROGRAM:

#include<stdio.h>main()

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

64

Page 65: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

{ int n,i,a[10],*p,x; printf("enter the no \n"); scanf("%d",&n); p=&a[0]; x=digit(p,n); printf("Sorted Digits\n"); sort(p,x);}

digit(int *p,int n){ int i=0,count=0; while(n!=0) { *(p+i)=n%10; n=n/10; i++; count++; } return(count); }

sort(int *p,int x){ int temp,i,j; for(i=0;i<x;i++) { for(j=i+1;j<x;j++) { if(*(p+i) > *(p+j)) { temp=*(p+i); *(p+i)=*(p+j); *(p+j)=temp; } } } for(i=0;i<x;i++) { printf("%d",*(p+i)); }

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

65

Page 66: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

}}

OUTPUT:

"bb.c" 45L, 450C written[root123@localhost]$ cc bb.c[root123@localhost]$ ./a.outenter the no156237Sorted Digits123567[root123@localhost]$

RESULT: Thus the C program for program to sort the digits of an integer using pointer is

executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

66

Page 67: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:6

DATE:

6. PROGRAM TO ACCESS ELEMENTS OF AN ARRAY & COUNT THE NO OF OCCURANCES OF THE GIVEN NO IN THE ARRAY USING POINTERS

AIM: To write a C program using pointers to access the elements of an array and count the number of occurrences of the given number in tne array.ALGORITHM: Step1:start the program Step2:assign count=0 and pos=0 Step3:assign p=&a[0] Step4:read the value of n Step5:assign i=0 Step6:repeat the steps 7 to8 untill i<n Step7:read(p+i) Step8:compre i=i+1 Step9:assign i=0 Step10:repeat the steps11to 12 until i<n Step11:ptint *(p+i) Step12:compute i=i+1 Step13:read the value of m Step14:assign i=0 Step15: repeat the steps 16 to19 until i<n Step16:if(*(p+i)))==m)then steps 17 to 18 Step17:compute count=count+1 Step18:compute pos=pos+1 Step19:compute i=i+1 Step20:if(count==0)then step 21 else step 22 Step21:print the elements is not present Step22:present m and pos Step23:stop

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

67

Page 68: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:

#include<stdio.h>main(){ int x,i,a[10],*p,n,count=0; printf("enter the limit \n"); scanf("%d",&x); p=&a[0]; printf(“enter the element\n”); for(i=0;i<x;i++) { scanf("%d",(p+i)); } printf(“Display the element\n"); for(i=0;i<x;i++) { printf("%d\n",*(p+i)); } printf("enter the elements to be searched\n"); scanf("%d",&n); for(i=0;i<x;i++) { if(n==*(p+i)) { count++; } } if(count==0) printf("the no is not present"); else printf("the no %d occurs %d times",n,count);}

OUTPUT:

"occ.c" 45L, 450C written[root123@localhost]$ cc occ.c[root123@localhost]$ ./a.outenter the limit5Enter the element

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

68

Page 69: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

759710enter the elements to be searched7the no 7 occurs 2 times[root123@localhost]$

RESULT:

Thus the C program for program to access elements of an array & count the no of occurrences of the given no in the array using pointers is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

69

Page 70: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

FILE HANDLING

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

70

Page 71: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

EX:NO:7

DATE:

7. COPY THE CONTENTS OF ONE FILE TO ANOTHER

AIM: To write a C program to copy the content of one file to another

ALGORITHM: Step1:start the program Step2:declare the file pointer fp1&fp2 Step3:read the source file name fname1 Step4:read the target file name fname2 Step5:open the file fname ,under read mode assigned To file pointer fp1 Step6:open the file fname unter write mode assigned to file Pointer fp2 Step7:if(fp1==null+then step8 else step9 Step8:print file fname,cannot be opend for reading and then goto Step9:if(fp2= =null)then step10 else step11 Step10:print file fname2 can notbe opened for writing and then goto step Step11:repeat the step13 to14,until while(c!=EOF) Step12:write the character infp2 Step13:assign c=getc(fp1) Step14:close all files(fname,and fname2) Step15:stop

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

71

Page 72: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

PROGRAM:#include<stdio.h>#include<stdlib.h>main(){ FILE *fp1,*fp2; int c; char fname1[40],fname2[40]; printf("\n\n\t\t To copy contents of one file to another "); printf("\n\n enter the source file:"); scanf("%s",&fname1); printf("\n\nenter the target file :"); scanf("%s",&fname2); fp1=fopen(fname1,"r"); fp2=fopen(fname2,"w"); if(fp1 == NULL) { printf("\n\ncannot open %s for readings \n ",fname1); exit(1); } else if (fp2 == NULL) { printf("\n\n cannot open %s for waiting \n",fname2); exit(1); }else { c=getc(fp1); while(c!=EOF) { putc(c,fp2); c=getc(fp1); } fclose(fp1); fclose(fp2); printf("\n\nfile successfully copied \n ");}}

OUTPUT:"fcopy.c" 66L, 611C written[root123@localhost]$ cc fcopy.c

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

72

Page 73: COMPUTER PRACTICE LABORATORY – II.pdf

KRS COLLEGE OF ENGINEERING

[root123@localhost]$ ./a.out

To copy contents of one file to another

enter the source file: aaa

enter the target file: bbb

nfile successfully copied [root123@localhost]$

RESULT: Thus the C program for copy the contents of one file to another

is executed and verified

CSE LAB MANUAL: 185253COMPUTER PRACTICE LAB-II / I YEAR/02 SEM/2011-12 BY R.Vidya, LECT/CSE, KRSCE

73