You are on page 1of 48

Ex.

no:1 Date: AIM:

BASIC UNIX COMMANDS

To write the various UNIX basic commands for performing various operations. COMMANDS: The commands are classified into 15 Categories 1. User access and user id commands 2. Directory Commands 3. Editor Commands 4. File Manipulation Commands 5. Security and Protection Commands 6. Interuser and Intermachine Communication Commands 7. Information Commands 8. Process Management Commands 9. Program development and Debugging Commands 10. Text Processing Commands 11. I/O Redirection Commands 12. Shell Commands and Shell Programming 13. Invoking Command Interpreters 14. System Administration Commands 15. UNIX enhancements in different version and release 1. User access and user id commands These commands are used to obtain permission and authorization from the system administrator to get a directory automated to him. The important commands are given below. i) Login - log into the system ii) Logout - logout of the system iii) Password - set the users password or change the earlier login password 2. Directory Commands These commands are used to create, remove and change from one to another i)mkdir These commands are used to create a directory Syntax - $ mkdir directory name
1

Example -$ mkdir directory exam1 ii)rmdir These commands are used to remove or delete a directory Syntax - $ rmdir directory name Example -$ rmdir directory exam1 iii)cd Change from current directory to a new working directory. new directory name Syntax $cd directory name Example - $cd Exam exam1 iv) ls -List the files directory are subdirectories contained in a

Syntax $ls v) pwd vi) mv Print or display the path of the present working directory. Syntax $pwd Rename a directory and copy Syntax $ mv source directory name new directory name Example - $ mv Exam exam1 vii) find viii) cat It is used to scan directories are locate a file. The cat command is used to create a file. Syntax $ find Syntax $ cat >filename Example - $ cat >exam1 3. Editor Commands It is used for execution of specific programs written in high level language. Three main editors: Command a. vi b. ed c. fed 4. File Manipulation Commands i) cp copy a file Syntax - $ cp filename new filename Example - $ cp exam exam1 ii) mv Move a file from one directory to another or to change the name of the file. Syntax - $ mv filename new filename Example - $ mv exam exam1 iii) rm Remove a file or delete a file
2

Function a screen editor a line editor a stream editor

The file is created the user to copy, rename, delete or view the contents of the file.

Syntax - $ rm filename Example - $ rm exam iv) sort Sort a file content Syntax - $ sort filename Example - $ sort exam v) pg - check the contents of a file one screen at a time, just like more. vi) lp - print the contents of a file through a printer vii) pr - print files with title, date and page number ix) file find the type of data in a file x) find searches and locates a file based on the owner access permission created date, name exc. xi) more The same as pg-view contents of a file one screen at a time. 5. Security and Protection Commands Being a multiuser operating system UNIX ensures security and protection of files/ directories created by a user or a group. It protects the file from unauthorized access. Whenever a file or directory is created, UNIX automatically provides default protection for read, write and execute and related permission for owners groups and others. The commands available are Command chmod chown chgrp newgrp Function Change file protection mode Change owner of a file Change file group Change to default mode

6. Inter user and Inter Machine Commands The user can communicate from his terminal to another user in the system irrespective of whether the other user is logged in the system or not. Command write mail rmail mesg-yln uucp Function Send a message to one who has logged in Send mail or messages to one logged in or not. Remote mail to be displayed on the screen Permit or deny messages to be displayed on the screen Unix to Unix copy.
3

uulog uusend uux cu uuencode/ uudecode news wall 7. Information Commands

Print log of uucd actions Send a file to remote host Unix to Unix command execution Call Unix connect to a remote system Encode/decode file formatting Send messages to all the users Write into all terminals logged in

Necessary commands to carry on day to day activities such as Command learn man who data cal tty calender Function Self- Learning instructions about Unix or a command. Detailed information about various commands and their proper usage. List of current users logged in the system. Print and set the data. Print a calendar for any year and month. Show the reference and name of terminal. Diary of appointments as a remainder service.

8. Process Management Commands Unix provides information on status of various processes controls the time priority for execution and suspension of various commands and processes. Command at ps kill wait sleep time nice batch Function Execute a command at a given time Print the status of various processes Stop or terminate a process Wait for background program Suspend execution for an interval Time a command Set command execution priority Execute a command on its own when the system is free

9. Program Development and Debugging Commands Compilers of high level languages are available in UNIX the compilation and linking of source programs can be achieved with simple commands. If there are errors in the source
4

programs, they are indicated by the system while compiling UNIX also provides a set of debugging tools. Command cc f77 cb int struct as pas bas id rmcab make adb sdb lex yacc Function Invoke a c complier Invoke a fortran compiler C program beautifier Verify a program Structure FORTRAN program Invoke assembler Invoke pascal compiler Invoke Basic compiler Loader and link editor Invoke RmCOBOL Compiler Maintain 1 update and regenerate group of programs Absolute debugger Symbolic Debugger Lexical Analyser Yet another Compiler

