Вы находитесь на странице: 1из 32

K.S.

R COLLEGE OF ENGINEERING, TIRUCHENGODE-637215


DEPARTMENT OF ELECTRICAL AND ELECTRONICS
ENGINEERING

LAB MANUAL

Prepared By
S.chinnaiya, Sr.lecturer/EEE

SUB CODE / SUBJECT: COMPUTER PRACTICE LABORATORY


SEMESTER / YEAR: II-SEMESTER / I YEAR

STAFF INCHARGE HOD

1
LIST OF EXPERIMENTS

SL.NO Name of the experiment Signature


I UNIX
1) BASIC COMMAND

2) BASIC COMMANDS – II

3) PROCESS MANAGEMENT
COMMANDS

II SHELL PROGRAMMING
1) ADDITION OF TWO NUMBERS.
2) GREATEST AMONG THREE NUMBERS.
3) TO FIND THE GIVEN NUMBER IS EVEN
OR ODD.

4) TO FIND THE GIVEN NUMBER IS


EQUAL OR NOT.
5) TO FIND THE GIVEN YEAR IS LEAP
YEAR OR NOT.

6) TO FIND THE SUM OF ‘N’ NUMBERS.

7) TO FIND THE POWER OF A GIVEN


NUMBER.
8) ARITHEMATIC OPERATIONS USING
CASE STATEMENT.
9) TO FIND THE FACTORIAL OF THE
GIVEN NUMBER.
10) TO FIND THE FIBONACCI SERIES OF
THE GIVEN NUMBER.

2
III C – PROGRAM ON UNIX
1) DYNAMIC STORAGE ALLOCATION
i. ALLOCATING MEMORY
ii. ALTERING THE ALLOCATED MEMORY
2) POINTERS
i. WAP TO ADD THE SUM OF NUMBER
USING POINTER
ii. WAP TO SORT A GIVEN NUMBER USING
POINTER

3) FUNCTIONS
i. CONVERSION OF LOWER CASE TO
UPPER CASE
ii. ADDDITION OF TWO NUMBERS
iii. FIND THE CUBE OF GIVEN NUMBERS
(USING CALL BY VALUE)
iv. INTERCHANGING TWO VALUES (USING
CALL BY REFERENCE)
v. PROGRAM TO READ CHARACTERS AND
PRINT REVERSELY USING RECURSION
vi. FACTORIAL OF NUMBER USING
RECURSION
4) FILE HANDLING
i. INPUT/OUTPUT OPERATIONS ON FILE
ii. TO READ INTEGER NUMBERS UP TO 12
AND STORE EVEN AND ODD NUMBERS
IN SEPARATE FILES
iii. HANDLING THE FILE WITH FSCANF()
AND FPRINTF()
iv. IN A GIVEN NUMBERS FIND WHICH IS
EVEN AND WHICH IS ODD NO’S PRINT
AS PER NO’S USING FILES
v. PROGRAM FOR REVERSING WORDS IN
A FILE
vi. MERGING OF TWO FILES

3
EX.NO:
UNIX-BASIC COMMANDS-I
Date:

AIM: To execute the following Unix commands.

PROCEDURE:

1. CAT COMMAND
The cat command is used to create file.
Syntax:$ cat>filename
After finished typing press ctrl+d to save and end of file.

2. DISPLAY CONTENTS OF A FILE


The cat command is also used to view the contents of a specified file
Syntax: $ cat filename
Option:
-v -> used to display non-printing characters
-n->used for numbering the lines.
The cat command serves to cancatenate mutiple files into a single file.
Syntax: $cat file1 file2>file3.

3.WORKING WITH DIRECTRORIES:


a) Current Working Directory:
Every user has logged into the system is always provided with a working
directory of his own called Current Working directory.
The pwd command is provided to knew the current working directory,
pwd abbreviation for “print working Directory”
Syntax: $pwd .

b) Create a Directory:
The mkdir is use to create an empty directory in a disk.
Syntax: $mkdir dirname

c) Removing a Directory:

4
The rmdir is used to remove a directory from disk. Before removing a
directory ,the directory must be empty.
Syntax: $ rmdir dirname.

d) Changing the Working Directory:


