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

# Subject Name: Computer Programming.

Semester
: II

## SAVITRIBAI PHULE SHIKSAN PRASARK MANDALS

SKNSCOE PANDHARPUR
DEPARTMENT OF FIRST YEAR ENGINEERING

LAB MANUAL

F .E.
COMPUTER PROGRAMMING
Prepared By
Prof. S. S. INGOLE
(Assist. Professor in Computer Department)

Semester
: II

## SAVITRIBAI PHULE SHIKSAN PRASARK MANDALS

SKNSCOE PANDHARPUR

CERTIFICATE
This is to certify that
Mr./ Miss _______________________________________,
Of Class FIRST YEAR Roll No. _____ Has completed all the practical
work in the subject COMPUTER PROGRAMMING satisfactorily in
the Department of FIRST YEAR ENGINEERING as prescribed by
University of Solapur, in the academic year 2011 -2012.

Staff In-charge

Principal

Semester
: II

## SAVITRIBAI PHULE SHIKSAN PRASARK MANDALS

SKNSCOE PANDHARPUR
INDEX

## Name : _____________________________ Roll No._____________

Class :____________________ Subject : _______________________
Name of Experiment
Sr.No
1.

2.

loops

3.

## Write a program to perform operations on matrices (Addition,

Subtraction, multiplication of two matrices.).

4.

## Write a program to perform swapping of two nos. by using

call by value and call by reference.
Write a program to perform file handling.

5.
6.

## Write a program to finding the factorial of given no with

recursion and without recursion.

7.

or not.

8.

Page. No

Date

Sign

Semester
: II

Experiment No.

Title

:1

types

Date

Semester
: II

## Problem Statement : Introduction of Algorithm, Flowchart and data types

Objective

:
1. To understand history of c language
2. To understand the concept of algorithm and flowchart.
3. To understand the concept of data types

Theory

INTRODUCTION TO C:
C is a programming language written by Dennis Ritche and developed at AT & T Bell Lab in USA
in 1972. C is a high level language. Most of the part of Operating System like Windows, UNIX, and
Linux are written in C. Many 3D gaming frame works are written in C language where speed is
important factor. Drivers are also written in C language.
ALGORITHM :
Algorithm is step by step procedure for solving a problem. Each step is called instruction. Algorithm
consists of English like sentences.
Characteristics of algorithm:
1. Input: It may accept zero or more inputs.
2. Output: It should produce at least one output [result].
3. Definiteness: Every instruction must be clear and well defined. There should not
be any ambiguity
4. Finiteness: It should end after finite number of steps. It should not enter into an
Infinite loop.
5. Effectiveness: Every operation must be simple and should complete in a finite
time.
ALGORITHM NOTATIONS:
Following notations are considered while writing an algorithm
1. Name of algorithm: It specifies problem definition to be solved.
2. Step number: Identification tag of an instruction.
3. Explanatory comment: It describes the operation and it is written within pair of
square brackets.
4. Termination: It specifies end of algorithm. It is generally stop statement.

Semester
: II

## Example1: Write an algorithm to calculate area of circle

Algorithm: Area of circle
Step2- [calculate the area of circle]
Step3- [print area of circle]
Print area of circle is = area
Step4- [End of algorithm]
Stop.
FLOW CHART :
Flowchart is diagrammatic representation of an algorithm. By using flowchart we can easily
understand and analyze the problem. Flowchart consist of different shapes and symbols indicating
different shapes and symbols used in flowchart with there function. All symbols are connected among
themselves to indicate the flow of information and processing. Flowchart is a quality improvement
tool for specifying the process of the program.
Examples of flow chart
Example1: Draw flowchart for finding average of given three numbers
Step1- [Start of algorithm]
START
Step3- [Calculate average]
sum= a+ b+ c;
Average= (sum)/3
Step4- [Display result]
Print average
Step5- [End of algorithm]
STOP.

START

sum = a+
b+c
Avg=sum/3
Print Average

STOP

Table A shows the different shapes used to draw flowchart with their function.

Semester
: II

TABLE A

