foc lab manual

75
I JERUSALEM COLLEGE OF ENGINEERING CHENNAI-100 DEPARTMENT OF COMPUTER APPLICATIONS LABORATORY MANUAL COMPUTER PRACTICE LABORATORY-II B.E II-SEMESTER 2010- 2011

Upload: vinu-arunagiri

Post on 03-Dec-2014

206 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Foc Lab Manual

I

JERUSALEM COLLEGE OF ENGINEERING CHENNAI-100DEPARTMENT OF COMPUTER APPLICATIONS

LABORATORY MANUAL COMPUTER PRACTICE LABORATORY-II

B.E II-SEMESTER2010-2011

Page 2: Foc Lab Manual

II NN DD EE XX

SS.. NN OO NN AAMM EE OO FF TTHH EE EXEXPP EERR II MM EENN TT

11 Study Of Unix Operating System.

22 Study Of Basic Unix Commands.

33 Study Of Vi Editors.

44 Calculating The Gross Salary Of an Employee.

55 Finding The Area Of A Circle.

66To Implement The Concept Of Two-Way Branching Using If Statement.

77To Implement The Concept Of Multi-Way Branching Using If- Elif Statement.

88 To Implement The Concept Of Multi-Way Branching Using Case Statement.

99 To Write A Program To Print N Natural Numbers.

1010 To Write A Program To Print First N Number Of Fibonacci Series.

1111 To Write A Program To Print The Multiplication Table Using Until Loop.

1212 To Write A Program To Print The Sum Using For Loop.

1313 To Write A Program To Calculate The Factorial Of a Given Number.

14 Program For The Illustration Of Pointer Expressions.

1515 Program For Dynamic Memory Allocation, Reallocation And Deletion

1616Program To Create File, To Store Information In The File And To Show The Information In The File By Using File Handling Functions.

Page 3: Foc Lab Manual

SSTT UU DD YY OOFF UU NN II XX OO PEPERR AATT II NN GG SYSSYSTT EMM

W H AT I S U N I X? U N I X i s a n o p e r a t i n g s y s t e m w h i c h w a s d e v e l o p e d f i r s t i n t h e

1 9 6 0 ’ s a n d h a s u n d e r c o n s t a n t d e v e l o p m e n t e v e r s i n c e b y o p e r a t i n g s y s t e m w e m e a n t h e s u i t e o f p r o g r a m s w h i c h m a k e t h e c o m p u t e r w o r k . I t i s a s t a b l e , m u l t i - u s e r , m u l t i - t a s k i n g o p e r a t i n g s y s t e m f o r s e r v e r s , d e s k t o p s a n d

l a p t o p s . U N I X o p e r a t i n g s y s t e m a l s o h a v e g r a p h i c a l u

s e r i n t e r f a c e ( G U I ) s i m i l a r t o M i c r o s o f t w i n d o w s o p e r a t i n g s y s t e m w h i c h p r o v i d e s a n e a s y t o u s e e n v i r o n m e n t . H o w e v e r k n o w l e d g e o f u n i x i s r e q u i r e d f o r o p e r a t i o n s w h i c h a r e n o t c o v e r e d b y g r a p h i c a l p r o g r a m o r f o r w h e n t h e r e i s n o w i n d o w s i n t e r f a c e a v a i l a b l e .T Y P E S O F U N I X

T h e r e a r e m a n y d i f f e r e n t v e r s i o n s o f u n i x , a l t h o u g h t h e y s h a r e t h e c o m m o n s i m i l a r i t i e s .T h e m o s t p o p u l a r v a r i e t i e s o f u n i x a r e S u n S o l a r i s , G N U / L i n u x a n d M a c o s X .T H E U N I X O P E R A T I N G S Y S T E M

T h e U n i x O p e r a t i n g s y s t e m i s m a d e u p o f t h r e e p a r t s

1 . T h e K e r n e l

2 . T h e S h e l l

3 . T h e P r o g r a m s

T H E K E R N E L

T h e K e r n e l o f U n i x i s t h e h u b o f t h e o p e r a t i n g s y s t e m . I t a l l o c a t e s t i m e a n d m e m o r y t o p r o g r a m s a n d h a n d l e s t h e f i l e s t o r e a n d c o m m u n i c a t i o n i n r e s p o n s e t o s y s t e m c a l l s .

A s a n i l l u s t r a t i o n o f t h e w a y t h a t t h e s h e l l a n d t h e k e r n e l w o r k t og e t h e r , s u p p o s e a u s e r t y p e s ‘ r m m y f i l e ’ . T h e s h e l l s earches t h e f i l e s t o r e f o r t h e f i l e c o n t a i n i n g t h e p r o g r a m ‘ r m ’ a n d t h e n r e q u e s t s t h e k e r n e l , t h r o u g h s y s t e m c a l l s t o ‘ r m m y f i l e ’ f i n i s h e d r u n n i n g , t h e s h e l l t h e n r e t u r n s t h e u n i x p r o m p t ‘ % ’ t o t h e u s e r , i n d i c a t i n g e x e c u t e

Page 4: Foc Lab Manual

t h e p r o g r a m ‘ r m ’ o n ‘ m y f i l e ’ .W h e n t h e p r o c e s s t h a t i t i s w a i t i n g f o r f u t u r e c o m m a n d s .

Page 5: Foc Lab Manual

T H E S H E L L