10. Test Processing Commands This command provides facilities for creating texts, formatting them and typesetted. With the advent of PCS ( Dos based) with word processing packages and software for desktop publishing UNIX utilities once used in earlier stages for document processing has new eroded 11. I/O Redirection and Piping Commands UNIX provides as a default device a terminal of the user for input and output. UNIX has a provision in its command structure for the user to redirect the input to be taken from files or a device and to direct the output to any file or device. These are called Redirection Command. The output of a command can be further processed for the desired end result using the symbol | (pipe) eliminating the need for an intermediate or temporary storage. Command > < >> Function Output to be stored in the directed file and net on the user terminal. Input to be taken from a file The output to be appended to the end of the file content and not overwrite.
5

| tee

To pass the effects of a command for further processing by a second command. To direct the output to file as well as to the screen.

12. Shell commands and Shell Programming UNIX commands are interpreted by the shell and executed by the kernel UNIX provides customized commands with the help of shell programming. This program can be executed by the kernel. The shell program consists of a set of command statements loop and control statements and case statements. Keywords Shell variable Untildodone Ifthen..elseif Eq,ne,it,le,gt,ge +a1 0 Case .esac Continue .break Function Alpha numeric or character constant to represent numeric values or strings. Loop to be executed if the condition is satisfied Logical and relational conditions decide the set of statements for execution. Repeating the loop so long as well while condition remaining the same. Select one of a number of alternative command sequences to be executed. Shorten or abandon the execution of for and while commands.

13. Invoking command interpreters: There are two types of shell the bourne shell and a shell commands in both the shells are more or less the same. District difference in syntax exists when it comes to shell programming the shell can be invoked by typing sh for bourne shell and csh for c shell at the format. 14. System Administration Commands: The user has to himself carry out the process of booting and also perform certain minimum functions of system administration. Keywords Function Su or root with For logging in as super user and getting the system prompt #. All password the commands are given at this prompt by the system administration. date Enter the current date. fsck Check the integrity of the file system and providing permission to repair system.

init2 kill sync shutdown mkfs,mouni,mount dump,restor,tarcpio adduser, rmuser,chgrp acct

Switch over from single user send a messages to all users informing system shutdown or any other such announcement Kill any running processes Forces Unix to write all information in buffers and memory to the disc immediately Sync automatically and shutdown the system Mounting and un mounting devices of the time of startup or shutdown or while using them and making file system on them. Take backup on tapes and flopping and also to retrieve Add or Delete users. Change group and ownership of files Maintain and print individuals account of users covering the login time usage time, memory and hard disc space used lines printed etc. Setup communication with other computer.

Uucp

15. UNIX Enhancements: While releasing UNIX versions generally in conformities with UNIX release 3,2 certain companies provides additional features and facilities to the operating system and call them as Enhancements to UNIX. OUTPUT DIRECTORY COMMANDS Creating the directory [EEE@localhost]$ mkdir cse [EEE@localhost]$ mkdir svhec [EEE@localhost]$ ls cse svhec Remove the directory [EEE@localhost]$ rmkdir svhec [EEE@localhost]$ Enter into the directory [EEE@localhost]$ cd cse [EEE@localhost cse]$ Creation of files [EEE@localhost cse]$ cat > svhec Welcome to svhec
7

Ctrl + z [EEE@localhost]$ Reading the file content [EEE@localhost cse]$ cat svhec Welcome to svhec [EEE@localhost]$ Exit from the directory [EEE@localhost cse]$ cd [EEE@localhost]$ Present working directory [EEE@localhost]$ pwd Home/EEE [EEE@localhost ]$ List of files [EEE@localhost]$ ls cse svhec avg.sh add.c EDITOR COMMANDS [EEE@localhost]$vi pgm1.sh ~ ~ "pgm.sh" [New File] FILE MANIPULATION COMMANDS Move the content of one file to another [EEE@localhost]$ mv svhec cse [EEE@localhost]$ Searching of files [EEE@localhost]$ find cse cse [EEE@localhost]$ Copy the file content to another file [EEE@localhost]$ cp cse svhec Remove the files [EEE@localhost]$ rm svhec
8

0, 0-1

All

Sorting [EEE@localhost]$ sort EEE [EEE@localhost]$ [EEE@localhost]$cat cse CSE ECE EEE IT [EEE@localhost]$ INFORMATION COMMANDS [EEE@localhost]$man echo ECHO(1) NAME echo - display a line of text SYNOPSIS echo [OPTION]... [STRING]... DESCRIPTION Echo the STRING(s) to standard output. -n -e -E do not output the trailing newline enable interpretation of backslash escapes disable interpretation of backslash escapes (default) User Commands ECHO(1)

--help display this help and exit --version output version information and exit If -e is in effect, the following sequences are recognized: : [EEE@localhost]$ [EEE@localhost]$who EEE pts/1 [EEE@localhost]$cal March 2009 Su 1 8 Mo 2 9 Tu 3 10 We 4 11 Th 5 12
9

2009-03-06 09:34 (192.168.1.51)

Fr 6 13

Sa 7 14

15 22 29

16 23 30

17 24 31

18 25

19 26

20 27

21 28

[EEE@localhost]$ [EEE@localhost]$ tty /dev/pts/1 [EEE@localhost]$

RECORD VIVA-VOICE TOTAL

15 10 25

RESULT: Thus the various basic UNIX commands are performed in various operations.
10