The cd command is used to move from one directory to another.
Syntax: cd dirname.

e) Clearing the Screen:


This command clears the screen and places a $ prompt at the top left
corner of the screen.
Syntax: $tput clear
4.PLAYING WITH FILES:
a) Copying a File:
This cp command is used to copy the contents of one file to another and
the file from one place to another.
Syntax:$ cp old_file new_file
Option:
-I- used to warn the user before overwriting the destination file.
-R- used to copy an entire directory structure.

b) Removing a File:
This rm command used to remove or erase an existing file.
Syntax: $ rm filename.
Option:
-I- used to ask the user for confirmation before removing each file.
-r-used to search and delete all the files and subdirectories.
-f-used to prompt for removal if a file is write protected.

c) Moving a File:
The mv command is used to move a file from on place to another. It
removes a specified file from its original location and places it in specified
location.
Syntax:mv old-finename new-filename.

d) Listing Files and Directories:


Whenever you logon to the system you are automatically placed inside a
special directory called Home directory to view the contents of a directory, the ls
command is used.

Syntax: $ls
5
Option Purpose
-x Display multicolumnar output
-t Lists files and subdirectories with time order
-r Lists files and subdirectories in reverse order
-A Lists all files excluding. and…
-a Lists all files including the normally hidden files
-c Lists all by inode modification time.
-I Lists the inode for each file
-h Lists names of hidden files also
-q List file names having non-printable characters
-l Lists permission owner size modification time etc
Along with the file and directory names

e) Directing Output to a File:


The ls command lists the files on the terminal. Using the redirection
operator ‘>’ we can send the output to file instead of showing it on the screen.
Syntax: $ ls>filename (No output on the terminal)

f) Counting Number of Words in a File:


The wc command is used to count the number of words, lines and
character in a file.
Syntax:$ wc filename
Purpose

Option
-l Used to display only number of lines
-w Used to display only number of words
-c Used to display only number of characters

g) File Types –The File Command


Even though there are basically three types of files, we may often require to know
more about these files. For instance possible that two files may be of the same type but
one contain text the other may contain executable code. So Unix provides the file
command to determine the type of the file, especially of an ordinary file.
Syntax: file filename.

5. METACHARACTERS OF UNIX:
Metacharacter are special character that are at a higher and abstract level
compared to most of other character in Unix. The shell understands and interprets these
metacharacters in a special way.
6
*--- Specifies number of characters
?--- Specifies a single character.
Example:
$ls[t]* Lists the files whose names begin with
‘t’
Or ‘T’
$ls[a-m]* Lists the files whose names begin with
alphabets from ‘a’ to ‘m’
!: Is used to specifies NOT
$ls[!a-g] Lists all files whose names begins with
from character ‘h’ and also lists all files
and directories with uppercase letters

6. FILE PERMISSIONS:
File permission is way of controlling the accessibility of file for each of three
users namely users,groups and others.

There are three types of file permission as under.


r-read
w-write
x-execute
The File permission can be divided into three parts of three bits each

First three bits Owner of the file


Group to which the owner of
Next three bits the file belongs
Others
Last three bits

a) Changing file permission:


The chmod command is used to set the three permissions for all categories
of user for a file.
Syntax:$chmod category operation permission file
Category Operation Permission
u- Users + Assign r-read
g- Group - Remove w - write
o-Others = Assign absolutely x - execute
a-All

Syntax: $ls myfile.


b) Changing file permissions:
The changing command is used to set the three permissions for all
categories of user for files.

7
Syntax: chmod category operation permission file
Where
Category is the usertype.
Operation is used to assign are remove permission
Permission is the type of permission
File are the file to assign are remove permission.
Options:

Category Operation Permission


u-user + - Assign R-read
g-group - Remove W-write
o-other = assign absolutely X-execute
a-all

Example: $chmod u –wx add.c


Removes write and execute permission for user for add.c file.

7.GENERAL PURPOSE COMMANDS:


a) The date command:
The date command is used to display the current data with day of week,month,day
time and the year.
Syntax: $date
Options:
Format Purpose Syntax
+%m To display onl $date +%m
month
+%h To display month $date +%h
name
+%d To display day of $date +%d
month
+%y To display last two $date +%y
digits of the year
+%H To display hours $date +%H
+%m To display minutes $date +%m
+%s To display seconds $date +%s