T h e s h e l l a c t s a s a n i n t e r f a c e b e t w e e n t h e u s e r a n d t h e k e r n e l . W h e n u s e r l o g i n , t h e l o g i n p r o g r a m c h e c k s t h e u s e r n a m e a n d p a s s w o r d , a n d t h e n s t a r t s a n o t h e r p r o g r a m c a l l e d t h e s h e l l . T h e s h e l l i s a c o m m a n d l i n e i n t e r p r e t e r ( C L I ) . I t i n t e r p r e t s t h e c o m m a n d s t h e u s e r t y p e s i n a n a r r a n g e s f o r t h e m t o b e c a r r i e d o u t . T h e c o m m a n d s a r e t h e m s e l v e s p r o g r a m s . W h e n t h e y t e r m i n a t e , t h e s h e l l g i v e s t h e u s e r a n o t h e r p r o m p t ( % o n o u r s y s t e m ) . B y t y p i n g p a r t o f t h e n a m e o f a c o m m a n d , f i l e n a m e a n d d i r e c t o r y a n d p r e s s i n g t h e [ t a b ] k e y , t h e s h e l l w i l l c o m p l e t e t h e r e s t o f t h e m a u t o m a t i c a l l y . I f s h e l l f i n d s m o r e t h a n o n e n a m e b e g i n n i n g w i t h t h o s e l e t t e r s y o u h a v e t y p e d , i t w i l l b e e p , p r o m p t i n g t o t y p e a f e w m o r e l e t t e r s b e f o r e p r e s s i n g t h e t a b k e y a g a i n .

T h e s h e l l k e e p s t h e l i s t o f t h e c o m m a n d s y o u h a v e t y p e d i n , i f y o u n e e d t o r e p e a t a c o m m a n d , u s e t h e c u r s o r k e y t o s c r o l l u p a n d d o w n t h e l i s t o r t y p e h i s t o r y f o r a l i s t o f p r e v i o u s c o m m a n d s . D i f f e r e n t u s e r s m a y u s e d i f f e r e n t s h e l l s . I n i t i a l l y , y o u r s y s t e m a d m i n i s t r a t o r w i l l s u p p l y a d e f a u l t s h e l l , w h i c h c a n b e o v e r r id d e n o r c h a n g e d .

T h e m o s t c o m m o n l y a v a i l a b l e s h e l l s a r e :

o B o u r n e s h e l l ( s h ) C s h e l l ( c s h )o K o r n s h e l l ( k s h )o T C S h e l l ( t c s h )o B o u r n e A g a i n S h e l l ( b a s h )

T h e B o u r n e s h e l l i s o n e o f t h e o l d e s t s h e l l s a n d i s t h e m o s t e f f i c i e n t f o r b a c k g r o u n d w o r k . H o w e v e r i t p r o v i d e s f e w f a c i l i t i e s f o r i n t e r a c t i v e u s e r s .

T h e C s h e l l p r o v i d e s s o p h i s t i c a t e d i n t e r a c t i v e c a p a b i l i t i e s l a c k i n g i n t h e B o u r n e s h e l l . F e a t u r e s o f t h i s s h e l l i n c l u d e a c o m m a n d h i s t o r y b u f f e r , c o m m a n d a l i a s e s a n d f i l e n a m e

c o m p l e t i o n .T h e C s h e l l h a s a s y n t a x w h i c h r e s e m b l e s t h e C p r o g r a m m i n g l a n g u a g e .

Page 6: Foc Lab Manual

T h e C s h e l l i s t h e d e f a u l t s h e l l f o r i n t e r a c t i v e w o r k o n m a n y U N I X s y s t e m s . I t w i l l b e c o v e r e d i n t h i s d o c u m e n t , a l t h o u g h m o s t o f t h e b a s i c c o m m a n d s g i v e n h e r e a r e r e l a t i v e l y s t a n d a r d a c r o s s a l l t h e m a i n s h e l l s .

T h e K o r n s h e l l w a s w r i t t e n b y D a v i d K o r n f r o m A T & T a n d i n i t h e a t t e m p t e d t o m e r g e t h e p r e f e r r e d f e a t u r e s o f b o t h t h e B o u r n e a n d C s h e l l s a s w e l l a s a d d i n g s o m e a d d i t i o n a l f e a t u r e s . U n f o r t u n a t e l y t h e K o r n s h e l l w a s n o t a v a i l a b l e f o r f r e e , a s o t h e r U N I X s h e l l s w e r e , s o m a n y u s e r s a n d c o m p a n i e s d i d n o t c h o s e t o u s e o f i t .

T h e B a s h s h e l l w a s b a s e d o n t h e B o u r n e s h e l l ( B o u r n e a g a i n s h e l l ) a n d a s w i t h K o r n i t a t t e m p t e d t o c o m b i n e t h e b e s t f e a t u r e s o f t h e o t h e r s h e l l s w h i c h w e r e a v a i l a b l e a t t h e t i m e . T h i s s h e l l h o w e v e r w a s a v a i l a b l e f o r f r e e . B a s h w a s i n i t i a l l y a d o p t e d f o r L I N U X a l t h o u g h s e v e r a l v a r i e t i e s o f

L I N U X n o w e x i s t e . g . R e d H a t , S u S E a n d D e b i a n - G N U .