Ex. No:2 SIMPLE SHELL PROGRAMMING Date: AIM To write a shell program for, i. ii. iii. iv. To find Average of three numbers To find Sum of N natural numbers To find Area of triangle To convert Fahrenheit to Celsius i) AVERAGE OF 3 NOS:

ALGORITHM Step 1: Start the program. Step 2: Read the value of a b c. Step 3: To calculate the average of three numbers by using the formula, Avg = (a+b+c)/3. Step 4: Display the value of average. Step 5: Stop the program. PROGRAM echo "Eneter a,b,c:" read a read b read c avg=$(echo "scale=2;($a+$b+$c)/ 3"|bc) echo The average=$avg" OUTPUT Enter a,b,c:10 20 30 The average=20.00 ii) SUM OF N NATURAL NUMBERS

ALGORITHM Step 1: Start the program. Step 2: Read the value of n. Step 3: To calculate the sum of n numbers by using the formula, Sum = (n (n+1))/2. Step 4: Display the value of Sum. Step 5: Stop the program. PROGRAM echo "Eneter n:" read n a=`expr $n + 1 ` b=`expr $n \* $a` s=`expr $b / 2` echo "The sum is $s" OUTPUT Enter n:5
11

The sum is 15 iii) AREA OF TRIANGLE:

ALGORITHM Step 1: Start the program. Step 2: Read the value of Breath and Height of triangle. Step 3: To calculate the area of triangle by using the formula, Area = (breath*height)/2. Step 4: Display the value of Sum. Step 5: Stop the program. PROGRAM echo " Enter the breath and height :" read a read b area=$(echo "scale=2;($a*$b)/ 2"|bc) echo "The area is= $area" OUTPUT Enter breath and height : 2 The area is 2.00 iv)

FAHRENHEIT TO CELSIUS

ALGORITHM Step 1: Start the program. Step 2: Read the value Fahrenheit. Step 3: To calculate the value of Celsius by using the formula, Celsius = (Fahrenheit -32.0)/1.8. Step 4: Display the value of Celsius. Step 5: Stop the program. PROGRAM echo "Enter f value:" read r c=$(echo "scale=2;($r-32)/ 1.8"|bc) echo "the Celsius is $c" OUTPUT RECORD Enter f value:98 The Celsius is 36.66 VIVA-VOICE TOTAL

15 10 25

RESULT Thus the program was executed successfully.

12

Ex. No:3 DECISION MAKING AND BRANCHING Date: AIM To write a shell program for, i. ii. iii. iv. To find Biggest of 3 numbers To find the Nature of quadratic equation To find Even or Odd To perform arithmetic operations using selective case i) BIGGEST OF 3 NUMBERS

ALGORITHM: Step 1: Start the program. Step 2: Read the value of a b and c. Step 3: Check whether a is greater than b or not. If it is true go to next step. Else go to step 7. Step 4: Check whether a is greater than c or not. If it is true go to next step. Else go to step 6. Step 5: Display a is big. Step 6: Display c is big. Step 7: Check whether b is greater than c or not. If it is true go to next step. Else go to Step 8: Display b is big. Step 9: Display c is big. Step 10: Stop the program. PROGRAM echo "Enter the value a b c:" read a read b read c if test $a -gt $b then if test $a -gt $c then echo "$a is big" else echo "$c is big" fi else if test $b -gt $c then echo "$b is big" else echo "$c is big " fi fi OUTPUT Enter the value of a b c :20 40
13

30 40 is big

ii)

QUADRATIC EQUATION

ALGORITHM Step 1: Start the program. Step 2: Read the value of a b and c. Step 3: T o calculate the value of d using the formula, d = (b*b)-(4*a*c). Step 4: Check whether d is greater than zero or not. If it is true go to next step. Else go to step 6. Step 5: Display roots are real and unequal. Step 6: Check whether d is greater than or equal to 0 or not. If it is true go to next step. Else goto step 8. Step 7: Display roots are real and equal. Step 8: Display roots are real and imaginary. Step 9: Stop the program. PROGRAM echo "Enter the value of a b c:" read a read b read c d=`expr $b \* $b ` e=`expr 4 \* $a ` e=`expr $c \* $e ` f=`expr $d - $e` if test $f -gt 0 then echo "roots are real and unequal" elif test $f -eq 0 then echo "roots are real and equal" else echo "roots are real and imaginary " fi OUTPUT Enter the value of a b c :1 4 4 Roots are real and equal iii) ALGORITHM Step 1: Start the program.
14

EVEN OR ODD

Step 2: Read the value of n. Step 3: T o calculate the value of b using the formula, b=n%2. Step 4: Check whether b is equal to zero or not. If it is true go to next step. Else go to step6 Step 5: Display the given number is even. Step 6: Display the given number is odd. Step 7: Stop the program. PROGRAM echo "Eneter n:" read n f=`expr $n % 2` if test $f -eq 0 then echo "$n is even number" else echo "$n is odd number " fi OUTPUT Enter n:6 6 is even number iv) ARITHMETIC OPERATIONS