Shapes

Name

Function
START and STOP.

Terminal
(Oval)

## It consist of first and last symbol

in flow chart

Parallelogram
Input, Output
Rectangle
Processing

Rhombus

Decision making

Arrows
Connection.

Circle

Continuation

Hexagon
Repetition/ Looping

Double sided
rectangle
(predefined
process)

are declared

Semester
: II

## Class Name: First Year

DATA TYPES
Data type indicates the type of data that variable can hold. Data or information consists of figures,
numbers and facts. If we want to save integer value with variable name x then declaration will be int
x where int is integer data type and x is variable name which stores integer value only. Data types are
categorized into.
a) Primary or Fundamental data type.
b) Derived data type.
c) User defined data type.

## Primary or Fundamental data type:

There are 5 primary data type in C as given below

Data Type
Integer
Float
Double
Character
Void

Keyword
int
float
double
char
void

Size in bytes
2
4
8
1
--

Table 1.1
Integer data type allows to store integer value only. The memory space allocated for integer data type
is 2 bytes for 16 bit machine. The range of integer data type is -32768 to +32767.
Floating data type store floating point values only. The range of float data type is -3.4e38 to
+3.4e38. The memory space allocated for float data type is 4 bytes for 16 bit machine.
Double data type store double value than the float. The range of double data type is -1.7e308 to
+1.7e308. The memory space allocated for double data type is 8 bytes for 16 bit machine.
Character data type is used to store single character or number. The range of character data type is
-128 to +127. The memory space allocated for character data type is 1 bytes for 16 bit machine.

Conclusion

Semester
: II

## Class Name: First Year

1. What is mean by algorithm?

2. What is mean by data types? And what are the different types of it explain with the
examples?

Semester
: II

Experiment No.

Title

:2

and loops

Date

Semester
: II

## Class Name: First Year

Problem Statement : Write a program to implement the control statements and loops
Objective

Theory

:
1. To understand concept of control statements.
2. To understand the concept of loop.

The statement used to control the program is called control statement. The flow of program execution
is controlled by these control statement. The control statement has three types:
1.
2.
3.

Selection
Repetition
Sequence

Selection: Selection control type is used to select condition from multiple conditions. After selecting,
that particular condition, it get execute. If else and switch case loops are used to select a condition
from multiple condition.
Repetition: Sometime user wants to repeat certain statements or instructions. By using initial and
final condition user can repeat the statement or instruction. The repetition starts from initial condition
and ends with when final condition occurs. While and for loops are used for repetition and Sequence.
Sequencing is the execution of statement or instruction depending upon conditions.
DECISION CONTROL STRUCTURE :
Decision making is used to select a particular condition from multiple conditions. If statement and Ifelse statement are the major decision making statement.
If statement:
The general form of if statement is as follows:

## if (this condition is true)

Entry

If condition

{
(Execute this statement);

Execute statement

}
True

False

Semester
: II

## Class Name: First Year

If-else statement.
If there are two group of statements and it is desired that one of them will be executed, if condition is
true and other will be executed if condition is false. In such situation if-else statement is used.
if(condition)

Entry

{
(Execute this statement1);

True

if condition

False

}
Execute statement 1

else

Execute statement 2

{
(Execute this statement 2);
}
LOOP CONTROL STRUCTURE :
Loop means repetition of instruction. The loop will iterate till given condition become true. When
condition becomes false the loop gets breaks. If user want to print his name 10 times, then there is no
need to write printf ( ) statement 10 times.
There are 3 loops control structure available in C:
1.
2.
3.

While loop
Do-while loop
For loop

While loop
The while loop starts with while keyword followed by condition. The general form of while loop is as
follows: Initialize loop counter
Start

while (condition)
{ Statement 1;
Statement 2;

Initialize

Test
Stop

Increment/decrement counter}
Body of loop
Increment

Semester
: II

## Class Name: First Year

When execution of program reaches at while loop, first condition is checked. If condition is true, it
will enter into body of loop. The body of loop iterate until the condition of while loop becomes false.
Do-while loop:
The main difference between while and do-while is checking of test condition. In while loop,
condition checks in the beginning of loop but in do-while loop condition, checks at the end loop.
The general form of do-while loop is:
Start