b) The echo command:


The echo command is used to print the message on the screen, whatever
you happen to type on the line.
Syntax: $echo text
c) Unix calendar:
The calendar command helps us to keep track of our days.

Syntax: $cal month or year.

8
d) Unix calculator:
Unix offers an online calculator and can be invoked by the command bc.
This command is programmable and has complex functions.

Syntax: $ bc
e) The who command:
The Who command is more powerful and used to display data about all
the users, who are currently logged into the system.

Syntax: $who
The –h option in who command displays the column header.

Syntax: $who –h
f)The who am I command:
The who command with am and I displays a single line of output pertain the login
details of the user.
Syntax: $who am I
g) The tty command:
The tty(teletype) command is used to know the terminal name that we are
using.
Syntax: $tty
h) Change the password:
If your account still does not have a password, or has one that already
known to others. Using the passwd command can change the password.

Syntax: passwd

9
EX.NO:
UNIX-BASIC COMMANDS-II
Date:

AIM: To execute the following Unix commands

PROCEDURE:

Command Grouping:

1.The Semicolon (;)


The semicolon operator used to separate multiple commands at the command line.

Syntax:
$ Command1; command2;……………command n

2.The && operator


The && operator signifies the logical AND operation appears in between two or
more valid Unix Commands .It means that only if the first command is successfully
executed then the next command will executed.

Syntax:
$ Command1 && command2………&& command n
3.The || operator
The || operator signifies the logical OR operator appears in between two or more
valid Unix Commands .It means if the first command will happen to be unsuccessful it
will continue to execute next commands.

Syntax:
$ command1 || command 2 || …………….. || command n
Filters:

1.The head filter


As the name implies, it displays the first ten lines of the file

Syntax:
$ head filename
2.The tail Filter
The tail command is the opposite of the head command . It displays 10 lines of a
file from the end of the file

10
Syntax:
$ tail filename

3.The pg filter
The pg command shows the file page by page. A screen full of information is
displayed, after which the pg command displays a prompt and pauses for the user to
strike the ‘Enter’ key to continue scrolling.

Example $ ls-l | pg
4.The more command
If a file is too large for its contents to fit on the screen, it will scroll your screen
when you view it. The more command almost does the same thing as the pg command
but for few differences. To continue scrolling with the more command you have to press
‘space bar’ not the ‘enter’ key as in pg command
Example $ more myfile
‘more’ command can also works with multiple files

Example $ more file1 file2 file3


Some internal commands of more:
Space bar - Scrolls one screenful forward
F - Scrolls one screenful forward
B - scrolls one screenful backward
J - scrolls one line forward
K - scrolls one line backward
100G - goes to line number 100
G - goes to last line of the file.

5.The Grep Command


The Grep command is used to search and print specified patterns from a file. The
grep command is the abbreviation of “global regular expression and print”

Syntax: $ grep [option] pattern file(s)


6.Sort command
The sort command is used to sort the contents of a file

Syntax: $ sort filename


Options:
-r  sorts and displays the file contents in reverse order
-c  checks if the file is sorted
-n  sorts numerically
-u  Removes Duplicate records
-m list  merges sorted files in list
7.nl command
The nl command adds line numbers to a file and it displays the file and not
provides to access or to edit, simply displays the contents it on the screen

11
Syntax: $ nl filename

8.cut command
We can select specified fields from a line of text using cut command

Syntax : $ cut –c filename.


Where –c is the option cut on the specified character position from each line

9.paste command
When we cut some of the characters in a file using the cut command those can be
pasted back with the paste command.

Syntax: $ paste filename

I/O Redirection :

1.Redirecting standard output to a file

Syntax: $ command > file


The symbol ‘>’ is the redirection operator. It sends the output of the command to a
file or a device such as a printer, disk, tape etc.

2. Reading standard input from a file

Syntax: $ command < file

The symbol ‘<’ is the redirection operator, it reads the input from the specified
file.

3. Redirecting Standard Error


It represents the standard error and it includes all the error messages written to the
terminals