ALGORITHM Step 1: Start the program. Step 2: Read the value of a, b and c. Step 3: Read the choices ch. The choices are 1 2 3 4 & 5. Step 4: Using the case function to perform the Addition, Subtraction, Multiplication, and Division & Modulo operation. Step 5: To perform the above operations using the following formula in the above order. 1) c=a*b 2) c=a-b 3) c=a*b 4) c=a/b 5) c=a%b and Display the results. Step 6: Stop the program. PROGRAM echo "Enter the value of a b c:" read a read b echo -n "Enter the choice:" echo 1.ADD 2.SUBTRACTION 3.MULTIPLICATION 4.DIVISION 5.MODULUS read ch case "$ch" in 1) c=`expr $a + $b` echo -n "The sum is =$c";; 2) c=`expr $a - $b` echo -n "The difference is=$c";; 3) c=`expr $a \* $b`
15

echo -n "The multiplication is=$c";; 4) c=$(echo "scale=2;$a/$b" | bc) echo -n "The division is=$c";; 5) c=`expr $a % $b` echo -n "The modulo operation result is=$c";; esac OUTPUT Enter the value of a b c: 21 5 Enter your choice: 1. ADD 2.SUBTRACTION 3.MULTIPLICATION 5.MODULUS 5 The modulo operation result=1

4.DIVISION

RECORD VIVA-VOICE TOTAL RESULT: Thus the program was executed successfully.

15 10 25

16

Ex. No: 4 Date: AIM:

DECISION MAKING AND LOOPING

To write a shell program for,

i. ii. iii. iv.

To find the Factorial of given number To find the reverse of the given number To find the sum of square of Natural Numbers upto the given Limit To find the sum of each digit in the given number

i) ALGORITHM Step 1: Start the program.

FACTORIAL OF GIVEN NUMBER

Step 2: Read the value of a. Step 3: Assign the values i=1 & f=1. Step 4: To check whether i is less than a or not. If it is true go to next step. Otherwise goto Step8 Step 5: To calculate the value of f by using the formula, f=f*i. Step 6: To increment the value of i by 1. Step 7: Goto step 4. Step 8: To print the value of f. Step 9: Stop the program. PROGRAM echo "Eneter n:" read a
17

i=1 f=1 while [ $i -le $a ] do f=`expr $f \* $i` i=`expr $i + 1` done echo -n "factorial of $a is $f" OUTPUT Enter n:5 factorial of 5 is 120 ii) ALGORITHM Step 1: Start the program. Step 2: Read the value of a. Step 3: Assign the values i=a & f=1. Step 4: To check whether a is greater than 0 or not. If it is true go to next step. Otherwise goto Step8 Step 5: To calculate the value of f by using the formula, r =a%10 f=f*10+r Step 6: To change the value of a by using the formula, a=a/10 Step 7: Goto step 4. Step 8: To print the value of f. Step 9: Stop the program. PROGRAM echo "Enter a:" read a i=$a
18

REVERSE OF THE NUMBER:

f=0 while [ $a -gt 0 ] do r=`expr $a % 10` f=`expr $f \* 10 ` f=`expr $f + $r` a=`expr $a / 10` done echo "The reverse of $i is $f" OUTPUT: Enter n:123 The reverse of 123 is 321

iii) SUM OF SQUARE OF NATURAL NUMBERS ALGORITHM Step 1: Start the program. Step 2: Read the value of a. Step 3: Assign the values i=1 & f=0. Step 4: To check whether i is less than a or not. If it is true go to next step. Otherwise goto Step8 Step 5: To calculate the value of f by using the formula, r =i*i f=f+r Step 6: To change the value of i by using the formula, i=i+1 Step 7: Goto step 4. Step 8: To print the value of f. Step 9: Stop the program. PROGRAM echo "Enter a:" read a i=1 f=0 while [ $i -le $a ] do r=`expr $i \* $i` f=`expr $f + $r` i=`expr $i + 1` done echo "The sum of square of natural numbers upto $a is $f" OUTPUT Enter a:5 The sum of square of natural numbers upto 5 is 55
19

iv)

SUM OF EACH DIGIT IN THE GIVEN NUMBER

ALGORITHM Step 1: Start the program. Step 2: Read the value of a. Step 3: Assign the values i=a & f=0. Step 4: To check whether a is greater than 0 or not. If it is true go to next step. Otherwise goto Step8 Step 5: To calculate the value of f by using the formula, r =a%10 f=f+r Step 6: To change the value of a by using the formula, a=a/10 Step 7: Goto step 4. Step 8: To print the value of f. Step 9: Stop the program.

PROGRAM echo "Enter n:" read a i=$a f=0 while [ $a -gt 0 ] do r=`expr $a % 10` f=`expr $f + $r` a=`expr $a / 10` done echo "The sum of each digit in the given number $i is $f" OUTPUT Enter n:123 The sum of each digit in the given number 123 is 6 RECORD VIVA-VOICE TOTAL RESULT Thus the program was executed successfully. 15 10 25

20

Ex. No: 5 Date:

NESTED DECISION MAKING

AIM To write a shell program for,

i. ii. iii. iv. i) ALGORITHM

To find the List of Prime numbers upto the given limit. To find the List of Armstrong numbers upto the given limit To find the List of Divisors for the given number To find the Multiplication table for given number GENERATE LIST OF PRIME NUMBERS

