0% found this document useful (0 votes)
225 views36 pages

Lab Manual: Department of Computer Science

The document describes a lab manual for a Computer Practice Laboratory course. It contains instructions for experiments on UNIX commands, shell programming, and C programming on UNIX. The UNIX commands section includes general purpose commands, directory handling commands, file handling commands, input/output redirection, and pipes and filters. The shell programming section provides examples of shell scripts to convert temperatures, check if a number is odd/even, compare strings, and determine if a number is positive/negative. It also includes a menu driven shell program. The C programming section provides examples of C programs using functions, arrays, strings, file input/output.

Uploaded by

MOHANBALAJIECE
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views36 pages

Lab Manual: Department of Computer Science

The document describes a lab manual for a Computer Practice Laboratory course. It contains instructions for experiments on UNIX commands, shell programming, and C programming on UNIX. The UNIX commands section includes general purpose commands, directory handling commands, file handling commands, input/output redirection, and pipes and filters. The shell programming section provides examples of shell scripts to convert temperatures, check if a number is odd/even, compare strings, and determine if a number is positive/negative. It also includes a menu driven shell program. The C programming section provides examples of C programs using functions, arrays, strings, file input/output.

Uploaded by

MOHANBALAJIECE
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 36

INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

FIRST YEAR – SECOND SEMESTER

(COMMON TO ALL BRANCHES)

LAB MANUAL

GE 2155 - COMPUTER PRACTICE LABORATORY - II

DEPARTMENT OF COMPUTER SCIENCE

1
INDEX

S.No LIST OF EXPERIMENTS Page No

1 UNIX COMMANDS

1.1 General Purpose Commands 5

1.2 Directory Handling Commands 7

1.3 File Handling Commands 8

1.4 Input and Output Redirection 9

1.5 Pipes and Filters 10

2 SHELL PROGRAMMING

2.1 Program to convert Fahrenheit to centigrade and vice versa 12

2.2 Program to check whether the number is odd or even 13

2.3 Program to Compare two strings 14

2.4 Program to determine the given number is positive, negative or zero 15

2
2.5 Program to generate menu driven program 16

2.6 Program to find the square and cube of the first five numbers 18

2.7 Program to find the sum of first ‘N’ natural numbers 19

2.8 Program to find the sum of digits in the given number. 20

3 C PROGRAMMING ON UNIX

3.1 Program to perform linear search using function 22

3.2 Program to print the elements of an array using pointers 25

3.3 Program to find the length of the string 28

3.4 Program to concatenate two strings 31

3.5 Program to copy the contents of one file to another. 34

3
UNIX COMMANDS

4
1.1. GENERAL PURPOSE COMMANDS

1. $ date “+ %d%m%y”

13/01/09

Use : Displays the date in the specified dd/mm/yy format.

2. $cal January 2009

January 2009

Use : Displays the calendar for the year Jan 2009

3. $who

2cse12 tty5 2009-12-31 07: 06


2cse11 tty6 2009-12-31 07: 06
2cse10 tty7 2009-12-31 07: 06
2cse15 tty8 2009-12-31 07: 06

Use: Displays the number of users who are logged in to the network

4. $ who –b

System reboot 2009 -12-30 08:45

Use : Display the last date and time of reboot of the UNIX server

5
5. $ who am i

User1 tty1 Jan15 09:34

Use : Identifies the user and lists the user names, terminal line, the date of
login.

6. $uname –a

Linux ubuntu 2.64-generic #1 SMP Thu Apr 10 13:25:23 UTC 2009 i686
GNU/Linux

Use : Display details about your operating system.

7. $ man cat

Use: Displays the help level for the cat command

8. $ echo “Information Technology”

Use : Displays the message on the screen

9. $ login Username

Password:

Use : To login in to the UNIX system

6
1.2. DIRECTORY HANDLING COMMANDS

10. $pwd

/home/root
Use : Displays the current working directory

11. $ls –a

.esd_auth .config .gvfs .pulse


.cache .gnugp. profile .ssh
.local Template Music Vidoes
Use: Display all the files including hidden files

12. $ls –R

Divisors nnsum nsn rns test


Use : Displays all the files in the sub directories

13. $mkdir cse

Use: Creates a new directory cse.

14. $rmdir cse

Use : Removes the existing directory cse

7
1.3. FILE HANDLING COMMANDS

15. $ cd stud

$root/stud

Use: To change the directory.

16. $ cat> test

Unix is a open source operating system and its license free of cost

Its mainly used for mainframe and high security networks

Ctr+d

Use: To create a file in the existing directory

17. $ cp m.c new.c

Use : This command copies the m.c to new file new.c

