fundamentals of computer

77
G.K.M COLLEGE OF ENGINEERING AND TECHNOLOGY CHENNAI-63 COMPUTER SCIENCE AND ENGINEERING DEPARTMENT LABORATORY MANUAL COMPUTER PRACTICE LABORATORY-II II SEMESTER 2008-2009

Upload: csetube

Post on 18-Nov-2014

161 views

Category:

Documents


1 download

DESCRIPTION

foc lab manualfrom gkm college

TRANSCRIPT

Page 1: fundamentals of computer

G.K.M COLLEGE OF ENGINEERING AND TECHNOLOGY

CHENNAI-63

COMPUTER SCIENCE AND ENGINEERING DEPARTMENT

LABORATORY MANUAL

COMPUTER PRACTICE LABORATORY-II

II SEMESTER

2008-2009

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00

Page 2: fundamentals of computer

APPROVAL

The MANUAL of (GE 2155) COMPUTR PRACTICE LABORATORY- II

UNDER COMPUTR PRACTICE LABORATORY- II IN II SEMESTER

OF I YEAR (2008-2009) .

PREPARED BY APPROVED BY

1. Ms.S.Sudha. Mr.M.Babu.2. Mr.M.Karthikeyan. HOD/CSE. 3. Mr.P.Selvakumar.4. Mr.E.Senthilkumaran.5. Mr.M.Manickam.6. Mr.N.Kalyanasundaram.7. Mr.Amit Prasad.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00

Page 3: fundamentals of computer

I N D E X - II N D E X - I

S.NOS.NO NAME OF THE EXPERIMENTNAME OF THE EXPERIMENT PAGE NOPAGE NO11 STUDY OF UNIX OPERATING SYSTEM. 11

22 STUDY OF BASIC UNIX COMMANDS. 88

33 STUDY OF VI EDITORS. 1414

4A4A CALCULATING THE GROSS SALARY OF

AN EMPLOYEE.

2121

4B4B FINDING THE AREA OF A CIRCLE. 2323

5A5A TO IMPLEMENT THE CONCEPT OF

TWO-WAY BRANCHING USING IF

STATEMENT.

2525

5B5B TO IMPLEMENT THE CONCEPT OF

MULTI-WAY BRANCHING USING IF-

ELIF STATEMENT.

2727

5C5C TO IMPLEMENT THE CONCEPT OF

MULTI-WAY BRANCHING USING CASE

STATEMENT.

2929

6A6A TO WRITE A PROGRAM TO PRINT N

NATURAL NUMBERS.

3131

6B6B TO WRITE A PROGRAM TO PRINT FIRST

N FIBONACCI SERIES.

3333

6C6C TO WRITE A PROGRAM TO PRINT THE

MULTIPLICATION TABLE USING UNTIL

LOOP.

3535

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00

Page 4: fundamentals of computer

I N D E X -I N D E X - IIII

S.NOS.NO NAME OF THE EXPERIMENTNAME OF THE EXPERIMENT PAGE NOPAGE NO6D6D TO WRITE A PROGRAM TO PRINT THE

SUM USING FOR LOOP.

3737

6E6E TO WRITE A PROGRAM TO

CALCULATE THE FACTORIAL.

3939

77 PROGRAM TO FIND INVERSE OF THE

GIVEN NUMBER AND FIBONACCI

SERIES UP TO NEEDED LEVEL USING

FUNCTIONS.

4141

88 PROGRAM FOR THE ILLUSTRATION

OF POINTERS EXPRESSIONS.

4444

99 PROGRAM FOR DYNAMIC MEMORY

ALLOCATION, REALLOCATION AND

DELETING.

4646

1010 PROGRAM TO CREATE FILE, TO

STORE INFORMATION IN THE FILE

AND TO SHOW THE INFORMATION IN

THE FILE BY USING FILE HANDLING

FUNCTIONS.

4949

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00

Page 5: fundamentals of computer

EX NO: 1 STUDY OF UNIX OPERATING SYSTEMEX NO: 1 STUDY OF UNIX OPERATING SYSTEM

AIM:AIM: To study about unix operating system.To study about unix operating system.

WHAT IS UNIX

UNIX is an operating system which was developed first in the