## Initializes loop counter

Initialize

do{

Body of loop

Statement 1;
Statement 2;

Increment

True
Test

False
Stop

---------------;
---------------;
Increment/decrement counter.
} while (condition);
For loop:
The for loop is most powerful and popular loop for repetition of instruction.The general form of
for loop is:
for (initialize counter; test counter; increment counter)
{

Do this;
Do this;
And this;

Semester
: II

## CASE CONTROL STRUCTURE :

Switch Statement:
The switch is case statement used to make decision from multiple choices. The general form of
switch case statement is as follows:
switch (integer expression )
{
case 1:
Do this;
Break;
case 2:

Do this;
Break;
--------------------Do this;
}
In switch statement, different conditions are called as cases. These cases are break by the keyword
break. Let discuss about how switch statement executed. Firstly expression is evaluated. Then value
of it matching with constant value of case statement. When match found then program execute
statement following that case. If match not found, then default statement will executed.
Conclusion:

Semester
: II

## Class Name: First Year

1. What is a control statement? Explain types of controls.

a) If

b) If else

c) while

d) do while

e) for

Semester
: II

Experiment No

Title

Date

Semester
: II

## Problem Statement : Write a program to perform swapping of two nos. by using

Call by value and call by reference

Objective

:1)To understand the basic concept of call by value and call by reference

Theory:
Up to now we used functions like printf( ), scanf( ), main( ), strcpy( ), strlen( ) etc. A function is
self contained block of statements that performs a specific task. Every C program is collection of
functions. If a program written by programmer is very large & complex and it is also difficult to test
and debug. Then such programs are divided into sub programs. These sub programs are easily
compiled and test. These sub programs are called functions.

Figure A
TYPES OF FUNCTIONS :
There are two types of Functions:
1. Library Functions: Library functions are readymade functions available in C so there is no need to
develop these functions. Library functions are not written by user.
2. User Defined Functions: User defined functions are to be developed by user. In this chapter, we will
focus on user defined functions.

## Elements of User Defined Functions

There are 3 elements of user defined functions.
1.
2.
3.

Function declaration
Function Call
Function Definition

Semester
: II

1.

## Class Name: First Year

Function Declaration:

Function declaration is just like a variable declaration. Function can be declared above main ( )
function. The general form of function declaration is:
Data type function_name (parameter list)
The program which call the function for execution is called calling function. The calling function can
communicate with called function by passing parameters, whereas the called function can
communicate with calling function by returning the value.
Example:
Declare the function with float data type for return value one integer and one float as a parameter.
The parameters are separated by ,.
If function is not having any parameter, then we can write void in bracket.
2.

Function Call:

A function call is called by writing the function name and passing parameter if required. The general
form of function call is as follows:
Function_name (Actual Parameter list)
Example:

Here, cube is function name and we pass number the parameter. Here number is called as
actual parameter. When compiler encounters a function call, the control is transferred to that
function. When execution of function completes, then automatic control is transferred to calling
function.
The function call has two types:
1.
2.

Call by value
Call by reference

## We will discuss these types in detail later.

3. Function Definition:
Function definition contains actual statements which to be executed. The general form of function
definition is:

Semester
: II

## Data type function_name (formal parameter list)

{
}
Example:
{
}
We know that when we pass number or variable to function call then these variables is called as
actual parameter. On other hand when we receive these values at function definition then the
variables are called as formal parameters.
CALL BY VALUE AND C ALL BY REFERENCE :
Call by Value:
When we call function by passing values or variables, the function is called as call by values
Call by Reference:
When we call function by passing address of variables, the function is called as call by reference.
Conclusion:

## Subject Name: Computer Programming.

Semester
: II

1. What is a Function?

Semester
: II

Experiment No

Title

Date

## Class Name: First Year

: Swapping of two nos. by using third variable and without using third
Variable.
:

Semester
: II

## Problem Statement : Write a program to perform swapping of two nos. by using