18. $wc m.c

Use : This counts the number of lines, characters and words in m.c file.

19. $rm f.c

Use: This command removes the file f.c

8
1.4. INPUT AND OUTPUT REDIRECTION

20. $date>testing

$cat testing

Wed Dec 31 12:47:59 UTC 2008

Use: Redirect the system date contents to a file.

21. $cat test1


Welcome to

$cat test2
Information Technology

$cat test1>>test2

$cat test2

Welcome to Information Technology

Use: Concatenate two files and displays the contents of test2

22. $ ls sh*

Shan
Suresh
SaiKumar.c
Shanthi.c

Use : List the files starting with “sh”

23. $ls *ha

Radha
Asha
Usha
Sha

Use : List the files ending with ‘ha”

9
1.5. PIPES AND FILTERS

24. $cat testing.c | tr “[a-z]” “[A-Z]”

UNIX IS AN OPEN SOURCE OPERATING SYSTEM AND ITS LICENSE

FREE OF COST. IT’S MAINLY USED FOR MAINFRAME AND HIGH SECURITY

NETWORKS

Use : Display the entire file in Uppercase.

25. $who |grep “3cse”

2cse12 tty5 2009-12-31 07: 06


2cse11 tty6 2009-12-31 07: 06
2cse10 tty7 2009-12-31 07: 06

Use: To extract logins of people whose login names starts with “cse”

26. $head -3 top

Welcome to C Language
Welcome to C++ Language
Welcome to Java Programming

27. $tail -3 top

Welcome to Visual Basic


Welcome to Oracle
Welcome to PHP

Use: Head -3 is used to display the first three lines in the file top and Tail -3 is
used to display the last three lines in the file top.

10
SHELL PROGRAMMING

11
2.1. FAHRENHEIT TO CENTIGRADE AND VICE VERSA

AIM:

To write a shell program to convert Fahrenheit to Centigrade and vice-versa

PROGRAM:

read c

f= ’expr “scale=2;(1.8 * $c) + 32” | bc’

echo “Fahrenheit equivalent is : $f”

echo –n “Enter temperature in Fahrenheit:”

read fh

cg= ‘expr “scale=2; ($fh – 32) / 1.8 “ | bc’

echo “Centigrade equivalent is : $cg”

OUTPUT

$ Sh Centigrade to Fahrenheit

Enter the temperature in Centigrade: 20

Fahrenheit equivalent is 68.0

Enter temperature in Fahrenheit: 68

Centigrade equivalent is 20.00

RESULT

Thus the above shell program to convert Fahrenheit to centigrade and vice-versa
is been verified and executed successfully.

12
2.2. ODD OR EVEN

AIM

To write a shell program to check whether the given number is odd or even.

PROGRAM

echo –n “Enter the number:”

read a

c= ’expr $a % 2’

if test $c –eq 0

then

echo “The number $a is even”

else

echo “The number is $a is odd:”

fi

OUTPUT

$ Sh oddeven

Enter the number 6

The number 6 is even

RESULT

Thus the above shell program to check whether the number is odd or even is been
verified and executed successfully.

13
2.3. COMPARE TWO STRINGS

AIM

To write a shell program to compare the two given strings.

PROGRAM

echo –n “Enter the String1:”

read s1

echo –n “Enter the String2:”

read s2

if [ $s1 = $s2 ]

then

echo “Strings are equal”

else

echo “Strings are not equal”

fi

OUTPUT

$ Sh stringcomp

Enter the String1 : Ramesh

Enter the String1 : Kumar

Strings are not equal

RESULT

Thus the above shell program to compare the two given strings is been verified
and executed successfully.

14
2. 4. CHECK NUMBER IS POSITIVE OR NEGATIVE

AIM

To write a shell program to determine the number is positive, negative or zero.

PROGRAM

echo –n “Enter the Value:”


read a
if test $a -eq 0
then
echo “The given number $a is equal to zero”
elif test $a -gt 0
echo “The given number $a is positive”
else
echo “The given number $a is negative”
fi

OUTPUT

$ Sh nopos

Enter the Value: 5


The given number 5 is positive

Enter the Value: -9


The given number -9 is negative

Enter the Value: 0


The given number 0 is zero.

RESULT

Thus the above shell program to determine the number is positive, negative or
zero is been verified and executed successfully.

15
2. 5. MENU DRIVEN PROGRAM

AIM

To write a shell program to generate menu driven program.

PROGRAM

echo “ Menu”
echo “1. List of files”
echo “2. Todays Date”
echo “3. Process Status”
echo “4. Logged in Users”
echo “5. Quit”
echo –n “Enter your Choice: ”
read ch
case “$ch” in