1960’s and has under constant development ever since by operating

system we mean the suite of programs which make the computer

work. It is a stable, multi-user, multi-tasking operating system for

servers, desktops and laptops.

UNIX operating system also have graphical user interface (GUI)

similar to Microsoft windows operating system which provides an

easy to environment. However knowledge of unix is required for

operations which are not covered by graphical program or for when

there is no windows interface available.

TYPES OF UNIX

There are many different versions of unix, although they

share the common similarit ies. The most popular varieties of unix

are Sun Solaris, GNU/Linux and Macos X.

THE UNIX OPERATING SYSTEM

The Unix Operating system is made up of three parts

1. The Kernel

2. The Shell

3. The Programs

THE KERNEL

The Kernel of Unix is the hub of the operating system. It

al locates t ime and memory to programs and handles the fi le store

and communication in response to system calls.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 1 OF 51

Page 6: fundamentals of computer

As an i l lustration of the way that the shell and the kernel work

together, suppose a user types ‘rm myfi le’ (which has the effect of

removing the fi le myfi le). The shell scratches the fi lestore for the f i le

containing the program ‘rm’. And then requests the kernel, through

system calls to ‘rm myfi le’ f inished running, the shell then returns

the unix prompt ‘%’ to the user, indicating execute the program ‘rm’

on ‘myfi le’. When the process that it is wait ing for future commands.

THE SHELL

The shell acts as an interface between the user and the

kernel. When user login, the login program checks the username and

password, and then starts another program called the shell. The

shell is a command l ine interpreter (CLI). It interprets the commands

the user types in an arranges for them to be carried out. The

commands are themselves programs. When they terminate, the shell

gives the user another prompt (% on our system). By typing part of

the name of a command, f i lename and directory and pressing the

[tab] key, the shell wil l complete the rest of them automatically. If

shell f inds more than one name beginning with those letters you

have typed, it wil l beep, prompting to type a few more letters before

pressing the tab key again.

The shell keeps the l ist of the commands you have typed in, i f you

need to repeat a command, use the cursor key to scroll up and down

the l ist or type history for a l ist of previous commands. Different

users may use different shells. Init ial ly, your system administrator

wil l supply a default shell, which can be overridden or changed.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 2 OF 51

Page 7: fundamentals of computer

The most commonly available shells are:

Bourne shell (sh)

C shell (csh)

Korn shell (ksh)

TC Shell (tcsh)

Bourne Again Shell (bash)

Each shell also includes its own programming language.

Command fi les, called "shell scripts" are used to accomplish a

series of tasks.

Uti l i t ies :UNIX provides several hundred uti l i ty programs, often

referred to as commands.

Accomplish universal functions

editing

f i le maintenance

printing

sorting

programming support

online info

Modular: single functions can be grouped to perform more

complex tasks.

The Bourne shell is one of the oldest shells and is the most

eff icient for background work.   However it provides few facil i t ies for

interactive users.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 3 OF 51

Page 8: fundamentals of computer

The C shell provides sophisticated interactive capabil i t ies lacking

in the Bourne shell.   Features of this shell include a command

history buffer, command aliases and fi le name

completion.  The C shell has a syntax which resembles the C

programming language. 

 The C shell is the default shell for interactive work on many UNIX

systems.  It wil l be covered in this document, although most of the

basic commands given here are relatively standard across all the

main shells.

The Korn shell was written by David Korn from AT&T and in it he

attempted to merge the preferred features of both the Bourne and C

shells as well as adding some addit ional features.

Unfortunately the Korn shell was not available for free, as other

UNIX shells were, so many users and companies did not chose to

use of i t .

The Bash shell was based on the Bourne shell (Bourne again

shell) and as with Korn it attempted to combine the best features of

the other shells which were available at the t ime. This shell however

was available for free.

Bash was init ial ly adopted for LINUX although several varieties of

LINUX now exist e.g. RedHat,   SuSE and Debian-GNU.

FILES AND PROCESS

Everything in unix is either a f i le or a process. A process

is an executing program identif ied by a unique PID (Process

Identif ier).

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 4 OF 51

Page 9: fundamentals of computer

A f i le is collection of data’s. They are created by users using text

editors, running, compilers etc.

Examples of f i les

Document