Third variable and without using third variable.

Objective

Theory

## In C programming language, variables can be referred differently depending on the

context. For example, if you are writing a program for a low memory system, you may
want to avoid copying larger sized types such as structs and arrays when passing them to
functions. On the other hand, with data types like integers, there is no point in passing by
reference when a pointer to an integer is the same size in memory as an integer itself.
Swapping means we are interchanging the numbers, it may be possible by using third
variable and without using third variable.
Syntax is as follows by using third variable:
Int a=10, b=20;
Int temp;
temp=a;

## // Declaration of third variable.

// temp=10.

a=b;

//a=20.

b=temp; //b=10.
After swapping a=20 & b= 10.
Swapping of two variables also possible by without using third variable, syntax is as
follows:
Int a=5, b=10;

b=a+b;

//b=5+10=15.

a=b-a;

// a=15-5=10.

b=b-a;

// b=15-10=5.

Semester
: II

## Class Name: First Year

Passing a variable by value makes a copy of the variable before passing it onto a
function. This means that if you try to modify the value inside a function, it will only
have the modified value inside that function. One the function returns, the variable you
passed it will have the same value it had before you passed it into the function. When we
call the function by passing the values or variables, the function is called as call by
values.
Algorithm

Step 1: Start
Step 2: Take the two numbers from keyboard.
Step 3: Use temp variable.
Step 4: temp=a;
a=b;
b=temp;
Execute this logic, two numbers will be swap.
Step 6: Stop

Conclusion:

## Subject Name: Computer Programming.

Semester
: II

1. What is mean by swapping?

Semester
: II

Experiment No.

Title

Program

.
Date

Semester
: II

Objective

## 1) Understand the concept of Files.

2) Understand the different concepts of File functions.
3) Understand the different operations of files.

Theory

We frequently use files for storing information which can be processed by our programs. In
order to store information permanently and retrieve it we need to use files. Files are not only
used for data. Our programs are also stored in files. The editor which you use to enter your
program and save it simply manipulates files for you. As you know, you can have many files on
your disk. If you wish to use a file in your programs, then you must specify which file or files
you wish to use.
Specific operation of files:
1. Creating a new file.
2. Opening an existing file.
4. Writing data to the file.
5. Moving to specific location in file.
6. Closing a file.

File Functions:
Fopen()

To open a file.

Fclose()

To close a file.

Fgetc()

Fputc()

Semester
: II

Fgets()

Fputs()

## Class Name: First Year

Specifying the file you wish to use is referred to as opening the file. When you open a file you
must also specify what you wish to do with it i.e. Read from the file, Write to the file, or both.
Because you may use a number of different files in your program, you must specify when
reading or writing which file you wishes to use. This is accomplished by using a variable called
a file pointer. Every file you open has its own file pointer variable. When you wish to write to a
file you specify the file by using its file pointer variable. You declare these file pointer variables
as follows:
FILE *fopen (), *fp1, *fp2, *fp3;
The variables fp1, fp2, fp3 are file pointers. You may use any name you wish. The file
<stdio.h> contains declarations for the Standard I/O library and should always be included at
the very beginning of C programs using files. Constants such as FILE, EOF and NULL are
defined in <stdio.h>.You should note that a file pointer is simply a variable like an integer or
character. It does not point to a file or the data in a file. It is simply used to indicate which file
your I/O operation refers to. A file number is used in the Basic language and a unit number is
used in FORTRAN for the same purpose. The function fopen is one of the Standard Library
functions and returns a file pointer which you use to refer to the file you have opened e.g.
fp = fopen ( prog.c, r) ;
The above statement opens a file called prog.c for reading and associates the file pointer fp with
the file. When we wish to access this file for I/O, we use the file pointer variable fp to refer to it.
You can have up to about 20 files open in your program - you need one file pointer for each file
you intend to use.

Semester
: II

## Class Name: First Year