Step 1: Start the program. Step 2: Read the value of m. Step 3: Assign the value k=2. Step 4: To check whether k is less than m or not. If it is true go to next step. Otherwise goto Step17 Step 5: Assign the values i=0 and j=2. Step 6: To calculate the value of n by using the formula, n=k-1 Step 7: To check whether j is less than n or not. If it is true go to next step. Otherwise goto Step13 Step 8: To calculate the value of d by using the formula, d=k%10 Step 9: To check whether d is equal to zero or not. If it is true goto next step. Otherwise
21

goto step11 Step 10: To increment the value of i by one. Step 11: To increment the value of j by one. Step 12: Goto step 7. Step 13: To check whether i is equal to zero or not. If it is true goto next step. Otherwise goto step 15. Step 14: To print the value of j Step 15: To increment the value of k by one. Step 16: Goto step 4. Step 17: Stop the program.

PROGRAM echo "Enter limit:" read m k=2 echo "Prime number Lists:" while [ $k -le $m ] do i=0 j=2 n=`expr $k - 1` while [ $j -le $n ] do d=`expr $k % $j` if test $d -eq 0 then i=`expr $i + 1` fi j=`expr $j + 1` done if test $i -eq 0 then echo "$j" fi k=`expr $k + 1` done
22

OUTPUT: Enter the limit: 10 Prime number Lists: 2 3 5 7 ii) GENERATE LIST OF ARMSTRONG NUMBERS

ALGORITHM Step 1: Start the program. Step 2: Read the value of a. Step 3: Assign the value of j=1. Step 4: To check whether j is less than a or not. If it is true go to next step. Otherwise goto Step14 Step 5: Assign the values f=0 and i=j. Step 6: To check whether i is greater than 0 or not. If it is true go to next step. Otherwise goto Step10 Step 7: To calculate the value of f by using the formula, r=i%10 f=f+(r*r*r) Step 8: To increment the value of i by one. Step 9: Goto step 7. Step 10: To check whether j is equal to f or not. If it is true goto next step. Otherwise goto step 15. Step 11: To print the value of j Step 12: To increment the value of j by one. Step 13: Goto step 4. Step 14: Stop the program.

PROGRAM echo "Enter the limit:" read a f=0 j=1 echo "ARMSTRONG NUMBERS:" while [ $j -le $a ] do f=0
23

i=$j while [ $i -gt 0 ] do r=`expr $i % 10` m=`expr $r \* $r` m=`expr $m \* $r` f=`expr $f + $m` i=`expr $i / 10` done if test $j -eq $f then echo -n "$j " fi j=`expr $j + 1` done OUTPUT Enter the limit: 999 ARMSTRONG NUMBERS: 1 153 370 371 407 iii) ALGORITHM Step 1: Start the program. Step 2: Read the value of a. Step 3: Assign the value of j=1. Step 4: To calculate the value of n by using the formula, n=a / 2. Step 5: To check whether j is less than n or not. If it is true go to next step. Otherwise goto Step11. Step 6: To calculate the value of r by using the formula, r=a % j Step 7: To check whether r is equal to 0 or not. If it is true goto next step. Otherwise goto Step9. Step 8: To print the value of j. Step 9: To increment the value of j by one. Step 10: Goto step 5.
24

LIST OF DIVISORS

Step 11: Stop the program. PROGRAM echo "Enter n:" read a j=1 echo "list of divisors are:" n=`expr $a / 2` while [ $j -le $n ] do r=`expr $a % $j` if test $r -eq 0 then echo "$j" fi j=`expr $j + 1` done OUTPUT: Enter the number:12 The divisors are: 1 2 3 4 6 iv) ALGORITHM Step 1: Start the program. Step 2: Read the value of m and n. Step 3: Assign the value of j=1. Step 4: To check whether j is less than m or not. If it is true go to next step. Otherwise goto Step9 Step 5: To calculate the value of d by using the formula, d=j*n. Step 6: To print the value of d
25

MULTIPLICATION TABLE

Step 7: To increment the value of j by one. Step 8: Goto step 4. Step 9: Stop the program.

PROGRAM echo "Enter limit:" read m echo "Enter n:" read n j=1 echo "Multiplication Tables are:" while [ $j -le $m ] do d=`expr $j \* $n` echo "" echo "$j * $n=$d" j=`expr $j + 1` done OUTPUT: Enter Limit: 5 Enter n: 2 1 * 2 =2 2 * 2 =4 3* 2 =6 4 * 2 =8 5 * 2 =10

RECORD RESULT: Thus the program was executed successfully. VIVA-VOICE TOTAL

15 10 25

26

Ex. No: 6 Date: AIM:

FUNCTIONS

To write a C program for finding the search element using,

i. ii.

Linear Search Binary search

i)

LINEAR SEARCH

ALGORITHM Step 1: Start the program. Step 2: Read the value of n. Step 3: To read the n elements into the array. Step 4: To call the module Linear with the argument of array starting index (goto step 1 in module Linear). Step 5: Stop the program MODULE Linear

Step 1: To read the search value. Step 2: Initialize the value of t is equal to zero and starting index is assigned to a[0]. Step 3: Check whether i is Less than n or not. If it is true goto next step. Otherwise goto step 7. Step 4: Check whether search element is equal to a[i] element. If it is equal then goto next step. Otherwise goto step 6.
27