F I L E S A N D P R O C E S S E v e r y t h i n g i n u n i x i s e i t h e r a f i l e o r a p r o c e s s . A p r o c e s s i s a ne x e c u t i n g p r o g r a m i d e n t i f i e d b y a u n i q u e P I D ( P r o c e s s I d e n t i f i e r ) . A f i l e i s c o l l e c t i o n o f d a t a ’ s . T h e y a r e c r e a t e d b y u s e r s u s i n g t e x t e d i t o r s , r u n n i n g , c o m p i l e r s e t c .

A l l f i l e s a r e g r o u p e d t o g e t h e r i n t h e d i r e c t o r y s t r u c t u r e . T h e f i l e s y s t e m i s a r r a n g e d i n a h i e r a r c h i c a l s t r u c t u r e , l i k e i n v e r t e d t r e e . T h e t o p o f t h e h i e r a r c h y i s t r a d i t i o n a l l y c a l l e d r o o t .

F E A T U R E S O F U N I X

1 . M u l t i t a s k i n g

M u l t i t a s k i n g i s t h e c a p a b i l i t y o f t h e o p e r a t i n g s y s t e m t o p e r f o r m v a r i o u s t a s k s s i m u l t a n e o u s l y ie a s i n g l e u s e r c a n r u n m u l t i p l e p r o g r a m s( t a s k s ) c o n c u r r e n t l y .

2 . M u l t i - u s e r C a p a b i l i t y

M u l t i - u s e r c a p a b i l i t y o f U N I X a l l o w s s e v e r a l u s e r s t o u s e t h e s a m e c o m p u t e r t o p e r f o r m t h e i r t a s k s . S e v e r a l t e r m i n a l s ( k e y b o a r d s a n d m o n i t o r s ) a r e c o n n e c t e d t o a s i n g l e p o w e r f u l c o m p u t e r ( U n i x s e r v e r ) a n d e a c h u s e r

Page 7: Foc Lab Manual

c a n w o r k w i t h t h e i r t e r m i n a l s .

3 .S e c u r i t y

U n i x a l l o w s s h a r i n g o f d a t a . E v e r y u s e r m u s t h a v e a l o g i n n a m e an d a p a s s w o r d .S o a c c e s s i n g a n o t h e r u s e r ’ s d a t a i s i m p o s s i b l e w i t h o u t p e r m i s s i o n ie i n v a l i d u s e r s c a n n o t a c c e s s d a t a .

4 . P o r t a b i l i t y

U N I X i s p o r t a b l e b e c a u s e i t i s w r i t t e n i n a h i g h l e v e l l a n g u a g e . S o ,

U N I X c a n b e r u n o n d i f f e r e n t c o m p u t e r s .

Page 8: Foc Lab Manual

5 . C o m m u n i c a t i o n

U N I X s u p p o r t s t h e f a l l o w i n g c o m m u n i c a t i o n s

o B e t w e e n t h e d i f f e r e n t t e r m i n a l s c o n n e c t e d t o t h e U N I X

s e r v e r .

o B e t w e e n t h e u s e r s o f o n e c o m p u t e rt o t h e u s e r s o f a n o t h e r c o m p u t e r l o c a t e d e l s e w h e r e i n t h e n e t w o r k .

6 . P r o g r a m m i n g f a c i l i t y

U N I X i s h i g h l y p r o g r a m m a b l e , t h e U N I X s h e l l p r o g r a m m i n g l a n g u a g e h a s a l l n e c e s s a r y i n g r e d i e n t s l i k e c o n d i t i o n a l a n d c o n t r o l s t r u c t u r e ( L o o p s ) a n d v a r i a b l e s , t h a t e s t a b l i s h i t a s a p r o g r a m m i n g l a n g u a g e i n i t s o w n r i g h t .

A D V A N T A G E S O F U N I X

U N I X i s a v e r y s t a b l e o p e r a t i n g s y s t e m .

U N I X s u p p o r t s t h e M u l t i - U s e r f e a t u r e .

U N I X i s a M u l t i t a s k i n g o p e r a t i n g s y s t e m .

U N I X c a n b e l o a d e d t o a n y t y p e o f c o m p u t e r h a r d w a r e .

U N I X i s o p t i m i z e d f o r p r o g r a m d e v e l o p m e n t .

U N I X h a s r i c h s e t o f s m a l l c o m m a n d s a n d u t i l i t i e s t h a t d o s p e c i f i c t a s k s .

U N I X h a s a p o w e r f u l u n i f i e d f i l e s y s t e m , e v e r y t h i n g i n a f i l e ;

d a t a , p r o g r a m a n d a l l p h y s i c a l d e v i c e s .

U N I X h a s t h e a b i l i t y t o s t r i n g c o m m a n d s a n d u t i l i t i e s t o g e t h e r i n u n l i m i t e d w a y s t o a c c o m p l i s h m o r e c o m p l i c a t e d t a s k s .

U N I X a l l o w s o n l y a u t h o r i z e du s e r s t o m o d i f y f i l e s a n d d i r e c t o r i e s .

Page 9: Foc Lab Manual

U N I X a l l o w s o n l y S y s t e m A d m i n i s t r a t o r s t o m a k e c h a n g e s i n

S y s t e m C o n f i g u r a t i o n f i l e s .

Page 10: Foc Lab Manual

EX NO: 2 STUDY OF BASIC UNIX COMMANDS

GENERAL COMMANDS