Algorithm:
Step 1: Start
Step 2: Create the text files in TC/ BIN folder.
Step 3: Save these text files with some name.
Step 4: In one of the text file write the data, that file is nothing but source file. Another text file
keeps as a blank.
Step 5: Open these two text files in turbo c Window by using fopen ( ) function.
Step 6: Perform read operation on source file and read the information from this file by using
fgetc ( ) function.
Step 7: Perform the write operation on the target file, by using fputc( ) function.
Step 8: Stop

## Subject Name: Computer Programming.

Semester
: II

Conclusion:

1. What is mean by File & what is mean by FILE ?

Semester
: II

Experiment No.

Title

:6

## : To check whether the string is palindrome or

Not. implement string function

Date

Semester
: II

## Class Name: First Year

Problem Statement : Write a program to perform whether our string is palindrome or not,
Implement string function

Objective:

## 1.To understands meaning of string.

2. To understand the operations on the string.
3. To understand concept of palindrome.

Theory

String is the collection of characters. Example Welcome. The general form of the string is char
array-name [size];
Char name [13]; here name is a character array having size 13. The empty memory location is
placed with /0 (NULL) it indicates end of string.
0

2
l

## Initialization of the string: Char name [13] = Welcome

If the reverse of a string is equal to original string then it is called palindrome. We are
going to perform different operations on string such as calculate the length of the string,
reverse the string , copy one string to the another, and check whether given string is
palindromes or not.
There are some string functions are available in C:
1. strlen ()

- string length

## 2. strcmp () - string compare

3. strcpy () - string copy
4. strcat ()

- string concatenate

5. strlwr()

- string lower

6. strupr()

-string upper

## Subject Name: Computer Programming.

Semester
: II

Algorithm:
Step 1: Start
Step 3: Make reverse of the given string.
Step4: Store this reverse string into the another temporary string
Step 4: Compare these two strings
Step 5: If both are equal then print palindrome
Step 6: Otherwise print not palindrome
Step 7: Stop

Conclusion:

## Subject Name: Computer Programming.

Semester
: II

1) What is string?

Semester
: II

Experiment No.

:5

Title

Date

Semester
: II

## Problem Statement : To study of pointer concepts

Objective

:
1) To understand the basic concept Pointer
2) To understand the basic concept of array of pointer
3) To understand the concept of declaration, accessing of pointer.

Theory

The Pointer is one of the powerful tools of C programming. Pointers are the variables which stores
1.
2.
3.
4.
5.
6.

## Pointer saves memory space and process the data fast.

Pointer allows C to support dynamic memory management.
Pointer provide efficient tool for manipulating structures, linked list, stacks, queues, and trees.
Pointer reduces the length of program.
Pointer reduces the complexity of program.
Pointer increases the execution speed and thus reduces the execution time of program.

We know that memory is consist of millions of cells. Each cell has its own address (Address is
nothing but numbers). These memory locations we can assign by using variable or memory address.
Let int i=3;
Here, i is name of memory location and memory space reserved to store integer value. 3 is stored at
memory location.
i

location name

3
3 Value at location

Semester
: II

## Class Name: First Year

j
3

65524

65524

65522

The variable j having its own address i.e. 65522. The variable j stores the address of i, so j is called as
pointer.
Declaration of Pointer:
The pointer is declared as follows:
Data type *pointer_name;

## Where * is pointer declaration.

Examples

int *i;
char *ch;
float *f;

Accessing Pointer
After declaring pointer it is necessary to store address of required variable into it. That is we create a
link between pointer and variable. The & is used to assign address of variable.
The general form to assign address to pointer is:
pointer_name = & (variable name)
Example: The statement ptr = &i shows that ptr points to i variable.
void main ( )
{ int i = 3;
int *ptr;
ptr = &i; }

Semester
: II

## ARRAY AND POINTER

Array is a collection of elements of same data type. Consider the following example:
int a[5]={5,10,15,20,25};
The memory location map of above array is shown below:

a[0]

a[1]

a[2]

a[3]

a[4]

location of array
Array Element

1000

1002

1004

1006

1008