Step 5: To increment the value of t by 1. Step 6: Goto step 3. Step 7: Check whether t is greater than zero or not. If it is zero goto next step. Otherwise goto step 9. Step 8: To display the value of t and search element was found. Step 9: To display the search element was not found. Step 9: Return the control to main program. PROGRAM #include<stdio.h> int linear(int *); int n,a[30],i; main() { printf("\nEnter the limit:"); scanf("%d",&n); printf("\nEnter the elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); linear(&a[0]); } int linear(int *p) { a[0]=*p; int ch,t=0; printf("\nThe elements are:"); for(i=0;i<n;i++) printf("\n%d",a[i]); printf("\nEnter the search element:"); scanf("%d",&ch); for(i=0;i<n;i++) { if(ch==a[i]) t++; } if(t>0) printf("\nThe search element %d is found in %d times",ch,t); else printf("\nThe search element is not found"); } OUTPUT
28

$ cc inser.c $ ./a.out Enter the limit:5 Enter the elements: 9 8 7 6 5 The elements are: 9 8 7 6 5 Enter the search element:9 The search element 9 is found in 1 time

ii) ALGORITHM Step 1: Start the program. Step 2: Read the value of n.

BINARY SEARCH

Step 3: To read the n elements into the array. Step 4: To sort the elements of array using the concept of Linear sorting. Step 4: To call the module binary with the argument of array starting index (goto step 1 in module binary). Step 5: Stop the program

MODULE Binary: Step 1: To read the search value. Step 2: Initialize the value of t is equal to zero, beg=0, end=n-1, mid=n/2 and starting index is assigned to a[0].
29

Step 3: Check whether beg is Less than end or not and a[i] is not equal to search element. If it is true goto next step. Otherwise goto step 7. Step 4: Check whether search element is les than a[mid] element or not . If it is equal then goto next step. Otherwise goto step 6. Step 5: To assign end=mid-1, n=n/2 and mid=n/2. Step 6: To assign beg=mid+1, n=n/2, mid=mid+n/2. Step 7: Goto step 3. Step 7: Check whether se is equal to a[mid] or not. If it is equal goto next step. Otherwise goto step 10. Step 8: To display the search element was found Step 10: To display the search element was not found. Step 11: Return the control to main program.

PROGRAM #include<stdio.h> int binary(int *); int n,a[30],i,j,temp; int se,beg,mid,end; main() { printf("\nEnter the limit:"); scanf("%d",&n); printf("\nEnter the elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { for(j=i;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; }
30

} } printf("\nSorted lists are:"); for(i=0;i<n;i++) printf("\n%d",a[i]); binary(&a[0]); } int binary(int *p) { a[0]=*p; beg=0; end=n-1; mid=n/2; printf("\nEnter the element to be searched:"); scanf("%d",&se); while((beg<=end)&&(a[mid]!=se)) { if(se<a[mid]) { end=mid-1; n=n/2; mid=n/2; } else { beg=mid+1; n=n/2; mid=mid+n/2; } } if(a[mid]==se) { printf("\nthe search element is found"); } else { printf("\nSearch element is not found"); } }

OUTPUT $ cc inser.c $ ./a.out Enter the limit:5 Enter the elements:8


31

7 6 5 4 Sorted lists are: 4 5 6 7 8 Enter the element to be searched:5 The search element is found

RECORD VIVA-VOICE TOTAL RESULT: Thus the program was executed successfully

15 10 25

32

Ex. No: 7 Date: AIM: To write a C program for,

RECURSION

i. ii. iii. iv.

To find the Factorial of given number. To find the Sum of N natural numbers To find the Sum of square of N natural numbers To find the Fibonacci Series i) FACTORIAL OF GIVEN NUMBER

ALGORITHM Step 1: Start the program. Step 2: Declare the fact function in global. Step 3: Declare the integer value of n. Step 4: Display the statement Enter the value of n. Step 5: Get the value of integer n using the scanf function. Step 6: To call the fact function to calculate the factorial of the given number using the formula (n*fact(n-1) until the given number reaches 1. Step 7: Pass the answer to the main function using return statement. Step 8: Display the answer using the printf statement. Step 9: Stop the program.

PROGRAM #include<stdio.h> int fact(int); main() {


33

int n; printf("\nEnter the value of n:"); scanf("%d",&n); printf("Factorial of %d is %d",n,fact(n)); } int fact(int n) { if(n==1) return 1; else return(n*fact(n-1)); } OUTPUT: $cc fact.c $ ./a.out Enter the value of n:5 Factorial of 5 is 120

ii) ALGORITHM Step 1: Start the program.

SUM OF N NATURAL NUMBERS

Step 2: Declare the fact function in global. Step 3: Declare the integer value of n. Step 4: Display the statement Enter the value of n using printf function. Step 5: Get the value of integer n using the scanf function. Step 6: To call the fact function to calculate the natural numbers of the given number using the formula (n+fact(n-1) until the given number reaches 1. Step 7: Pass the answer to the main function using return statement. Step 8: Display the answer using the printf statement. Step 9: Stop the process.
34

PROGRAM #include<stdio.h> int fact(int); main() { int n; printf("\nEnter the value of n:"); scanf("%d",&n); printf("Sum of %d natural numbers are %d",n,fact(n)); } int fact(int n) { if(n==1) return 1; else return(n+fact(n-1)); } OUTPUT: $ cc sum.c $ ./a.out Enter the value of n:5 Sum of 5 natural numbers are 15

iii) ALGORITHM