NO COMMAND SYNTAX VARIANCE/OPTIONS DESCRIPTION

1 Date Date_

Displays the current date and time

2 Cal cal MM YYYY

Cal Displays the calendar of the current month

cal YYYY Displays the calendar of the whole year specified

cal MM YYYY Displays the calendar of the specified month and year

3 Man man command name -

Displays the manual pages of the command name specified.

4 Who Who-

Displays information about people who are presently logged in

5 who am i who am i-

Displays information about oneself

6 Finger finger user-

Displays information about the specified user

Page 11: Foc Lab Manual
Page 12: Foc Lab Manual

FILE MANAGEMENT COMMANDS

NO COMMAND SYNTAX VARIANCE/ OPTIONS

DESCRIPTION

7 Cat cat > filename cat > filename Creates a file with the specified name

cat filename Displays the specified file in the console

cat file1 file2…filen > filez

Copies the specified files to filez

cat >> filename Appends contents to the specified file

8 Rm rm filename-

removes the specified file

9 Mv mv filename destination

mv filename destination

Moves the file to the destination

mv filename newname

Renames the file with the new nameRenaming can be done while moving withinthe same directory or different directory

10 Cp cp source destination

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

11 More more file More is a filter for paging through text one screenful at a time for lengthy files.

Page 13: Foc Lab Manual

12 Page page file Same as more except that more requires spacebar to be pressed for every page whereas page requires return key to be pressed

Page 14: Foc Lab Manual

14 File file filename Displays the type of the specified file

15 Wc wc filename Print the number of lines, words, characters and bytes in the file.

16 Head head n filename

Display the first n lines from the file

17 Tail tail n filename Display the last n lines from the file

18 Sort sort filename -r sort in reverse order-f sort ignoring case-n sort numerically-b ignore leading blanks

Sorts the lines in the text files and displays them in the console

19 Grep grep pattern filename

-F for fixed grep-E for extended grep-c print the count of matching lines-i ignore case for matching-h prefix each matching line with line number

Grep searches the named input FILEs for lines containing a match to the given PATTERN.

13 Ls Ls -l shows about permission, owner, size etc-a shows all files including hidden files-R displays the contents of subdirectories-r reverses the order of files displayedls ~ List the contents of your home directory ls / List the contents ofyour root d irectory.ls ../ List the contents of the parent directory. ls */ List thecontents of all sub directories.ls -d */ Only list the directories in the current directory.

Page 15: Foc Lab Manual
Page 16: Foc Lab Manual

20 Chmod chmodfilename users+ per mi ss i ons f il e / d i re c t ory

+ for adding permission- for removing permissionu stands for ownerg stands for groupo stands for others

chmod changes the permissions of each given file accordingto the mode specified

DIRECTORY MANAGEMENT COMMANDS

NO COMMAND SYNTAX VARIANCE/OPTIONS DESCRIPTION

21 Mkdir Mkdirdirname

Creates a directory with the specified name.

22 Rmdir Rmdir dirname

Deletes the specified directory if not empty

23 Mv mv dirname destination

Move the directory and all its contents to the destination. If a name is specified in the destination, the directory will be renamed.

24 Pwd Pwd Shows the current directory

25 Cd cd cd ~

Moves to the home directory

cd .. cd -

Moves to the parent directory Moves to the previous directory

Page 17: Foc Lab Manual

* vi filename edit filename starting at line 1

vi -r filename Recover filename that was being edited when system crashed

EX:NO:3 VI EDITOR

What is VI?

The default editor that comes with the UNIX operating system is called vi (visual editor). [Alternate editors for UNIX environments include Pico and emacs, a product of GNU.]The UNIX vi editor is a full screen editor and has two modes of operation:

1. Command mode commands which cause action to be taken on the file, and2. Insert mode in which entered text is inserted into the file.

In the command mode, every character typed is a command that does something to the text file being edited; a character typed in the command mode may even cause the vi editor to enter the insert mode.

In the insert mode, every character typed is added to the text in the file; pressing the <Esc> (Escape) key turns off the Insert mode.

The most basic and useful commands are marked with an asterisk (* or star) in the tables below.NOTE: Both UNIX and vi are case-sensitive. Be sure not to use a capital letter in place of a lowercase letter; the results will not be what you expect.

To Get Into and Out Of VI

To Start vi

To use vi on a file, type in vi filename. If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.

Page 18: Foc Lab Manual

* :x<Return> quit vi, writing out modified file to file named in original invocation

:wq<Return> quit vi, writing out modified file to file named in original invocation

:q<Return> quit (or exit) vi

* :q!<Return> quit vi even though latest changes have not been saved for this vi call

To Exit vi

Usually the new or modified file is saved when you leave vi. However, it is also possible to quit vi without saving the file.Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command is completed by hitting the <Return> (or <Enter>) key.

Moving the Cursor

Unlike many of the PC and MacIntosh editors, the mouse does not move the cursor within the vi editor screen (or window). You must use the key commands listed below. On some UNIX platforms, the arrow keys may be used as well; however, since vi was designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange effects in vi and should be avoided.If you go back and forth between a PC environment and a UNIX environment, you may find that this dissimilarity in methods for cursor movement is the most frustrating difference between the two.In the table below, the symbol ^ before a letter means that the <Ctrl> key should be held down while the letter key is pressed.

*j or <Return>[or down-arrow] move cursor down one line