Array elements are stored in contiguous memory location. If we store base element address to 0th
element then rest of elements are accessed by pointer. Now, we assign the base address to pointer as
follows:
int *ptr;
ptr=&a[0];
Here, base address of 0th element will be stored. By using statement ptr++ we will easily go to the
next location.
ARRAY OF POINTER
Array of pointer is nothing but it is a collection of pointers of same data type. The array of pointer is
declared as:
Data type *array_name [size];

Example:

int *a[20];

Semester
: II

## Class Name: First Year

char *state[3]={
Goa,
Maharashtra,
Karnataka,
}
POINTER TO STRUCTURE
Structure is collection of element. These elements may be same type or may be different data type.
We can declare pointer to structure. We use () operator to point to the member of structure. The
general form of pointer to structure is:
struct <structure name>
{
Data type

variable name;

------------------------------};
struct *ptr;

Conclusion:

Semester
: II

Semester
: II

Experiment No.

:4

Title

matrices

Date

Semester
: II

## Class Name: First Year

Problem Statement :
Write a program to perform operations on matrices (Addition Subtraction,
Multiplication of two matrices.).
Objective
:
1. To understand concept of array.
2. Passing of array to function.
3. To understand different Operation on Matrices.
4. To understand concept of 2 Dimensional array.
5. Passing of 2 Dimensional arrays to function.
6. To understand different Operation on Matrices.

Theory

Array is a collection of elements of similar data type. Discussing array without using pointer
makes the discussion incomplete and wanting. Array makes the use of pointers internally. In
array for loop is used to access input values from first location till the end of array.
TYPES OF ARRAY
1. ONE DIMENSIONAL ARRAY.
2. TWO DIMENSIONAL ARRAY.
3. MULTIDIMENSIONAL ARRAY.
1.ONE DIMENSIONAL ARRAY
SYNTAX:
Data type array_ name [size];
Examples:
1. int a[5];
2. float b[10];
3. ch ar name [20];

Semester
: II

## INITIALIZATION OF ONE DIMENSIONAL ARRAY

Data type array_name [array size]={List of variables}
Int a[5]={10,15,20,30,40};

a[0]

a[1]

a[2]

10

15

20

a[3]
30

a[4]
40

## 2. TWO DIMENSIONAL ARRAY

Syntax:
Data type array name [row] [column];
EX.
int stud[3][3];
We will discuss the elementary operations of addition, subtraction, multiplication, and inversion,
a matrix algebra analogue of division of scalar numbers. Subsequently, we will turn our attention
to powers of matrices and to operations involving scalar numbers and matrices, scalar numbers
and vectors, and vectors and matrices. The operations on matrices differ from similar operations
of scalar algebra in several respects. The matrix algebra operations, in general, are not
commutative and attention must be paid to whether the matrices are conformable with respect to
the intended operation. Also, it must be noted whether the matrix operation pertains to matrix
elements or to matrices.

## Addition and Subtraction of Matrix Elements

To add elements of matrices A and B and store the result as a matrix C,

Semester
: II

## Class Name: First Year

elements of matrix A are added to their corresponding elements in matrix B and stored as
elements of matrix C. Obviously, all three matrices must be of the same order or size. Notice that
elements, as contrasted with addition of matrices, to be discussed in section to follow. An
example of addition of matrix elements is schematized below

and illustrated as

## Subtraction of Matrix Elements

The operation of subtracting matrix elements can be schematized as

and illustrated as

In sum, the above matrix elements can be added and subtracted if and only if the matrices are of
the same order (identical in the number of rows and columns). Matrices upon which an operation
is permissible are said to conform to the operation.
MULTIPLICATION OF MATRIX
If A = [a ij] is a matrix of order m x n and B = [b ij] a matrix of order n x p, then the product C =
AB of the two matrices is a matrix of order m x p matrix defined.

Semester
: II

## c ij = a i1b 1j + a i2b 2j + a i3b 3j + . . . + a inb nj

Conclusion

1. What is mean by two dimensional array give any example of two dimensional array?

2. What is the difference between one dimensional array and two dimensional array?

3. How to assign pointer to the one dimensional and two dimensional array?

Semester
: II