The text of a program written in some high level language.

All f i les are grouped together in the directory structure. The fi le

system is arranged in a hierarchical structure, l ike inverted tree. The

top of the hierarchy is tradit ionally called root (written [/] slash)

FEATURES OF UNIX

1. Multitasking

Multitasking is the capabil i ty of the operating system to

perform various tasks simultaneously. I.e. A single user can run

multiple programs (tasks) concurrently.

2. Multi-user Capabil i ty

Multi-user capabil i ty of UNIX allows several users to use the

same computer to perform their tasks. Several terminals (keyboards

and monitors) are connected to a single powerful computer (Unix

server) and each user can work with their terminals.

3. Security

Unix allows sharing of data. Every user must have a login

name and a password. So, accessing another user’s data is

impossible without permission.

I.e. Invalid users cannot access data.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 5 OF 51

Page 10: fundamentals of computer

4. Portabil i ty

UNIX is portable because it is written in a high level language.

So, UNIX can be run on different computers.

5. Communication

UNIX supports the fallowing communications

o Between the different terminals connected to the UNIX

server.

o Between the users of one computer to the users of

another computer located elsewhere in the network.

6. Programming facil i ty

UNIX is highly programmable, the UNIX shell programming

language has all necessary ingredients l ike condit ional and control

structure (Loops) and variables, that establish it as a programming

language in its own right.

ADVANTAGES OF UNIX

UNIX is a very stable operating system.

UNIX supports the Multi-User feature.

UNIX is a Multitasking operating system.

UNIX can be loaded to any type of computer hardware.

UNIX is optimized for program development.

UNIX has rich set of small commands and uti l i t ies that do

specif ic tasks.

UNIX has a powerful unif ied f i le system, everything in a f i le;

data, program and all physical devices.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 6 OF 51

Page 11: fundamentals of computer

UNIX has the abil i ty to string commands and uti l i t ies together

in unlimited ways to accomplish more complicated tasks.

UNIX allows only authorized users to modify f i les and

directories.

UNIX allows only System Administrators to make changes in

System Configuration f i les.

RESULT:

Thus study of unix operating system was completed.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 7 OF 51

Page 12: fundamentals of computer

EX NO: 2 STUDY OF BASIC UNIX COMMANDS

AIM:To study about basic UNIX commands in UNIX

GENERAL COMMANDS

NO COMMAND SYNTAX VARIANCE/OPTIONS DESCRIPTION1 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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 8 OF 51

Page 13: fundamentals of computer

FILE MANAGEMENT COMMANDSNO COMMAND SYNTAX VARIANCE/

OPTIONSDESCRIPTION

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 within the 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.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 9 OF 51

Page 14: fundamentals of computer

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

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 of your root directory.ls ../ List the contents of the parent directory.ls */ List the contents of all sub directories.ls -d */ Only list the directories in the current directory.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 10 OF 51

Page 15: fundamentals of computer

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.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 11 OF 51

20 Chmod chmod + for adding chmod changes

Page 16: fundamentals of computer

filename users + permissions file/directory

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

the permissions of each given file according to the mode specified

DIRECTORY MANAGEMENT COMMANDS

NO COMMAND SYNTAX VARIANCE/OPTIONS DESCRIPTION

21 Mkdir Mkdir dirname

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 cdcd ~

Moves to the home directory

cd ..cd -

Moves to the parent directoryMoves to the previous directory

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 12 OF 51

Page 17: fundamentals of computer

RESULT: Thus basic UNIX commands in UNIX was studied.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 13 OF 51

EX:NO:3 VI EDITOR

Page 18: fundamentals of computer

AIM: To Study of Basic vi Commands

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, and 2. 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.

* vi filename edit filename starting at line 1

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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 14 OF 51

To Exit vi

Page 19: fundamentals of computer

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.

* :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

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.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 15 OF 51

Page 20: fundamentals of computer

* 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

  :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.

  ^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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 16 OF 51

Page 21: fundamentals of computer

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.

* 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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 17 OF 51

Page 22: fundamentals of computer

Changing Text

The following commands allow you to modify text.