1) ls –I;;
2) date;;
3) ps;;
4) who;;
5) exit;;
6) echo “Invalid Choice”
exit;;
esac

16
OUTPUT

$ Sh menu

Enter the your choice 1

.esd_auth .config .gvfs .pulse


.cache .gnugp. profile .ssh

Enter Your Choice 2

13/01/09

Enter Your Choice 3

PID TTY TIME CMD

8547 pts/0 00:00:02 bash


4547 pts/0 01:00:42 sh
5470 pts/0 00:30:02 ps

Enter Your Choice 4

2cse12 tty5 2009-12-31 07: 06


2cse11 tty6 2009-12-31 07: 06
2cse10 tty7 2009-12-31 07: 06
2cse15 tty8 2009-12-31 07: 06

Enter Your Choice 5

RESULT

Thus the above shell program to determine to generate menu driven program is
been verified and executed successfully.

17
2. 6. SQUARE AND CUBE OF NUMBER

AIM

To write a shell program to find the square and cube for the first five numbers.

PROGRAM

echo –n “Number Square Cube:”


for i in 1 2 3 4 5
do
square= ‘expr $i ‘*’ $i’
cube= ‘expr $i ‘*’ $i ‘*’ $i’
echo “$i $Square $Cube”
done

OUTPUT

$ Sh sqcube

Number Square Cube


1 1 1
2 4 8
3 9 27
4 16 64
5 25 125

RESULT

Thus the above shell program to the square and cube for the first five numbers is
been verified and executed successfully.

18
2.7. SUM OF N NATURAL NUMBERS

AIM

To write a shell program to find the sum of ‘N’ natural Numbers.

PROGRAM

echo –n “Enter the limit:”


read n
sum=0
until [ $i –le $n ]
do
sum = ‘expr $sum + $i’
i= ‘expr $i + 1’
done
echo “The Sum of first $n natural numbers: $sum”

OUTPUT

$ Sh nna

Enter the limit: 5

The sum of first 5 natural numbers: 15

RESULT

Thus the above shell program to find the sum of ‘N’ natural Numbers is been
verified and executed successfully.

19
2.8. SUM OF DIGITS IN GIVEN NUMBER

AIM

To write a shell program to find the sum of digits in the given number.

PROGRAM

echo –n “Enter the number”


read n
sd=0
while [ $n –gt 0 ]
do
a= ‘expr $n %10’
sd= ‘expr $sd + $a’
n= ‘expr $n /10’
done
echo “The sum of digits in the given number is $sd”

OUTPUT

$ Sh sd

Enter the number : 458

The sum of digits in the given number is 17

RESULT

Thus the above shell program to find the sum of digits in the given number is
been verified and executed successfully.

20
C PROGRAMMING
ON UNIX

21
3.1. LINEAR SEARCH

AIM:

To write a C program to perform linear search using function

ALGORITHM:

Step 1: Start the program

Step 2: Read the value of n.

Step 3: Read the array of elements a[i].

Step 4: Read the value of x and call the liner()

Step 4: Compute i=i+1

Step 5: if flag==1 then print element is found else not

Step 6: Stop the program

22
PROGRAM

#include<stdio.h>

void linear(int [],int,int);


main()
{
int i,x,a[20],n;
printf("Enter the limit\n");
scanf("%d",&n);
printf("\n Enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched\n");
scanf("%d",&x);
linear(a,n,x);
}
void linear(int a[],int n,int x)
{
int i,flag=0;
for(i=0;i<n;i++)
{
if(x==a[i])
{
flag=1;
break;
}
}
if(flag==1)
printf("The element %d in the position%d",a[i],i+1);
else
printf("\n The element is not found in the list");
getch();
}

23
OUTPUT:

Enter the limit : 5

Enter the elements : 21 56 89 75 41

Enter the element to be searched: 89

The element is fond at 3rd position

RESULT

Thus the above C program to perform linear search using function is been
verified and executed successfully.

24
3. 2. ARRAY USING POINTERS

AIM

To write a program to print the elements of an array string manipulation using


pointers

ALGORITHM

Step 1 : Start the program

Step 2 : Read the value of n

Step 3 : Assign i=0

Step 4 : Repeat until the i<n

Step 5 : Read a+i

Step 6 : Compute i=i+1

Step 7 : Assign i=0 and repeat until i<n

Step 8 : Print *(a+i)

Step 9 : Stop the program

25
PROGRAM

#include<stdio.h>

void main()

int *a[10],i,n;

printf("\n Enter the size of array\n");

scanf("%d",&n);

printf("Enter the elements");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("\n The given elements are");

for(i=0;i<n;i++)