SUM OF SQUARE OF NATURAL NUMBERS

Step 1: Start the program. Step 2: Declare the fact function in global. Step 3: Declare the integer value of n. Step 4: Display the statement Enter the value of n using printf function. Step 5: Get the value of integer n using the scanf function. Step 6: To call the fact function to calculate the natural numbers of the given number using the formula ((n*n)+fact(n-1) until the given number reaches 1. Step 7: Pass the answer to the main function using return statement.
35

Step 8: Display the answer using the printf statement. Step 9: Stop the process. PROGRAM #include<stdio.h> int fact(int); main() { int n; printf("\nEnter the value of n:"); scanf("%d",&n); printf("Sum of square of %d natural numbers are is %d",n,fact(n)); } int fact(int n) { if(n==1) return 1; else return((n*n)+fact(n-1)); } OUTPUT $ cc sumsquare.c $ ./a.out Enter the value of n:5 Sum of square of 5 natural number are 55

iv) ALGORITHM Step 1: Start the program.

FIBANOCCI SERIES

Step 2: Declare the fact function in global. Step 3: Declare the integer value of n. Step 4: Display the statement Enter the value of n using printf function. Step 5: Get the value of integer n using the scanf function. Step 6: To call the fact function to calculate the Fibonacci series of the given limit using
36

the formula (fact(n-1)+fact(n-2)) until the given number reaches 2 or 1. Step 7: Pass the answer to the main function using return statement. Step 8: Display the answer using the printf statement. Step 9: Stop the process.

PROGRAM #include<stdio.h> int fact(int); main() { int n; printf("\nEnter the value of n:"); scanf("%d",&n); printf("Fibnocci series at the index %d is %d \n",n,fact(n)); } int fact(int n) { if(n==1) return 0; else if(n==2) return 1; else return(fact(n-1)+fact(n-2)); } OUTPUT $ cc fib.c $ ./a.out Enter the value of n:5 Fibonacci series at the index 5 is 3 RECORD VIVA-VOICE TOTAL 15 10 25

RESULT: Thus the program was executed successfully


37

Ex. No: 8 Date: AIM: To write a C program for,

POINTERS

i. ii. i) ALGORITHM

To access the elements of array using pointers. To find the search element in array using pointers.

TO ACCESS THE ARRAY ELEMENT USING POINTERS

Step 1: Start the program. Step 2: Read the value of n. Step 3: To read the n elements into the array. Step 4: To initialize the value of starting address to pointer variable. Step 5: To display the content of array using pointer variables. Step 6: Stop the program PROGRAM #include<stdio.h> main() { int a[10],n,i,*p; printf("\nEnter the Limit:"); scanf("%d",&n); printf("\nEnter the values:"); for(i=0;i<n;i++) scanf("%d",&a[i]); p=&a[0]; printf("\nElements of arrays are:"); for(i=0;i<n;i++,p++) printf("\n\n %d\n",*p); } OUTPUT $ cc arrayaccess.c
38

$ ./a.out Enter the Limit:5 Enter the values:89 78 67 56 45 Elements of arrays are: 89 78 67 56 45

ii) ALGORITHM

TO SEARCH THE GIVEN NUMBER USING POINTERS

Step 1: Start the program. Step 2: Read the value of n. Step 3: To read the n elements into the array. Step 4: To initialize the value of starting address to pointer variable. Step 5: To read the value of search element. Step 6: Check whether i is less than n or not. If it is true goto next step. Otherwise goto step 9. Step 7: Check whether search element is equal to the value of pointer variable. If it is true goto next step. Otherwise goto step 9. Step 8: Goto Step 6. Step 9: check whether t is greater than 0 or not. If it true goto next step. Otherwise goto step 11. Step 10: To display the value of t and print search element was found. Step 11: To display search element was not found.
39

Step 12: Stop the program PROGRAM #include<stdio.h> main() { int a[10],n,i,*p,t,se; printf("\nEnter the Limit:"); scanf("%d",&n); printf("\nEnter the values:"); for(i=0;i<n;i++) scanf("%d",&a[i]); p=&a[0]; printf("\nEnter the search element:"); scanf("%d",&se); t=0; for(i=0;i<n;i++,p++) { if(se==*p) t++; } if(t>0) printf("\nsearch elemnt %d is found in %d times",se,t); else printf("\nsearch element %d is not found",se); } OUTPUT $ cc arrayfind.c $ ./a.out Enter the Limit:5 Enter the values:89 78 67 56 45 Enter the search element: 78 Search element 78 is found in 1 times RECORD VIVA-VOICE TOTAL 15 10 25

RESULT: Thus the program was executed successfully


40

Ex. No: 9 Date: AIM: To write a C program for,

STRINGS

i. ii.

To find the Given string is palindrome or not To sort the given names in ascending order