* r replace single character under cursor (no <Esc> needed)

  R replace characters, starting with current cursor position, until <Esc> hit

  cw change the current word with new text, starting with the character under cursor, until <Esc> hit

  cNw change N words beginning with character under cursor, until <Esc> hit;   e.g., c5w changes 5 words

  C change (replace) the characters in the current line, until <Esc> hit

  cc change (replace) the entire current line, stopping when <Esc> is hit

  Ncc or cNc change (replace) the next N lines, starting with the current line,stopping when <Esc> is hit

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

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

  D delete the remainder of the line, starting with current cursor position

* dd delete entire current line

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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 18 OF 51

Page 23: fundamentals of computer

Cutting and Pasting Text

The following commands allow you to copy and paste text.

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

  Nyy or yNy 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.

  /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

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.

  :.= 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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 19 OF 51

Page 24: fundamentals of computer

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 named prevfile

RESULT: Thus the commands related to vi Editor was studied.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 20 OF 51

Page 25: fundamentals of computer

Ex .No: 4A 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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 21 OF 51

Page 26: fundamentals of computer

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 and executed successfully.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 22 OF 51

Page 27: fundamentals of computer

Ex .No: 4 B 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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 23 OF 51

Page 28: fundamentals of computer

PROGRAM

echo “FINDING THE AREA OF THE CIRCLE”

echo "Enter the radius of the circle"

read radius

echo "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: Thus the program to find radius of circle was created and executed successfully.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 24 OF 51

Page 29: fundamentals of computer

Ex .No:5 A 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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 25 OF 51

Page 30: fundamentals of computer

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

echo “A is Big”

else

echo “B is Big”

fi

OUTPUT Concept of two-way branching using if statement

Finding 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.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 26 OF 51

Page 31: fundamentals of computer

Ex .No: 5 B 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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

Page 32: fundamentals of computer