Syntax: $ Command > error file


4.Combining standard input and output
Standard Input and output can be combined in the same command

Syntax: $ command <file1> file2

Pipes:
A pipe is a mechanism by which the output of one command can be channeled
into the input of another command.

Syntax: $ command | tee file

12
Transfer data between devices/users:

Communication is the transformation of data between the users or devices.


Communication in Unix can be using the following commands.

1. The mesg command:


The message command is used to give permissions to other users to send
messages to your terminal.

Syntax:$ mesg y
where
mesg is the command used to give permission
y specifies yes to communicate.
2.The write command
The write command is used to communicate with other users that are
logged in at the same time. before using this command we have to make sure that
our terminal has the write permission.

Syntax:$ write user_name

3.Wall command
The wall command is used to send message to all users those who are
currently logged in using the Unix server and have permission to receive message

Syntax: $ wall message.


4.The news command
The news command permits users to read messages published on the
system Administrator. The important news is saved as a
file in /usr/news Syntax: $ news directory.

Options
-n  This option will only list the names of the news item not yet
read for /usr/news directory Example $ news –n
-s  This option provides a count of the unread news item in
the /usr/news directory. Example $ news –s

Mail:
Mail refers, the textual information that can be electronically transferred from one
user to another user.

1.Mail command:
The mail Syntax: $ mail username command is used to send
mail to another user. Body of the mail
Ctrl+d

13
2.Sending mails with subject:
We can send mail to user with subject specified in mail command itself

Syntax:$ mail –s ‘subject’ user


where –s is the option specifies subject

3.To view the contents of the mail:


To view the contents of received mail follow

4. To send reply to the specified user:

Syntax: $ reply user


body of the mail
ctrl+d

14
EX.NO:

UNIX- PROCESS MANAGEMENT COMMANDS


Date:

AIM:
To execute the process management commands in Unix environment

PROCEDURE:

1. Process Status:
The PS command is used to display the characteristics of a process. It reads through
the kernel data structure or process table to fetch the attributes of a process. This
command involved without options simply lists out the process associated with a user
at a particular terminal.

Syntax : $ ps
Options in PS command:

Option Meaning
-f Generates full listing showing
eight attributes of a process
-u usr Process of User only
-a Process of all users
-l Generates a long listing
-t term Process running on terminal term
-e or –A All process including user and
system processes.

2. Running jobs in background:


Since Unix is a multitasking operating system, which allows us to run
more tasks at a time, there can be only one job in the foreground. The remaining jobs has
to run in the background.

2.1 & No logging out

The ‘&’ operator is used to run a process in the background and it is a shell
operator. In this case the parent process does not wait for the child process death, it just
terminates the command line with ‘&’, this command run in the background.
Eg. $ sort abc &
3923
where 3923 is the jobs PID

15
2.2 The nohup command

This command is used to execute the process, even after the user has logged out.
Eg. $ nohup cal &
5669
sending output to nohup.out

3. Execution of job with low priority: nice


All the Unix system processes are executed with equal priority. This is not suitable
because, the highest priority process will be executed before the lowest priority process.
So Unix provides he nice command with ‘&’ operator to reduce the priority of jobs.

3.1 The at command


The ‘at’ command is used to execute commands at a later time

Syntax: $ at time
Eg: $ at 8 am

3.2 The sleep command


The sleep command is used to delay a process for a particular interval of
time.
Syntax: $ sleep time

4. Killing processes with signals


The signal in the Unix is used to communicate the occurrence of a event to
a process. Here, each signal is identified by a number and designed to perform a specific
function because the same signal number may represent two different signals on the
different machines. The kill command is used to send a signal with the intensity of killing
the processes. It is an internal command

Eg. $ kill 5669

16
EX.NO:

SHELL PROGRAMMING
Date:

AIM: To write the shell programs for the following

S.NO TITILE
CONDITIONAL STATMENTS
1 Addition of two numbers.
2 Greatest among three numbers.
3 To find the given number is even or odd.
4 To find the given number is equal or not.
5 To find the given year is leap year or not.
TESTING AND LOOPS
6 To find the sum of ‘n’ numbers.
7 To find the power of a given number.
8 Arithematic operations using case statement.
9 To find the factorial of the given number.
10 To find the fibonacci series of the given number.

ALGORITHM:

Step-1: Start the Program.


Step-2: Read the values.
Step-3: Calculate the values using the above operations.
Step-4: Display the result.
Step-5: Stop the Program.

PROGRAM:
1. ADDITION OF TWO NUMBERS.

echo TO FIND THE ADDITION OF TWO NUMBERS.


echo ENTER THE NUMBERS.
read a b c
c = ` expr $a + $b `
echo THE ADDITION OF TWO NUMBERS IS $c

2.GREATEST AMONG THREE NUMBERS.

echo TO FIND THE GREATEST AMONG THREE NUMBERS.


echo ENTER THE THREE VALUE S.
read a b c

17
if [ $a –gt $b –a $a –gt $c ]
then
echo A IS GREATER.
elif [ $b –gt $c ]
echo B IS GREATER.
else
echo C IS GREATER.
fi

3.TO FIND THE GIVEN NUMBER IS EVEN OR ODD.

echo TO FIND THE GIVEN NUMBER IS ODD OR EVEN.


echo ENTER THEVALUE.
read n
a = ` expr $n % 2 `
if [ $a –eq 0 ]
then
echo NUMBER IS EVEN
else
echo NUMBER IS ODD
fi

4.TO FIND THE GIVEN NUMBER IS EQUAL OR NOT.

echo TO FIND THE GIVEN IS EQUAL OR NOT.


echo ENTER THE VALUES.
read a b
if [ $a –eq $b ]
then
echo THE TWO NUMBERS ARE EQUAL
else
echo THE TWO NUMBERS ARE NOT EQUAL
fi

5.TO FIND THE GIVEN YEAR IS LEAP YEAR OR NOT.

echo TO FIND THE GIVEN YEAR IS LEAP YEAR OR NOT


echo ENTER THE YEAR
read n
y = ` expr $n % 4 `
if [ $y –eq 0 ]
then
echo THE GIVEN YEAR IS LEAP YEAR
else
echo THE GIVEN YEAR IS NOT A LEAP YEAR
fi

18
6..TO FIND THE SUM OF ‘N’ NUMBERS.

echo TO FIND THE SUM OF ‘N‘ NUMBER


echo get the value for n.
read n
i=1
sum=0
while [ $I –le $n ]
do
echo ENTER THE NUMBER
read b
sum = ` expr $sum + $b `
i= ` expr $i + 1 `
done
echo THE SUM OF ‘N’ NUMBER IS $sum.

7.TO FIND THE POWER OF A GIVEN NUMBER.

echo TO FIND THE POWER OF A GIVEN NUMBER


echo ENTER THE VALUE
read value
echo ENTER THE POWER FOR THE VALUE
read power
i=1
p=1
while [ $i –le $power ]
do
p= ` expr $p \* $value `
i= `expr $i + 1`
done
echo THE RESULT IS $p

8.ARITHEMATIC OPERATIONS USING CASE STATEMENT.

echo ARITHEMATIC OPERATIONS USING CASE STATEMENT