i) ALGORITHM Step 1: Start the program. Step 2: Read the input string.

PALINDROME

Step 3: To Calculate the length of the input string Using the function strlen(). Step 4: Initialize i is equal to zero and j is equal to n. Step 5: Check whether the i is less than n then goto next step. Otherwise goto step 8. Step 6: To copy the content of jth memory location in the character array name is copied into ith memory location to the character array name1. Step 7: Goto step 5. Step 8: Check whether the character array name and name1 are equal or not Using strcmp() function. If it is equal goto next step. Otherwise goto step 10. Step 9: To display the given string is Palindrome. Step 10: To display the given string is not palindrome. Step 11: Stop the program.

PROGRAM
41

#include<stdio.h> main() { char name[30],name1[30]; int i,n,j; printf("\nEnter the string:"); scanf("%s",name); n=strlen(name); for(i=0,j=n-1;i<n;i++,j--) name1[i]=name[j]; if(strcmp(name1,name)==0) printf("The given string is palindrome"); else printf("\nThe given string is not palindrome"); }

OUTPUT: $ ./a.out Enter the string:madam The given string is palindrome

ii)

STRING SORTING

ALGORITHM Step 1: Start the program. Step 2: Declare the character arrays as name & temp and char values as i,j. Step 3: To get the limit value how many names to be sorted using the scanf function. Step 4: To get the names in name array and print those names. Step 5: Using the strcmp and strcpy functions to sort the names and print the names in sorted order. Step 6: Stop the program.
42

PROGRAM #include<stdio.h> #include<string.h> main() { char name[10][10],i,j,temp[10],n; printf("\nEnter the limit:"); scanf("%d",&n); printf("\nEnter the names:"); for(i=0;i<n;i++) { scanf("%s",name[i]); } printf("\nThe names are:"); for(i=0;i<n;i++) printf("\n\n%s\n\n",name[i]); for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(strcmp(name[i],name[j])<0) { strcpy(temp,name[i]); strcpy(name[i],name[j]); strcpy(name[j],temp); } } } printf("\nSorting lists are:"); for(i=0;i<n;i++) printf("\n%s\n",name[i]); }

OUTPUT $ cc stringsort.c $ ./a.out Enter the limit:5 Enter the names: yuvaraj prabhu loganathan karthi arun Sorting lists are:
43

arun karthi loganathan prabhu yuvaraj

RECORD VIVA-VOICE TOTAL RESULT: Thus the program was executed successfully
44

15 10 25

Ex. No: 10 Date: AIM: To write a C program for,

FILES

i. ii.

To copy the content of one file to another. To Count Number of characters and Lines in a file.

i)

COPY OF FILE FROM ONE TO ANOTHER

ALGORITHM Step 1: Start the program. Step 2: Open the file a1.dat file in Write mode using the pointer fp1. Step 3: Read the content of a1.dta file using fputc() function. Step 4: To copy the content of a1.dat file into a2.dat file using file pointers fp1 and fp2. Step 5: Display the content of file a2.dat using file pointers using the file pointer fp2. Step 6: Finally to close all the file pointers. Step 7: Stop the program. PROGRAM #include<stdio.h> main() { FILE *fp1,*fp2; char c; fp1=fopen("a1.dat","w"); while((c=fgetc(stdin))!=EOF) { fputc(c,fp1); } fclose(fp1);
45

fp2=fopen("a2.dat","w"); fp1=fopen("a1.dat","r"); while(!feof(fp1)) { fputc(fgetc(fp1),fp2); } fcloseall(); fp2=fopen("a2.dat","r"); while((c=fgetc(fp2))!=EOF) { printf("%c",c); } fclose(fp2); }

OUTPUT $ ./a.out hai how are you? hai how are you?

ii)

TO COUNT NUMBER OF CHARACTERS AND LINES IN THE GIVEN FILE

ALGORITHM Step 1: Start the program. Step 2: Read the file name. Step 3: Open the file in read mode using the file pointer fp2. Step 4: Check whether the content of file is equal to NULL or not. If it is true go to next step. Otherwise goto step 6. Step 5: To display the files cannot be open. Step 6: To calculate Number of lines and words in the file using file pointer. Step 7: To display The number of words and Lines in the file.
46

Step 7: Finally to close all the file pointers. Step 7: Stop the program. PROGRAM #include<stdio.h> main() { FILE *fopen(),*fp2; int c,n,nl; char fname[30]; nl=0; n=0; printf("\nEnter the file name:"); scanf("%s",fname); fp2=fopen(fname,"r"); if(fp2==NULL) { printf("\nCannot open %sthe file for reading",fname); } else { c=getc(fp2); while(c!=EOF) { if(c=='\n') nl++; n++; c=getc(fp2); } fclose(fp2); if(n!=0) { printf("\nThere are %d characters in %s \n",n,fname); printf("\n There are %d lines \n",nl); } else printf("file %s is empty",fname); } }

OUTPUT: $ cc filecount.c $ ./a.out


47

Enter the file name:a.txt There are 15 characters in a.txt There are 1 lines Content of file a.txt: Hai how are you?

RECORD VIVA-VOICE TOTAL

15 10 25

RESULT: Thus the program was executed successfully.

48

You might also like