ISSUE: B REV: 00 PAGE NO: 27 OF 51

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 aecho b=read becho c=read cif [ $a –gt $b ]then if [ $a –gt $c ] then echo “A is Big” else echo “ C is Big” fielif [ $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 Numbersa=14 b=23 c=10B is Big

Result: Thus the program to find highest number among given three numbers by using if-elif statement was developed and executed successfully. .

ISO/CSE/MANUAL/CP LBII – GE 2155

Page 33: fundamentals of computer

ISSUE: B REV: 00 PAGE NO: 28 OF 51

Ex .No: 5C 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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 29 OF 51

Page 34: fundamentals of computer

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 choice

case $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

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

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 30 OF 51

Page 35: fundamentals of computer

Ex .No: 6 A 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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 31 OF 51

Page 36: fundamentals of computer

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

echo $i

i=`expr $i + 1`

done

OUTPUT

print n natural numbers

Enter a number to get N natural numbers

5

The first 5 natural numbers are

1

2

3

4

5

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

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 32 OF 51

Page 37: fundamentals of computer

Ex .No: 6B 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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

Page 38: fundamentals of computer

ISSUE: B REV: 00 PAGE NO: 33 OF 51

PROGRAM

echo Enter the number of terms

read n

f1=-1

f2=1

i=1

echo The Fibonacci series upto $n terms is

while [ $i -le $n ]

do

f3=`expr $f1 + $f2`

echo $f3

f1=$f2

f2=$f3

i=`expr $i + 1`

done

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. .

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 34 OF 51

Page 39: fundamentals of computer

Ex .No: 6C 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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

Page 40: fundamentals of computer

ISSUE: B REV: 00 PAGE NO: 35 OF 51

PROGRAM

echo “ Enter a number”

read n

i=1

echo “ The multiplication table of $n is”

until [ $i –gt 10 ]

do

pro=`expr $i \* $n`

echo “ $n x $i=$pro”

i=`expr $i + 1`

done

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

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

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 36 OF 51

Page 41: fundamentals of computer

Ex .No: 6D 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 list

STEP 5 : End the loop.

STEP 6 : Print the value of the sum

STEP 7 : Stop

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 37 OF 51

Page 42: fundamentals of computer

Program

echo “ Program implementing For Loop”

sum=0

for i in 1 2 3 4 10

do

sum=`expr $sum + $i`

done

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.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 38 OF 51

Page 43: fundamentals of computer

Ex .No: 6E 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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 39 OF 51

Page 44: fundamentals of computer

Program

echo "Factorial Program"

echo "Enter the number"

read a

f=1

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

do

f=`expr $f \* $i`

done

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.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 40 OF 51

Page 45: fundamentals of computer

EX NO: 7 Program to find inverse of the given number and Fibonacci series

up to needed level using functions

AIM: To develop the program to find inverse of the given number and

Fibonacci series up to needed level using 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, declare

two functions. One function to find inverse of given number

with return type and one argument of integer data type.

5. Another function for Fibonacci series with no return type

and one argument of integer data type.

6. Assign the values to given variables at compile or runtime.

7. Call the two functions inside main ( ) function.

8. Close main ( ) function. Define two functions outside

main ( ) function.

9. To save the program, press Escape button in the keyboard

and enter :wq command.

10.To compile the program edit the command cc file name.c.

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

command ./a. out.

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

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 41 OF 51

Page 46: fundamentals of computer

Program #include<stdio.h> main( ) { int n1,n2; printf(" ENTER THE VALUE FOR N1: "); scanf("%d",&n1); printf(" INVERSE OF THE GIVEN NUMBER %d = %d",n1,inverse(n1)); n2=10; printf("\n"); printf(" FIBONACCI SERIES . . . . \n "); fibo(n2); } inverse(int n) { int a,d; d=0; while(n>1) { a=n%10; d=d*10+a; n =n/10; } return d; } fibo(int m) { int a,b,c,i; a=0;b=1; for(i=1;i<=m;i++) { if(i==1) printf("%d\t",a); else if(i==2) printf("%d\t",b); else { c=a+b; printf("%d\t",c); a=b; b=c; }}} .

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 42 OF 51

Page 47: fundamentals of computer

OUTPUT

ENTER THE VALUE FOR N1: 7865 INVERSE OF THE GIVEN NUMBER 7865 = 5687 FIBONACCI SERIES . . . . . 0 1 1 2 3 5 8 13 21 34

RESULT: Thus the program to find inverse of the given number and Fibonacci series by using functions is developed and executed successfully.

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 43 OF 51

Page 48: fundamentals of computer

EX NO: 8 Program for the illustration of pointers 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 name

3. 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.c

10. 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: fundamentals of computer

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 44 OF 51

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 3218466676 8 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: fundamentals of computer

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 45 OF 51

EX NO: 9 Program for dynamic memory allocation, reallocation and deleting.

AIM:

To develop the program for dynamic memory allocation to store

information and the reallocating dynamically the same memory to store more

information .After the deleting same memory dynamically by dynamic memory

allocation 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, 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.

Page 51: fundamentals of computer

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

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 46 OF 51

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 52: fundamentals of computer

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 47 OF 51

OUTPUT Dynamically Memory Allocated Buffer contains : chennai Buffer size modified . . . Buffer still contains: chennai Buffer now contains: chennai is the capital of Tamilnadu state Releasing dynamically allocated memory . . .

RESULT: Thus the program for dynamically memory allocation, reallocation and releasing the allocated memory dynamically is developed and executed successfully.

Page 53: fundamentals of computer

.

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 48 OF 51

EX NO: 10 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.

Page 54: fundamentals of computer

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

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 49 OF 51

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 name and fallowing 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); }

.

ISO/CSE/MANUAL/CP LBII – GE 2155

Page 55: fundamentals of computer

ISSUE: B REV: 00 PAGE NO: 50 OF 51

OUTPUT ENTER NUMBER OF STUDENTS: 2 ENTER STUDENT NAMES AND FALLOWING SUBJECT MARKS . . ENGLISH TAMIL MATHS SCIENCE SOCIALSCIENCE 1: STUDENT DETAILS Gnanasekar.P 48 78 90 76 56 2: STUDENT DETAILS Visu.L 70 89 76 65 54 STUDENT DETAILS STORED IN THE FILE . . . Gnanasekar.P 48 78 90 76 56 348 69.6 Visu.L 70 89 76 65 54 354 70.8

RESULT: Thus the program for file handling by using file handling functions is developed and executed successfully. .

Page 56: fundamentals of computer

ISO/CSE/MANUAL/CP LBII – GE 2155

ISSUE: B REV: 00 PAGE NO: 51 OF 51