* k [or up-arrow] move cursor up one line

*h or <Backspace>

[or left-arrow] move cursor left one character

*l or <Space>[or right-arrow] move cursor right one character

* 0 (zero) move cursor to start of current line (the one with the cursor)

* $ move cursor to end of current line

w move cursor to beginning of next word

b move cursor back to beginning of preceding word

Page 19: Foc Lab Manual

^f move forward one screen^b move backward one screen^d move down (forward) one half screen^u move up (back) one half screen^l redraws the screen

^r redraws the screen, removing deleted lines

* i insert text before cursor, until <Esc> hit

I insert text at beginning of current line, until <Esc> hit

* a append text after cursor, until <Esc> hit

A append text to end of current line, until <Esc> hit

* o open and put text in a new line below current line, until<Esc> hit

* O open and put text in a new line above current line, until<Esc> hit

:0<Return> or 1G move cursor to first line in file

:n<Return> or nG move cursor to line n

:$<Return> or G move cursor to last line in file

Screen Manipulation

The following commands allow the vi editor screen (or window) to move up or down several lines and to be refreshed.

Adding, Changing, and Deleting Text

Unlike PC editors, you cannot replace or delete text by highlighting it with the mouse. Instead use the commands in the following tables.Perhaps the most important command is the one that allows you to back up and undo your last action. Unfortunately, this command acts like a toggle, undoing and redoing your most recent action. You cannot go back more than one step.

* u UNDO WHATEVER YOU JUST DID; a simple toggle

The main purpose of an editor is to create, add, or modify text for a file.

Inserting or Adding Text

The following commands allow you to insert and add text. Each of these commands puts the vi editor into insert mode; thus, the <Esc> key must be pressed to terminate the entry of text and to put the vi editor back into command mode.

Page 20: Foc Lab Manual

/string search forward for occurrence of string in text

?string search backward for occurrence of string in text

n Move to next occurrence of search string

N Move to next occurrence of search string in opposite direction

Deleting Text

The following commands allow you to delete text.

* x delete single character under cursor

Nx delete N characters, starting with character under cursor

dw delete the single word beginning with character under cursor

dNwdelete N words beginning with character under cursor;e.g., d5w deletes 5 words

Ddelete the remainder of the line, starting with current cursor position

* dd delete entire current line

Ndd or dNddelete N lines, beginning with the current line;e.g., 5dd deletes 5 lines

Cutting and Pasting TextThe following commands allow you to copy and paste text.

yy copy (yank, cut) the current line into the buffer

Nyy or copy (yank, cut) the next N lines, including the current line, into the buffer

P put (paste) the line(s) in the buffer into the text after the current line

Other Commands

Searching Text

A common occurrence in text editing is to replace one word or phase by another. To locate instances of particular sets of characters (or strings), use the following commands.

Page 21: Foc Lab Manual

:.= Returns line number of current line at bottom of screen

:= Returns the total number of lines at bottom of screen

^gprovides the current line number, along with the total number of lines,in the file at the bottom of the screen

Determining Line Numbers

Being able to determine the line number of the current line or the total number of lines in the file being edited is sometimes useful.

Saving and Reading FilesThese commands permit you to input and output files other than the named file with which you are currently working.

:r filename<Return>read file named filename and insert after current line(the line with cursor)

:w<Return> write current contents to file named in original vi call

:w newfile<Return> write current contents to a new file named newfile

:12,35w smallfile<Return>write the contents of the lines numbered 12 through 35 to a new file named smallfile

:w! prevfile<Return>write current contents over a pre-existing file namedprevfile

Page 22: Foc Lab Manual

Ex .No: 4 CALCULATING THE GROSS SALARY OF AN EMPLOYEE

AIM:

To write a simple script to calculate the gross salary of an employee.

ALGORITHM:

STEP 1 : Start

STEP 2 : Get the basic salary from the user

STEP 3 : Calculate the Da , Hra, and gross with the needed formulas namely da=`expr $b \* 10 / 100`hra=`expr $b \* 20 / 100`

gross=`expr $b + $da + $hra`

STEP 4 : Print the gross salary by use of echo command

STEP 5 : Stop

Page 23: Foc Lab Manual

PROGRAM

echo “Calculating the gross salary of an employee”

echo “Enter the Basic salary”

read b

da=`expr $b \* 10 / 100` hra=`expr $b \* 20 / 100` gross=`expr $b + $da + $hra` echo ‘Gross Salary=$gross`

OUTPUT

Calculating the gross salary of an employee

Enter the Basic salary

10000

Gross Salary= 13000

Result:Thus the program to find gross salary of an employee is created andexecuted successfully.

Page 24: Foc Lab Manual

Ex .No: 5 FINDING THE AREA OF A CIRCLE

AIM:

To write a simple script to find the area of a circle.

ALGORITHM:

STEP 1 : Start

STEP 2 : Get the radius from the user

STEP 3 : Read the radius.

STEP 4 : Calculate the area by using the formula 3.142857 \* $radius \* $radius | bc

STEP 5 : Stop

Page 25: Foc Lab Manual

PROGRAM

echo “FINDING THE AREA OF THE CIRCLE”

echo "Enter the radius of the circle" read radiusecho "The Area of the circle is"

echo 3.142857 \* $radius \* $radius | bc

OUTPUT

FINDING THE AREA OF THE CIRCLE

Enter radius of the circle

5

The Area of the circle is 78.571425

Result:

Page 26: Foc Lab Manual

Thus the program to find radius of circle was created and executed

successfully.

Page 27: Foc Lab Manual

Ex .No:6 TWO-WAY BRANCHING

AIM:

To implement the concept of two-way branching using “if ” statement.

ALGORITHM:

STEP1 :Start

STEP2 :Declare the variables and its access

STEP3 :If a is greater than b then print A is Big, else

STEP4 :Print B is Big

STEP5 :Stop

Page 28: Foc Lab Manual

Program

echo “Concept of two-way branching using if statement”

echo “ Finding the Biggest of two numbers”

echo “Enter the two Numbers”

read a b

if [ $a –gt $b ]

then

else fi echo “A is Big”

echo “B is Big”

OUTPUT

Concept of two-way branching using if statementFinding the Biggest of two numbers

Enter the two Numbers

12 45

B is Big

Result:

Thus the program to find biggest of given two numbers by using if statement

was created and executed successfully.

Page 29: Foc Lab Manual
Page 30: Foc Lab Manual

Ex .No: 7 MULTI-WAY BRANCHING USING “ IF-ELIF ” STATEMENT

AIM:

To implement the concept of multi-way branching using “ if-elif ” statement.

ALGORITHM:

STEP 1 : Start

STEP 2 : Declare the variables and its access

STEP 3 : If a is greater than b and a is greater than c, then print A is Big, else

STEP 4 : If b is greater than c then print B is Big, else

STEP 5 : Print C is Big

STEP6 : Stop

Page 31: Foc Lab Manual

Program

echo “Concept of multi-way branching using if-elif statement”echo “Finding the Biggest of three numbers”echo “Enter the three Numbers”echo a read a echo b= read b echo c= read cif [ $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

Concept of multi-way branching using if-elif statementFinding the Biggest of three numbersEnter the three Numbers a=14b=23 c=10B is Big

Result:

Page 32: Foc Lab Manual

Thus the program to find highest number among given three numbers by using

if-elif statement was developed and executed successfully.

Page 33: Foc Lab Manual

Ex .No: 8 MULTI-WAY BRANCHING USING “CASE” STATEMENT

AIM:

To implement the concept of multi-way branching using “ case ” statement.

ALGORITHM:

STEP 1 : Start

STEP 2 : Initialize the number of choices

STEP 3 : If the choice is present corresponding result will be displayed otherwise

STEP 4 : The statement of the default will be displayed

STEP 5 : Stop

Page 34: Foc Lab Manual

Program

echo “Concept of multi-way branching using case statement”

clear

echo “1. To know Your current Directory”

echo “2.Todays Date” echo “3.List of Users” echo “Enter your choice” read choicecase $choice in

1) pwd;;

2) 2)date;;

3) who;;

*) echo “Only give inputs from 1 to 3”

esac

OUTPUTConcept of multi-way branching using case statement1. To know Your current Directory2.Todays Date3.List of UsersEnter your choice2wed march 18 08:25:20 IST 2009

Page 35: Foc Lab Manual

Result: Thus the program for multiway branching by using case statement was createdand executed successfully.

Ex .No: 9 PROGRAM TO PRINT N NATURAL NUMBERS

AIM:

To write a program to print n Natural numbers

ALGORITHM:

STEP 1 : Start

STEP 2 : Get a number from the user to print n Natural numbers

STEP 3 : Read the number

STEP 4 : Start the while loop and write the statements of the loop within do and done

STEP 5 : Echo the value of i and increment the value of i

STEP 6 : End the loop

STEP 7 : Stop

Page 36: Foc Lab Manual

PROGRAM

echo “print n natural numbers”

echo “Enter a number to get N natural numbers”

read n

echo “The first $n natural numbers are”

i=1

while [ $i –le $n ]

do

done

echo $i

i=`expr $i + 1`

OUTPUTprint n natural numbers

Enter a number to get N natural numbers

5

The first 5 natural numbers are

12345

Page 37: Foc Lab Manual

Result: Thus the program to print n natural numbers was created by using while loop

and executed successfully.

Page 38: Foc Lab Manual

Ex .No: 10 PROGRAM TO PRINT FIRST N FIBONACCI SERIES.

AIM:

To write a program to print first N Fibonacci Series.

ALGORITHM:

STEP 1 : Start

STEP 2 : Get the number of terms of Fibonacci Series the user needs. STEP 3 : Read the number

STEP 4 : Initialize f1=-1,f2=1 and i=1

STEP 5 :Start the while loop and write the statements of the loop within do and done

STEP 6 : calculate the value of the Fibonacci Series with the needed formulas

STEP 7 : End the loop

STEP 8 : Stop

Page 39: Foc Lab Manual
Page 40: Foc Lab Manual

PROGRAM

echo Enter the number of terms read nf1=-1 f2=1 i=1echo The Fibonacci series upto $n terms is while [ $i -le $n ]do

done

f3=`expr $f1 + $f2`

echo $f3 f1=$f2 f2=$f3i=`expr $i + 1`

OUTPUTEnter the number of terms10The Fibonacci series upto 10 terms is0112358132134

Result: Thus the program to display Fibonacci series up to given terms was created by using while loop and executed successfully.

Page 41: Foc Lab Manual

Ex .No: 11 PRINT THE MULTIPLICATION TABLE USING UNTIL LOOP

AIM:

To write a program to print the multiplication table using until loop.

ALGORITHM:

STEP 1 : Start

STEP 2 : Get the number from the user to print its multiplication table.

STEP 3 : Read the number

STEP 4 :Start the until loop

STEP 5 : write the necessity statements within do and done

STEP 6 : End the loop

STEP 7 : print the multiplication table .

STEP 8 :Stop

Page 42: Foc Lab Manual

PROGRAM

echo “ Enter a number”

read n i=1echo “ The multiplication table of $n is”

until [ $i –gt 10 ]

do

done

pro=`expr $i \* $n` echo “ $n x $i=$pro” i=`expr $i + 1`

OUTPUT

Enter a number5The multiplication table of 5 is5 x 1=55 x 2=105 x 3=155 x 4=205 x 5=255 x 6=305 x 7=355 x 8=405 x 9=455 x 10=50

Page 43: Foc Lab Manual

Result: Thus the program for multiplication table was developed and executed successfully by until loop.

Page 44: Foc Lab Manual

Ex .No: 12 PROGRAM TO PRINT THE SUM USING FOR LOOP.

AIM:

To write a program to print the sum using for loop.

ALGORITHM:

STEP 1 : Start

STEP 2 : Initialize the variable i.e sum=0

STEP 3 : Start the for loop

STEP 4 : calculate the sum by using the formula sum=`expr $sum + $i` for all the elements in the for loop listSTEP 5 : End the loop.

STEP 6 : Print the value of the sum

STEP 7 : Stop

Page 45: Foc Lab Manual

Program

echo “ Program implementing For Loop”

sum=0for i in 1 2 3 4 10 do

done

sum=`expr $sum + $i`

echo “The sum is $sum”

OUTPUT

Program implementing For Loop

The sum is 20

Result:

Thus the program to find the sum of the numbers by using for loop was

developed and executed successfully.

Page 46: Foc Lab Manual

Ex .No: 13 PROGRAM TO CALCULATE THE FACTORIAL

AIM:

To write a program to calculate the factorial.

ALGORITHM:

STEP 1 : Start

STEP 2 : Get the number from the user

STEP 3 : Read the number

STEP 4 :Initialize the variable i.e f=1

STEP 5 : Start the for loop STEP 6 : calculate the factorial STEP 7 : End the loop.STEP 8 : print the factorial of the given number. STEP 9 : Stop

Page 47: Foc Lab Manual

Program

echo "Factorial Program" echo "Enter the number" read af=1

for (( i = 1; i <= ${a}; i++ ))

do

done

f=`expr $f \* $i`

echo "The factorial of $a is $f"

OUTPUT

Factorial Program

Enter the number5The factorial of 5 is 120

Result:

Thus the program to find factorial of given number was created and executed

successfully by using for loop.

Page 48: Foc Lab Manual

EX NO: 14 PROGRAM FOR THE ILLUSTARTION OF POINTER EXPRESSIONS

AIM:To develop the program for the illustration of pointers expressions.

ALGORITHM:

1. Open the UNIX operating system.

2. Edit the command vi filename in the home directory or in the directory created by user. VI stands for visual, a full screen editor, indicates most powerful editor. File name indicates program name3. Now enter required header files and open main ( )

function.

4. Inside main ( ) function declare required variables, pointers of integer data type.5. Give the value for the variables at compile or runtime. After given values, assign the address of variables to the pointers.6. Show the value of the variables and its stored address by pointers.7. Evaluate more than one expression by pointers and show the result.8. To save the program, press Escape button in the keyboard and enter :wq command.9. To compile the program edit the command cc filename.c10. If no compile time error, to run the program edit the command ./a. out.11. If no run time error, output of the program is displayed.

Page 49: Foc Lab Manual

Program

#include<stdio.h>main( )

{int a,b,*p1,*p2;printf(" ENTER THE VALUE FOR A,B : ");scanf("%d%d",&a,&b);p1=&a;p2=&b;printf(" \n %d is stored in the address %u ",*p1,p1); printf(" \n %d is stored in the address %u ",*p2,p2); printf(" \n (*p1+*p2)*(a -*p2) = %d", (*p1+*p2)*(a-*p2)); printf(" \n (*p1+*p2)/(*p1-b) = %d",(*p1+*p2)/(*p1-b)); printf(" \n *p1*p2/a+b = %d",*p1*p2/a+b); printf("\n");

}

OUTPUT

Enter the value for a,b: 12 8

12 is stored in the address 32184666768 is stored in the address 3218466672

(*p1+*p2)*(a-*p2) = 80(*p1+*p2)/(*p1-b) = 5

*p1**p2/a+b = 16

RESULT:

Thus the program for the illustration of pointer expressions is

developed, executed successfully

Page 50: Foc Lab Manual

EX NO: 15 PROGRAM FOR DYNAMIC MEMORY ALLOCATION AND REALLOCATION

Page 51: Foc Lab Manual

AIM:

To develop the program for dynamic memory allocation to store

information and the reallocating dynamically the same memory to store more information .

ALGORITHM:

1. Open the UNIX operating system.2. Edit the command vi filename in the home directory or in the

directory created by user. VI stands for visual, a full screen editor, indicates most powerful editor. File name indicates program name.

3. Now enter required header files and open main ( )function.

4. Inside main ( ) function declare required variables, pointers and allocate a block of memory dynamically by using malloc ( ) function.

5. Check whether dynamically a block of memory allocated or not.

6. If allocated, store the information and display the information.

7. Reallocate the same memory to store more information than before by using realloc ( ) function.

8. Check whether reallocation of same memory dynamically done or not.

9. If allocated, store the new information and display it.10. If no longer needed the stored information, release by

using free ( ) function.11. To save the program, press Escape button in the

keyboard and enter :wq command.12. To compile the program edit the command cc

filename.c13. If no compile time error, to run the program edit the

command ./a. out.

14. If no run time error, output of the program is displayed.

Page 52: Foc Lab Manual

Program

#include<stdio.h>#include<stdlib.h>#include<string.h>main( ){

char *buffer;buffer=(char *)malloc(10*sizeof(char));if(buffer==0){

printf(" Dynamically memory not allocated . . . \n");exit(1);

}else

printf(" Dynamically memory allocated . . . \n");strcpy(buffer,"Chennai");printf(" Buffer contains : %s \n",buffer);buffer=realloc(buffer,50);if(buffer==0)

{printf(" Reallocation failed . . . \n");exit(1);

}printf(" Buffer size modified . . .\n");printf(" Buffer still contains: %s \n",buffer); strcpy(buffer,"Chennai is the capital of the Tamilnadu state "); printf(" Buffer now contains: %s \n",buffer);printf(" Releasing dynamically allocated memory . . . . ");free(buffer);}

Page 53: Foc Lab Manual

.

I OUTPUT

Dynamically Memory Allocated

Buffer contains : chennai Buffer size modified . . . Buffer still contains: chennai

Buffer now contains: chennai is the capital of Tamilnadu stateReleasing dynamically allocated memory . . .

RESULT:

Thus the program for dynamically memory allocation, reallocation and

releasing the allocated memory dynamically is developed and executed successfully.

Page 54: Foc Lab Manual

EX NO: 16 Program to create file, to store information in the file and to show the information in the file by using file handling functions.

AIM:

To develop the program to create file, to store information in the

file and to show the information in the file by using file handling functions.

ALGORITHM:1. Open the UNIX operating system.2. Edit the command vi filename in the home directory or in the

directory created by user. VI stands for visual, a full screen editor, indicates most powerful editor. File name indicates program name.

3. Now enter required header files and open main ( )function.

4. Inside main ( ) function declare required variables,file pointer. Open the file with write mode and assign to the

file pointer.5. Enter number of students information to store.6. Through for( ) loop, read the one by one student

information by using stdin keyword, represents the keyboard and write to the given file by file pointer.

7. Close the file using fclose ( ) function with file pointer as argument.

8. Again open the file with read mode and assign to the file pointer.

9. Again through same for( ) loop, read one by one student information from the file by file pointer and write to the screen by using stdout keyword, represents screen.

10. For reading use fscanf ( ) function and for writing fprintf ( )function.

11. Close the file using fclose ( ) function with file pointer as argument.

12. To save the program, press Escape button in the keyboard and enter :wq command.

13. To compile the program edit the command cc filename.c

14. If no compile time error, to run the program edit the command ./a. out.

15. If no run time error, output of the program is displayed.

Page 55: Foc Lab Manual

Program

#include<stdio.h>main( ){

FILE *fp;char studname[15];int n, i,em,tm,mm,sm.scm,total; float avg; fp=fopen("studentdetails","w");printf(" ENTER NUMBER OF STUDENTS : ");scanf("%d",&n);

printf("ENTER STUDENT NAMES AND FOLLOWING SUBJECT MARKS . \n \n");

printf("ENGLISH TAMIL MATHS SCIENCE SOCIALSCIENCE \n \n");

for(i=1;i<=n;i++){

printf(" %d: STUDENT DETAILS \n ",i);fscanf(stdin,"%s%d%d%d%d%d",studname,&em,&tm,&mm,&sm,&scm);

fprintf(fp,"%s%d%d%d%d%d",studname,em,tm,mm,sm,scm); printf("\n");}

fclose(fp);fp=fopen("studentdetails","r");printf(" STUDENT DETAILS STORED IN THE FILE . . . \n \n");

for(i=1;i<=n;i++){

fscanf(fp,"%s%d%d%d%d%d",studname,&em,&tm,&mm,&sm,&scm);total=em+tm+mm+sm+scm;avg=total/5;fprintf(stdout,"%s\n%d\n%d\n%d\n%d\n%d\n%d\n%f",studname,em,tm,mm,sm,scm,total,avg);printf("\n");

}fclose(fp);

}

Page 56: Foc Lab Manual

OUTPUT

ENTER NUMBER OF STUDENTS: 2

ENTER STUDENT NAMES AND FOLLOWING SUBJECT MARKS . .

ENGLISH TAMIL MATHS SCIENCE SOCIALSCIENCE

1: STUDENT DETAILS Gnanasekar.P48 78 90 76 56

2: STUDENT DETAILS Visu.L70 89 76 65 54

STUDENT DETAILS STORED IN THE FILE . . .

Gnanasekar.P487890765634869.6

Visu.L708976655435470.8

RESULT:

Thus the program for file handling by using file handling functions is

developed and executed successfully.

Page 57: Foc Lab Manual
Page 58: Foc Lab Manual
Page 59: Foc Lab Manual