echo ENTER THE TWO VALUES
read a b
echo ENTER THE CHOICE:
echo 1. ADD 2. SUB 3. MUL 4. DIV 5. MODULO DIV 6. EXIT
read choice
case $choice in
expr $a + $b ;;
expr $a - $b ;;
expr $a /* $b ;;
expr $a / $b ;;
expr $a % $b ;;

19
esac

9.TO FIND THE FACTORIAL OF THE GIVEN NUMBER.

echo TO FIND THE FACTORIAL OF THE GIVEN NUMBER


echo ENTER THE VALUE:
read n
f=1
i=1
while [ $I –le $n ]
do
f= ` expr $f \* $I `
i= ` expr $i + 1 `
done
echo THE FACTORIAL OF $n IS $f

10.TO FIND THE FIBONACCI SERIES OF THE GIVEN NUMBER.

echo TO FIND THE FIBONACCI SERIES OF THE GIVEN NUMBER


echo ENTER THE VALUE
read n
f= -1
i= 0
while [ $I –lt $n ]
do
c= ` expr $f + $b `
echo $c
f= $b
b= $c
i= ` expr $i + 1 `
done

20
EX.NO:

C PROGRAMMING ON UNIX
Date:

AIM: To write the c programs for the following

S.NO TITILE

Dynamic Storage allocation


1 Allocating Memory
2 Altering the allocated memory
3 pointer
4 Functions
5 File Handling

ALLOCATING MEMORY
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
Void main()
{
int *a,*n,size;
clrscr();
printf(“Enter the size……”);
scanf(“%d”,&size);
n=(int *)malloc(size * sizeof(int));
printf(“Address of the first byte is…..%u\n”,n);
printf(“enter the values…..”);
for(a=n;a<n+size;a++)
scanf(“%d”,a);
printf(“Printing the values……\n”);
for(a=n+size-1;a>=n;a--)
printf(“%d is stored in address %u\n”,*a,a);

ALTERING THE ALLOCATED MEMOREY:

#include<stdio.h>
#include<stdlib.h>
main()
{
char *p;
p=(char*)malloc(6);
strcpy(p,”MADRAS”);

21
printf(“MEMOREY CONTAINS:%s/n”,p);
strcpy(p,”CHENNAI”);
printf(“MEMOREY NOW CONTAINS:%s/n”,p);
free(P);
}

WAP TO ADD THE SUM OF NUMBER USING POINTER


#include<stdio.h>
main()
{
int b,total;
int a[5];
int *c
for(b=0;b<5;b++)
{
Printf(“Enter the no %d”,b+1);
Scanf(“%d”,&a[b]);
}
c=a;
for(b=0;b<5;b++)
{
Printf (“%d”,*c);
Total=total+*c;
c=c+1;
}
Printf(“/n total =%d”,total);
}

OUTPUT:
Enter the no 10 20 30 40 50
Total 150

WAP TO SORT A GIVEN NUMBER USING POINTER


#include<stdio.h>
main()
{
int j,k;
int arr[5];
unt *a;
a=arr;
for (j=0;j<5;j++)
{
Printf(“Enter the no %d”,j+1);
Scanf(“%d”,&arr[j]);
}
Printf(“/nbefore sorting”);

22
for(j=0;j<5;j++)
Printf(“/n %d”,arr[j]);
a=arr;
for(j=0;j<5;j++)
for(k=0;k<5-j-1;k++)
if(*(arr+k)>*(arr+k+1))
swap(arr+k,arr+k+1)
printf(“after sorting”);
for(j=0;j<5;j++)
Printf(“/n %d”,arr[j]);
}

Swap(int *b,int *c)


{
Int temp;
Temp=*b
*b=*c;
*c=temp;
}

OUTPUT:
ENTER THE NUMBER :8
ENTER THE NUMBER :6
ENTER THE NUMBER :7
ENTER THE NUMBER :5
ENTER THE NUMBER :4
Before sorting
86754
After sorting
45678

CONVERSION OF LOWER CASE TO UPPER CASE [A LINE OF TEXT]


#include<stdio.h>
#include<conio.h>
#include<ctype.h>
Void main()
{
Char txt[80];
Inti,t;
Clrscr();
For(i=0;(txt[i]=getchar())!=’\n’:++i);
T=I;
For(i=0;i<t;++i)
Putchar(toupper(txt[i]);
}

23
OUTPUT
Ksrce

KSRCE

ADDITION OF TWO NUMBERS:


main()
{
int add((int,int);
int a,b,c;
printf(“ENTER THE TWO NUMBERS”);
scanf(“%d %d”,&a,&b);
c=add(a,b);
printf(“result is . . . . .%d”,c);
}
int add(int x,int y)
{
int z;
z=x+y;
return(z);
}

OUTPUT:
ENTER THE TWO NUMBERS 22 20
result is . . . . .42

FIND THE CUBE OF GIVEN NUMBERS(USING CALL BY VALUE):


#include<stdio.h>
int cube(int x)
main()
{
int n;
printf(“ENTER THE NUMBER”);
scanf(“%d”,&n);
printf(“cube of . . . . .%d is %d”,n,cube(n));
}

int cube(int);
{
x=x*x*x;
return(x);
}

OUTPUT:
cube of 5 is . . . .125

24
INTERCHANGING TWO VALUES (USING CALL BY REFERENCE):
#include<stdio.h>
void interchange(int *a,int *b);
main()
{
int i=10,j=12;
printf(“i and j values before: %d %d /n”,i,j);
void interchange(&i,&j);
printf(“i and j values after: %d %d /n”,i,j);
}

void interchange(int *a,int *b);


{
int t;
t=*a;
*a=*b;
*b=t
}

OUTPUT:
i and j values before :10 12
i and j values after : 12 10

PROGRAM TO READ CHARACTERS AND PRINT REVERSELY USING


RECURSION:
main()
{
void reverse();
printf(enter the line of text….in);
reverse ();
}

void reverse()
{
char c;
if ((c=getchar()!=’\n’)
reverse ();
putchar(c);
}

OUTPUT:

ENTER THE LINE OF TEXT . . . . . . . .VINAYAKA


AKAYANIV

25
FACTORIAL OF NUMBER USING RECURSION :
main()
{
int a;
printf(“Enter The Number”);
scanf(%d”,&a);
printf(“The factorial of %d=%d”,a,rec(a) );
}
rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x-1);
return(f)
}

OUTPUT:
ENTER THE NUMBER 5
THE FACTORIALOF 5 =120

INPUT/OUTPUT OPERATIONS ON FILE :


#include<stdio.h>
main()
{
FILE *fp;
char c;
fp=fopen(“in.Dat”,”w”);
printf(“DATA INPUT /n”);
while(c=getchar())!=EOF)
putc(c,fp);
fcolsoe(fp);
fp=fopen(“in.Dat”,”r”);
printf(“DATA INPUT /n”);
while(c=getchar(fp))!=EOF)
printf(“%c”,c);
fclose(fP);
}
O/P : DATA INPUT
C LANGUAGE IS EASY TO LEARN
DATA OUTPUT
C LANGUAGE IS EASY TO LEARN

26
TO READ INTEGER NUMBERS UP TO 12 AND STORE EVEN AND ODD
NUMBERS IN SEPARATE FILES
#include<stdio.h>
main()
{
FILE *fp1, *fp2, *fp3;
int I,no;
clrscr();
fp1=fopen(“number”,”w”);
printf(“ENTER 12 NUMBER/n”);
for(i=1;i<12;i++)
{
scanf(“%d”,&no);
putw(no,fp1);
}
fclose(fP1);
fp1=fopen(“number”,”r”);
fp2=fopen(“enum”,”w”);
fp3=fopen(“onum”,”w”);
while((no=getw(fp1))!=EOF))
{
if((no%2)==0))
putw(no,fp2);
else
putw(no,fp3);
}
fclose(fP1);
fclose(fP2);
fclose(fP3);
fp2=fopen(“enum”,”r”);
fp3=fopen(“onum”,”r”);
printf(“EVEN NUMBERS/n”);
while((no=getw(fp2))!=EOF))
putc(“%d”,no);
printf(“ODD NUMBERS/n”);
while((no=getw(fp3))!=EOF))
putc(“%d”,no);
fclose(fP2);
fclose(fP3);
}
O/P :ENTER 12 NUMBERS
1 2 3 4 5 6 7 8 9 10 11
EVEN NUMBERS
2 4 6 8 10
ODD NUMBERS
1 3 5 7 9 11

27
HANDLING THE FILE WITH FSCANF () AND FPRINTF () :
#include<stdio.h>
main()
{
FILE *fp1,;
int age,i;
char filename[25],name[25];
float salary;
clrscr();
printf(“give the file name”);
scanf(“%s”,filename);
fp1=fopen(filename,”w”);
printf(“ENTER THE NAME,SALARY AND AGE/N”);
for(i=1;i<3;i++)
{
fscanf(stdin,“%s%f%d”,&filename,&salary,&age);
fprintf(“fp1, “%s%f%d”,filename,salary,age”);
}
fclose(fP1);
fprintf(stdout,”/n /n”);
fp1=fopen(“filename”,”r”);
printf(“name salary age”);
for(i=1;i<=3;i++)
{
fscanf(fp1,”%s %f %d”,name,&salary,&age);
fscanf(stdout,”%s %f %d”,name,salary,age);
}
fclose(fP1);
}

O/p :
ENTER THE FILENAME VRB
ENTER THE NAME, SALARY,AGE
AJAY 10000 27
UDAY 12000 22
RAJA 9000 19

NAME SALARY AGE


AJAY 10000 27
UDAY 12000 22
RAJA 9000 19

28
WAP FROM A GIVEN NO OF NUMBERS FIND WHICH IS EVEN AND WHICH
IS ODD NO’S PRINT AS PER NO’S USING FILES :
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
Void main()
{
FILE *fp1, *fp2,*fp3;
Int I,number,f;
Clrscr();
Fp1=fopen(“data”,”w”);
Scanf(“%d”,&f);
For(i=1; i<=f; i++)
{
Scanf(“%d”, &number);
If(number==-1)
Break;
Putw(number,fp1);
}
Fclose(fp1);
Fp1=fopen(“data”, “r”);
Fp2=fopen(“odd”, “w”);
Fp3=fopen(“even”, “w”);
While((number=getw(fp1))!=EOF)
{
If(number%2==0)

Putw(number,fp3);
Else
Putw(number,fp2);
}
Fclose(fp1);
Fclose(fp2);
Fclose(fp3);
Fp2=fopen(“odd”, “r”);
Fp3=fopen(“even”, “r”);
Printf(“contents of odd file”);
While((number=getw(fp2))!=EOF)
{
Printf(“\t%d”,number);
}
Printf(“\ncontents of evev file”);
While((number=getw(fp3))!=EOF)
{
Printf(“\t%d”,number);
}

29
Fclose(fp2);
Fclose(fp3);
Getch();
}

PROGRAM FOR REVERSING WORDS IN A FILE :


#include<stdio.h>
#include<conio.h>
#include<string.h>
Void main()
{
Char c[10] [20],s[100],s1[100],t[25],i1;
Int i=0,j,k,l,x,m=0,z=0;
Clrscr();
FILE *f1;
Printf(“\nType a line.At end leave one space:\n”);
F1=fopen(“textfile”, “w”);
Gets(s1);
Fprintf(f1,”%s”, s1);
Fclose(f1)
F1=fopen(“textfile”, “r”);
While((i1=getc(f1))!=EOF)
{
S[i++]=i1;
}
K=strlen(s)
For(i=0;i<k;i++)
{
If(s[i]==’ ‘)
{
For(x=0,l=I; l>=z; l--, x++)
C[m][x]=s[l];
Z=i+1;
M++;
}
}
Fclose(f1);
Printf(“\n reverse order is :\n”);
For(i=0;i<m;i++)
Printf(“%s”, c[i];
Getch();
}

30
MERGING OF TWO FILES :
#include<stdio.h>
#include<conio.h>
#include<string.h>
Void main()
{
Char c;
FILE *p1,*p2,*p3;
Clrscr();
P1=fopen(“data1”,”w”);
Printf(“input data for file 1:\n”);
While((c=getchar())!=EOF)
{
Fprintf(p1,”%c”,C);
}
Fclose(p1);
P2=fopen(“data2”,”w”);
Printf(“\n input data for file 2:\n”);
While((c=getchar())!=EOF)
{
Fprintf(p2,”%c”,C);
}

Fclose(p2);
p1=fopen(“data1”, “r”);
p2=fopen(“data2”, “r”);
p2=fopen(“data3”, “w”);
While((c=getc(p1))!=EOF)
{
Fseek(p1,-1,1);
While((c=getc(p1))!=’\n’)
{
Putc(c,p3);
Putc(c,p3);
}
While((c=getc(p2))!=’\n’)
{
Putc(c,p3);
Putc(c,p3);
}
}
While((c=getc(p2))!=EOF)
{
Putc(c,p3);
}
Fclose(p1);

31
Fclose(p2);
Fclose(p3);
P3=fopen(“data3”,”r”);
Printf(“\nthe merged file is:\n”);
While ((c=getc(p3))!=EOF)
{
Putchar(C);
}
Fclose(p3);
Getch();
}

32

Вам также может понравиться