printf("\n a[%d]=%d",i,*(a+i));

26
OUTPUT:

Enter the size of array: 4

Enter the elements: 10 20 30 45

The given elements are:

a[0]=10

a[1]=20

a[2]=30

a[3]=45

RESULT

Thus the above C program to print the elements of an array string manipulation
using pointers is been verified and executed successfully.

27
3.3. LENGTH OF STRING

AIM:

To write a program to find the length of the string

ALGORITHM:

Step 1: Start the program

Step 2: Declare the character array and assign a string char s1[]= ”anand”

Step 3: call length(s1) function and assign to len.

Step 4: print len

Step 5: stop the program

28
PROGRAM

#include<stdio.h>
#include<string.h>
int l(char*);
main()
{
int len;
static char s1[]="anand ";
len=l(s1);
printf("\n The length of String s1 %d\n",len);
}

int l(char*ptr)

{
int s=0;
while(*ptr!='\0')
{s+=1;
ptr++;
}
return(s);
}

29
OUPUT:

Length of string s1: 5

RESULT

Thus the above C program to find the length of the string is been verified and
executed successfully.

30
3.4. STRING CONCATENATE

AIM:
To write a C program to concatenate two strings.

ALGORITHM:

Step 1. Start the program

Step 2. Declare the character pointer variables as s1,s2,s3.

Step 3. Allocate memory for s1,s2 and s3 character pointer variables using

mallloc function

Step 4. Read the string s1 and s2.

Step 5. Repeat the steps 7 and 8 until ((c=*(s1+i)!=’\0’)

Step 6. Compute i=i+1

Step 7. Assign s3[i+k]=c

Step 8. Compute k=k+1

Step 9. Assign s3[i+k]=’\0’

Step 10. Print the concatenated string s3

Step 11. Stop the program

31
PROGRAM

#include<stdio.h>
#include<malloc.h>
#define length 30
#include<conio.h>
main()
{
char *s1,*s2,*s3,c;
int i,k;
//clrscr();
s1=(char *)malloc(length *sizeof(char));
s2=(char *)malloc(length *sizeof(char));
s3=(char *)malloc(2*length *sizeof(char));
printf("\n Enter the String S1\n");
scanf("%s",s1);
printf("\n Enter the String S2\n");
scanf("%s",s2);

i=0;
while((c=*(s1+i))!='\0')
{
s3[i]=c;
i++;
}
k=0;
while((c=*(s2+k))!='\0')
{
s3[i+k]=c;
k++;
}
s3[i+k]='\0';
printf("\n the concatenated string is %s",s3);
printf("\n");
}

32
Output:

Enter the String1: Suresh

Enter the String2: Kumar

Concatenated String is Suresh Kumar

RESULT

Thus the above C program to concatenate two strings is been verified and
executed successfully.

33
3.5. FILE HANDLING

AIM:

To write a C program to copy the contents of one file to another.

ALGORITHM:

Step 1. Start the program

Step 2. Declare the file pointers fp1 and fp2.

Step 3. Read the source filename –fname1

Step 4. Read the target filename-fname2

Step 5. Open the file fname1 under read mode assigned to file pointer-fp1.

Step 6. Open the file fname2 under write mode assigned to file pointer-fp2.

Step 7. If(fp1==NULL) then step 8 else step 9

Step 8. Print file fname1 cannot be opened for reading and then goto step16

Step 9. If(fp2==NULL) then step 10 else step 11

Step10. Print file fname2 cannot be opened for writing and then goto

step16

Step 11. Assign c=getc(fp1)

Step 12. Write the character in fp2

Step 13. Assign c =getc(fp1)

Step 14. Close all files(fname1 and fname2)

Step 15. Stop

34
Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp1,*fp2;
int c;
char fname1[40],fname2[40];
clrscr();
printf("\n Enter the source file\n");
gets(fname1);
printf("\n Enter the target file\n");
gets(fname2);
fp1=fopen(fname1,"r");
fp2=fopen(fname2,"w");
if(fp1==NULL)
{
printf("\n Cannot open %s for reading \n",fname1);
getch();
exit(1);
}
else if(fp2==NULL)
{
printf("\n Cannot open %s for writing \n",fname2);
getch();
exit(1);
}else
{
c=getc(fp1);
while(c!=EOF)
{
putc(c,fp2);
c=getc(fp1);
}
fclose(fp1);
fclose(fp2);
printf("\n File successfully copied\n");
}
getch();
}

35
Output:

Enter the source file: f1.txt

Enter the target file : f2.txt

File successfully copied.

RESULT

Thus the above C program to copy the content of one file to another is been
verified and executed successfully.

36

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy