CP Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 87

C Programming Lab 2024

Lab Manual

Of

C Programming Lab
F.E. (ALL)

SEM II

F. H. 2024

TEC[General Engineering] Page 1


C Programming Lab 2024

LIST OF EXPERIMENTS

Lab Outcomes:
After successful completion of the course student will be able to:
1. Demonstrate fundamental concepts of C Programming
2. Apply the concept of control structures for solving real life problems
3. Use modular programming approach for diversified problems
4. Represent data in arrays, strings and structures and manipulate them
through a program.
5. Demonstrate the concept of call by reference, dynamic memory allocation
using pointers

Experiment List:

Sr. LO
Experiment
no. Mapping

1. Introduction to c programming environment. LO1

Write a program to demonstrate all data types with:


● Variable declaration,
2. LO1
● Initialization
● Input/output statements.

3. Given a year, determine whether it is a leap-year or not. LO2

4. Determine weather conditions based on temperature LO2

Menu-Driven code to compute the area of the chosen geometrical


5. LO2
shape.

6. Display the squares of numbers in specified range. LO2

7. Find the sum of the series 1-X^2/2!+X^4/4!... LO2

Write a C Program to print Star pattern - Pyramid star pattern


8. a) * b) * * * * * LO2
** ****
*** ***

TEC[General Engineering] Page 2


C Programming Lab 2024

**** **
***** *
Write a C program to find multiplication of two matrix and
9. LO4
transpose of the resultant matrix.

10. Write a C program for sorting numbers using Selection sort. LO4

11. Write a C Program to check whether string is palindrome or not. LO4

12. Compare two strings without using library function. LO4

13. Find GCD of two numbers. LO3

14. Search an element in the array using function. LO3

Write a C program to print Fibonacci series of 20 numbers using


15. LO3
recursion.

16. Find product of digits of a number using recursion. LO3

17. Store product details in structure and calculate total amount. LO4

18. Demonstrate the example of nested structure. LO4

19. Swap two numbers by call by reference. LO5

20. Print a string using pointer. LO5

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.01
A.1 Aim:

TEC[General Engineering] Page 3


C Programming Lab 2024

Introduction to c programming environment.

A.2 Outcome:
After successful completion of this experiment students will be,
Demonstrate fundamental concepts of C Programming.

A.3 Theory:
C Language Introduction:
C is a procedural programming language. It was initially developed by Dennis
Ritchie in the year 1972. It was mainly developed as a system programming
language to write an operating system. The main features of C language include
low-level access to memory, a simple set of keywords, and clean style, these
features make C language suitable for system programming like an operating
system or compiler development.
Many later languages have borrowed syntax/features directly or indirectly from C
language. Like syntax of Java, PHP, JavaScript, and many other languages are
mainly based on C language. C++ is nearly a superset of C language (There are
few programs that may compile in C, but not in C++).

Beginning with C programming:


1. Structure of a C program
After the above discussion, we can formally assess the structure of a C
program. By structure, it is meant that any program can be written in this
structure only. Writing a C program in any other structure will hence lead to
a Compilation Error.

The structure of a C program is as follows:

TEC[General Engineering] Page 4


C Programming Lab 2024

The components of the above structure are:


1. Header Files Inclusion: The first and foremost component is the
inclusion of the Header files in a C program.
A header file is a file with extension .h which contains C function
declarations and macro definitions to be shared between several source
files.
Some of C Header files:
● stddef.h – Defines several useful types and macros.
● stdint.h – Defines exact width integer types.
● stdio.h – Defines core input and output functions
● stdlib.h – Defines numeric conversion functions, pseudo-random
network generator, memory allocation
● string.h – Defines string handling functions
● math.h – Defines common mathematical functions

Syntax to include a header file in C:


#include

2. Main Method Declaration: The next part of a C program is to declare


the main() function. The syntax to declare the main function is:

TEC[General Engineering] Page 5


C Programming Lab 2024

Syntax to Declare main method:


int main()
{}

3. Variable Declaration: The next part of any C program is the variable


declaration. It refers to the variables that are to be used in the function.
Please note that in the C program, no variable can be used without
being declared. Also in a C program, the variables are to be declared
before any operation in the function.

Example:
int main()
{
int a;
.
.

4. Body: Body of a function in C program, refers to the operations that are


performed in the functions. It can be anything like manipulations,
searching, sorting, printing, etc.

Example:
int main()
{
int a;

printf("%d", a);
.
.

5. Return Statement: The last part in any C program is the return


statement. The return statement refers to the returning of the values
from a function. This return statement and return value depend upon
the return type of the function. For example, if the return type is void,
then there will be no return statement. In any other case, there will be a
return statement and the return value will be of the type of the

TEC[General Engineering] Page 6


C Programming Lab 2024

specified return type.


Example:

int main()
{
int a;

printf("%d", a);

return 0;
}
PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: Date of Submission:

Grade:

B.1 Code Screenshot:


#include<stdio.h> \\Introduction\\
#include<conio.h>

void main()
{
clrscr();
printf("Hello World \n Atharv Wagh");
getch();
}

B.2 Output:

TEC[General Engineering] Page 7


C Programming Lab 2024

B.3 Conclusion:
With this experiment our aim was to get the complete introduction to
C programming environment and after the completion of this
experiment we were demonstrated fundamental concepts of C
programming. The Programming Subject was completely new to us
therefore firstly we got detailed history of programming language
works. In this experiment we went through the basics of C
programming language, After this experiment we are aware to the
body structure of C programming like how to start it and what to write
in between and how to end it.

TEC[General Engineering] Page 8


C Programming Lab 2024

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.02
A.1 Aim:
Write a program to demonstrate all data types with:
● Variable declaration,
● Initialization
● Input/output statements.

A.2 Outcome:
After successful completion of this experiment students will be,
Demonstrate fundamental concepts of C Programming.

A.3 Theory:
Variable:
A variable is a name given to the memory location where data is stored
depending upon the type of the variable.

Variable Declaration:
Syntax:
data_type variable_name;
Example:
int choice;
char ans;

Variable Initialization:
Variable initialization means assigning some value to that variable
Example:
int ch;
ch = 10;

TEC[General Engineering] Page 9


C Programming Lab 2024

OR
Variable Declaration and initialization on the same line
int ch = 10;

Data Type:
Data types refer to the characteristics of data stored into a variable.
Data type helps you find Size, range and type of value
C Data Types are used to:
Identify the type of a variable when it declared
Identify the type of the return value of a function
Identify the type of a parameter expected by a function

TEC[General Engineering] Page 10


C Programming Lab 2024

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No: 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: Date of Submission:

Grade:

B.1 Code Screenshot:


#include<stdio.h>
#include<conio.h>
void main()
{
int a;
char b;
float c;
double d;
clrscr();
printf("Enter a Character Value:");
scanf("%ch",&b);
printf("Enter an Integer Value:");
scanf("%d",&a);
printf("Enter a float Value:");
scanf("%f",&c);
printf("Enter a double value:");

TEC[General Engineering] Page 11


C Programming Lab 2024

scanf("%lf",&d);
getch();
}

B.2 Input and Output:

B.4 Conclusion:
In this experiment we got to know that how we can define the
variables and assigned value to it. In this experiment we used various
keywords like integer, character, float, double & many more, assigned
values to them & print. After assigning values to them we have
printed them using then printf function and we got our desired output.

TEC[General Engineering] Page 12


C Programming Lab 2024

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.03
A.1 Aim:
Given a year, determine whether it is a leap-year or not.

A.2 Outcome:
After successful completion of this experiment students will be,
Apply the concept of control structures for solving real life problems

A.3 Theory:
Decision Making:
Decision making is the most important aspect of almost all the programming
languages. As the name implies, decision making allows us to run a particular
block of code for a particular decision. Here, the decisions are made on the
validity of the particular conditions. Condition checking is the backbone of
decision making.

if else statement:
The if-else statement in C is used to perform the operations based on some
specific condition. The operations specified in if block are executed if and only if
the given condition is true.

Nested if else statement:


Nested If in C Programming is placing If Statement inside another IF Statement.
Nested If in C is helpful if you want to check the condition inside a condtion. If
Else Statement prints different statements based on the expression result (TRUE,
FALSE). Sometimes we have to check even further when the condition is TRUE.
Syntax:
if ( test condition 1)

TEC[General Engineering] Page 13


C Programming Lab 2024

//If the test condition 1 is TRUE then these it will check for test condition
2
if ( test condition 2)
{
//If the test condition 2 is TRUE, these statements execute
Test condition 2 True statements;
}

else
{
//If the c test condition 2 is FALSE, then these statements execute
Test condition 2 False statements;
}

else
{
//If the test condition 1 is FALSE then these statements will be executed
Test condition 1 False statements;
}

TEC[General Engineering] Page 14


C Programming Lab 2024

4. Algorithm:
Step 1: Start
Step 2: Read year
Step 3: if year>0 && year<=9999
if year%4 == 0 && (year%400==0 || year%100!=0)
Display year is a leap year
Else
Display year is not a leap year
Else
Display not a valid year
Step 4: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: Date of Submission:

Grade:

B.1 Code Screenshot:


#include<stdio.h> //Program of leap year//
#include<conio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year %4==0)
{
if(year %100==0)

TEC[General Engineering] Page 15


C Programming Lab 2024

{
if(year %400==0)
printf("Leap Year \n");
else
printf("Not a leap year\n");
}
else
printf("It's a leap year\n");
}
else
printf("It's not a leap year\n");
getch();
return 0;
}
B.2 Input and Output:

B.3 Conclusion:
In this experiment we had to determine that a given year is leap year
or not a leap year . Before executing it in C programming we studied
about how to execute a condition based statements in C
programming with the help of ‘If else’ or ‘nested if else’ statements.
After that we executed the condition based in C program and
checked weather the output showing to us is right by entering a leap
year, not a leap and not a valid year in output .

TEC[General Engineering] Page 16


C Programming Lab 2024

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.04
A.1 Aim:
Determine weather conditions based on temperature
Temperature < 0 then print "Freezing weather".
Temperature 0 - 10 then print "Very cold weather".
Temperature 10 - 20 then print "Cold weather".
Temperature 20 - 30 then print "Normal in temperature".
Temperature 30 - 40 then print "Its Hot".
Temperature >= 40 then print "Its Very Hot".

A.2 Outcome:
After successful completion of this experiment students will be,
Apply the concept of control structures for solving real life problems

A.3 Theory:
if else if ladder:
The if-else-if ladder statement is an extension to the if-else statement. It is used
in the scenario where there are multiple cases to be performed for different
conditions. In if-else-if ladder statement, if a condition is true then the
statements defined in the if block will be executed, otherwise if some other
condition is true then the statements defined in the else-if block will be executed,
at the last if none of the condition is true then the statements defined in the else
block will be executed. There are multiple else-if blocks possible. It is similar to
the switch case statement where the default is executed instead of else block if
none of the cases is matched
Syntax:

TEC[General Engineering] Page 17


C Programming Lab 2024

if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
else
{
//code to be executed if all the conditions are false
}

TEC[General Engineering] Page 18


C Programming Lab 2024

4. Algorithm:
Step 1: Start
Step 2: Read temp
Step 3: if temp<0
Display “Freezing weather”
else if temp>=0 && temp<10
Display “Very cold weather”
else if temp>=10 && temp<20

TEC[General Engineering] Page 19


C Programming Lab 2024

Display “Cold weather”


else if temp>=20 && temp<30
Display “Normal in temperature”
else if temp>=30 && temp<40
Display “It’s Hot”
else
Display “It’s Very hot”
Step 4: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: Date of Submission:

Grade:

B.1 Code Screenshot:


//Weather condition based on if else ladder//
#include<stdio.h>
#include<conio.h>
int main()
{
float temperature;
printf("Enter the temperature: ");
scanf("%f",&temperature);
if (temperature >=40)
{
printf("It's very hot \n");
}
else
if (temperature >=30)
{

TEC[General Engineering] Page 20


C Programming Lab 2024

printf("It's hot \n");


}
else
if (temperature >=20)
{
printf("It's warm \n");
}
else
if (temperature >=10)
{
printf("It's cool \n");
}
else
{
printf("It's cold \n");
}
getch();
return 0;
}

B.2 Input and Output:

B.3 Conclusion: This experiment tells us about the application of


control structure that is if else if ladder. Through the use of if else if
ladder we were able to find about the weather conditions when the
user input some temperature. Hence the program was executed
successfully.

TEC[General Engineering] Page 21


C Programming Lab 2024

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.05
A.1 Aim:
Menu-Driven code to compute the area of the chosen geometrical shape.
A.2 Outcome:
After successful completion of this experiment students will be,
Apply the concept of control structures for solving real life problems
A.3 Theory:
Switch Case:
The switch statement in C is an alternate to if-else-if ladder statement which
allows us to execute multiple operations for the different possible values of a
single variable called switch variable. Switch statement in C tests the value of a
variable and compares it with multiple cases. Once the case match is found, a
block of statements associated with that particular case is executed. If a case

TEC[General Engineering] Page 22


C Programming Lab 2024

match is NOT found, then the default statement is executed, and the control goes
out of the switch block.
1) The switch expression must be of an integer or character type.
2) The case value must be an integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If there is no
break statement found in the case, all the cases will be executed present after
the matched case. It is known as fall through the state of C switch statement.
Syntax:
switch(expression)
{
case constant1:
// statements
break;

case constant2:
// statements
break;
.
.
.
default:
// default statements
}

TEC[General Engineering] Page 23


C Programming Lab 2024

4. Algorithm:
Step 1: Start
Step 2: Display Menu with options
Step 3: Read choice
Step 4:
Case 1: Circle
a = 3.14*r*r
Display a (Area of circle)
Case 2: Triangle
a = ½*base*height
Display a (area of triangle)
Case 3: Square

TEC[General Engineering] Page 24


C Programming Lab 2024

a = side*side
Display a (area of square)
Case 4: Rectangle
a = length*breadth
Display a (area of rectangle)
Case 5: Parallelogram
a = base * height
Display a (area of parallelogram)
Default:
Display “Incorrect Choice”
Step 5: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: 23/01/2024 Date of Submission:

Grade:

B.1 Code:
//Program to determine area of geometric shape by switch//
#include<stdio.h>
#define PI 3.14
#include<conio.h>
void main()
{
int shape;

TEC[General Engineering] Page 25


C Programming Lab 2024

float side,base,length,breadth,height,area,radius;
clrscr();
printf("\n 1.Circle");
printf("\n 2.Rectangle");
printf("\n 3.Triangle");
printf("\n 4.Square");
scanf("%d",&shape);
switch(shape)
{
case 1:
printf("Enter radius:");
scanf("%f",&radius);
area=PI*radius*radius;
printf("Area of Circle=%f\n",area);
break;
case 2:
printf("Enter breadth and length:\n");
scanf("%f%f",&breadth,&length);
area=breadth*length;
printf("Area of rectangle=%f\n",area);
break;
case 3:
printf("Enter base and height:");
scanf("%f%f",&base,&height);
area=0.5*base*height;
printf("Area of Triangle=%f\n",area);
break;
case 4:
printf("Enter side:");

TEC[General Engineering] Page 26


C Programming Lab 2024

scanf("%f",&side);
area=side*side;
printf("Area of square=%f\n",area);
break;
default:
printf("Error in shape\n");
break;
}
getch();
}

B.2 Input and Output Screenshot:

B.3 Conclusion: This experiment tells about the application of control


structure that is for loop, through the use of for loop in this we can
display the square of numbers in specified ranges. So the program
was executed successfully

TEC[General Engineering] Page 27


C Programming Lab 2024

PART A
(PART A : TO BE REFFERED BY STUDENTS)

TEC[General Engineering] Page 28


C Programming Lab 2024

Experiment No.06
A.1 Aim:
Display the squares of numbers in specified range.
A.2 Outcome:
After successful completion of this experiment students will be,
Apply the concept of control structures for solving real life problems.
A.3 Theory:
For Loop:
The for loop in C language is used to iterate the statements or a part of the
program several times. It is frequently used to traverse the data structures like
the array and linked list.
For loop specifies the three things:
❖ Setting a loop counter to an initial value

❖ Testing a loop counter to determine whether its value has reached the
number of repetitions required
❖ Increasing the value of loop counter each time the program within the loop
has been executed
Syntax:
for(initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}

TEC[General Engineering] Page 29


C Programming Lab 2024

4. Algorithm:
Step 1: Start
Step 2: Read start, end (Specify range)
Step 3: for i = start to end
Display i*i
Step 4: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: 23/01/2024 Date of Submission:

Grade:

B.1 Code:
//Display sq of a number inspecified range//

TEC[General Engineering] Page 30


C Programming Lab 2024

#include<stdio.h>
#include<conio.h>
int main()
{
int start,end,i;
clrscr();
printf("Enter the start of range:");
scanf("%d",&start);
printf("Enter the end of range:");
scanf("%d",&end);
for(i=start;i<=end;i++)
{
printf("%d\t",i*i);
}
getch();
return 0;
}
B.2 Input and Output Screenshot:

B.3 Conclusion: This experiment tells about the application of control

TEC[General Engineering] Page 31


C Programming Lab 2024

structure that is for loop, through the use of for loop in this we can
display the square of numbers in specified ranges. So the program
was executed successfully.
PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.07
A.1 Aim:
Find the sum of the series 1-X^2/2!+X^4/4!...
A.2 Outcome:
After successful completion of this experiment students will be,
Apply the concept of control structures for solving real life problems.
A.3 Theory:
While Loop:
While loop is also known as a pre-tested loop. In general, a while loop allows a
part of the code to be executed multiple times depending upon a given boolean
condition. It can be viewed as a repeating if statement. The while loop is mostly
used in the case where the number of iterations is not known in advance.

Syntax:
initialization expr;
while(test expr)
{
// body of the loop
// statements we want to execute
update Expr;
}

TEC[General Engineering] Page 32


C Programming Lab 2024

4. Algorithm:
Step 1: Start
Step 2: Read n, x
Step 3: Initialize sum = 1
Step 3: for i = 1 to n
fact = 1
for j = 2*i to 1
fact = fact *j
if i%2 == 0
sum = sum + pow(x, 2*i)/fact
else
sum = sum – pow(x, 2*i)/fact
Step 4: Display sum
Step 5: Stop

TEC[General Engineering] Page 33


C Programming Lab 2024

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv h

Class: K Batch: HK3

Date of Experiment: 30/01/2024 Date of Submission:

Grade:

B.1 Code:
//Sum of series//
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1,count;
float sum=0.0,fact;
while (num<=7)
{
fact=1;
for(count=1;count<=num;count++)
{
fact=fact*count;
}
sum=sum+(num/fact);
num++;
}
printf("Sum of series=%f\n",sum);
getch();

TEC[General Engineering] Page 34


C Programming Lab 2024

return 0;
}
B.2 Input and Output Screenshot:

B.3 Conclusion:
In this we have successfully executed the program of find the sum of
series.

TEC[General Engineering] Page 35


C Programming Lab 2024

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.08
A.1 Aim:
Write a C Program to print Star pattern - Triangle star pattern
a) * b) * * * * *
** ****
*** ***
**** **
***** *
A.2 Outcome:
After successful completion of this experiment students will be,
Apply the concept of control structures for solving real life problems.

A.3 Theory:
Nested Loop:
C supports nesting of loops in C. Nesting of loops is the feature in C that allows
the looping of statements inside another loop. Let's observe an example of
nesting loops in C.
Any number of loops can be defined inside another loop, i.e., there is no
restriction for defining any number of loops. The nesting level can be defined at n
times. You can define any type of loop inside another loop; for example, you can
define 'while' loop inside a 'for' loop.
Syntax:
Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
}

Nested For Loop:

TEC[General Engineering] Page 36


C Programming Lab 2024

The nested for loop means any type of loop which is defined inside the 'for' loop.
Syntax:
for (initialization; condition; update)
{
for(initialization; condition; update)
{
// inner loop statements.
}
// outer loop statements.
}

4. Algorithm:
a)
Step 1: Start
Step 2: for i = 1 to 5 do
for j = 1 to i do
Display *
end for loop
end for loop
Step 4: Stop

TEC[General Engineering] Page 37


C Programming Lab 2024

b)
Step 1: Start
Step 2: for i = 1 to 5 do
for j = i to 5 do
Display *
end for loop
end for loop
Step 4: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: 30/01/2024 Date of Submission:

Grade:

B.1 Code:
B1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++) //Rows//
{
for(j=1;j<=i;j++) //Columns//

TEC[General Engineering] Page 38


C Programming Lab 2024

{
printf("* ");
}
printf("\n");
}
getch();
}
B.2 Input and Output Screenshot:
B1

#include<stdio.h>

#include<conio.h>

void main()

int i,j;

clrscr();

for(i=5;i>=1;i--) //Rows//

TEC[General Engineering] Page 39


C Programming Lab 2024

for(j=1;j<=i;j++) //Columns//

printf("* ");

printf("\n");

getch();

Output B2:

B.3 Conclusion:
In this way we have successfully executed the program to print star
pattern-Triangle star pattern.

TEC[General Engineering] Page 40


C Programming Lab 2024

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No. 9
A.1 Aim:
Write a C program to find multiplication of two matrix and transpose of the
resultant matrix.

A.2 Outcome:
After successful completion of this experiment students will be,
Represent data in arrays, strings and structures and manipulate them through a
program.

A.3 Theory:
Two Dimensional Array:
The two-dimensional array can be defined as an array of arrays. The 2D array is
organized as matrices which can be represented as the collection of rows and
columns. However, 2D arrays are created to implement a relational database
lookalike data structure. It provides ease of holding the bulk of data at once
which can be passed to any number of functions wherever required.

Declaration of two dimensional Array:


Syntax:
data_type array_name[rows][columns];

Example:
int twodimen[4][3];
Here, 4 is the number of rows, and 3 is the number of columns.

Initialization of 2D Array in C

TEC[General Engineering] Page 41


C Programming Lab 2024

In the 1D array, we don't need to specify the size of the array if the declaration
and initialization are being done simultaneously. However, this will not work with
2D arrays. We will have to define at least the second dimension of the array. The
two-dimensional array can be declared and defined in the following way.

int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};

Matrix Multiplication:
The multiplication of matrices is possible only when the matrices are
compatible. Two matrices A and B are said to be compatible if the number of
columns in A is equal to the number of rows in B. That means if A is a matrix
of order m×n and B is a matrix of order n×p, then we can say that matrices A and
B are compatible. That means, the resultant matrix for the multiplication of for
any m × n matrix 'A' with an n × p matrix 'B', the result can be given as matrix 'C' of
the order m × p.

Multiplying matrices can be performed using the following steps:

Step 1: Make sure that the number of columns in the 1st matrix equals the
number of rows in the 2nd matrix (compatibility of matrices).

TEC[General Engineering] Page 42


C Programming Lab 2024
th
Step 2: Multiply the elements of i row of the first matrix by the elements of
th
j column in the second matrix and add the products. This would the element
th th
that is in the i row and j column of the resultant matrix.
Step 3: Place the added products in the respective positions.

Transpose of a Matrix:
The transpose of a matrix is obtained by changing its rows into columns (or
equivalently, its columns into rows). Here for matrix A the elements of the first
row have been written in the first column of the new matrix, and the elements of
the second row have been written in the second column of the new matrix. And
this new matrix is denoted as AT, which is the transpose of the given matrix A.

4. Algorithm:
Matrix Multiplication:
Step 1: Start
Step 2: Read matrix a[m][n] and b[p][q]
Step 3: if n == p
Define matrix c[m][q]
for i = 0 to m-1
for j = 0 to q-1
c[i][j] = 0
for k = 0 to n-1
c[i][j] += a[i][k]*b[k][j]

TEC[General Engineering] Page 43


C Programming Lab 2024

Display matrix c[m][q]


else
Display matrix multiplication not possible
Step 4: Stop
Transpose of matrix:
Step 1: Start
Step 2: Define t[q][m]
Step 3: for i = 0 to m-1
for j = 0 to q-1
t[j][i] = c[i][j]
Step 4: Display t[q][m]
Step 5: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: 06/02/2024 Date of Submission:

Grade:

B.1 Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int m,n,p,q,c,d,k,i,j,sum=0;
int first[10][10],second[10][10],multiply[10][10],tra[10][10];
clrscr();
printf("Enter number of rows and columns of first matrix: ");
scanf("%d%d",&m,&n);
printf("\n Enter the elements of first matrix:");

TEC[General Engineering] Page 44


C Programming Lab 2024

for (c=0;c<m;c++)
for(d=0;d<n;d++)
scanf("%d",&first[c][d]);
printf("\n Enter the number of rows and columns of second matrix:");
scanf("%d%d",&p,&q);
if(n!=p)
printf("\n Matrice with the entered order can not be multiplied with
each other");
else
{
printf("\n Enter the elements of second matrix:");
for(c=0;c<m;c++)
for(d=0;d<q;d++)
scanf("%d",&second[c][d]);
for(c=0;c<m;c++)
{
for(d=0;d<q;d++)
{
for(k=0;k<p;k++)
{
sum=sum+first[c][k]*second[k][d];
}
multiply[c][d]=sum;
sum=0;
}
}
printf("Product of entered matrices:- \n");
for(c=0;c<m;c++)
{
for(d=0;d<q;d++)
printf("\t\t%d",multiply[c][d]);
printf("\n");
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
tra[j][i]=multiply[i][j];

TEC[General Engineering] Page 45


C Programming Lab 2024

}
}
printf("\n After transpose the elements are: \n");
for(i=0;i<q;i++)
{
for(j=0;j<m;j++)
{
printf("\t\t%d",tra[i][j]);
}
printf("\n");
}
getch();
return 0;
}

B.2 Input and Output Screenshot:

B.3 Conclusion:
In this way have successfully executed this program to find
multiplication of two matrix and transpose of the resultant matrix.

TEC[General Engineering] Page 46


C Programming Lab 2024

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.10
A.1 Aim:
Write a C program for sorting numbers using Selection sort.

A.2 Outcome:
After successful completion of this experiment students will be,
Represent data in arrays, strings and structures and manipulate them through a
program.

A.3 Theory:
Selection Sort:
• Selection sort is one of the easiest approaches to sorting.
• It is inspired from the way in which we sort things out in day to day life.
• It is an in-place sorting algorithm because it uses no auxiliary data structures
while sorting.

How Selection Sort Works?


Consider the following elements are to be sorted in ascending order using
selection sort-
6, 2, 11, 7, 5
Selection sort works as-
• It finds the first smallest element (2).
• It swaps it with the first element of the unordered list.
• It finds the second smallest element (5).

TEC[General Engineering] Page 47


C Programming Lab 2024

• It swaps it with the second element of the unordered list.


• Similarly, it continues to sort the given elements.
• As a result, sorted elements in ascending order are-
2, 5, 6, 7, 11

Selection Sort Example:


Consider the following elements are to be sorted in ascending order-

6, 2, 11, 7, 5

The above selection sort algorithm works as illustrated below-

Step-01: For i = 0

Step-02: For i = 1

Step-03: For i = 2

TEC[General Engineering] Page 48


C Programming Lab 2024

Step-04: For i = 3

Step-05: For i = 4

Loop gets terminated as ‘i’ becomes 4.

The state of array after the loops are finished is as shown-

With each loop cycle,

The minimum element in unsorted sub-array is selected.


It is then placed at the correct location in the sorted sub-array until array A is
completely sorted.

TEC[General Engineering] Page 49


C Programming Lab 2024

4. Algorithm:
Step 1: Start
Step 2: Declare array a of size n
Step 3: Set i = 0 and for each unsorted element in a repeat the steps 3 to 7
Step 4: Set min = i
Step 5: Set j = i+1 and repeat step 5
Step 6: if a[j] < a[min]
Set min = j
increment j
[End loop]
Step 7: Set temp = a[min]
Set a[min] = a[i]
Set a[i] = temp
Step 8: Increment i
Step 9: call Display()
Step 10: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: 06/02/2024 Date of Submission:

Grade:

B.1 Code:
//Sorting numbers using selection sort//
#include<stdio.h>
#include<conio.h>

TEC[General Engineering] Page 50


C Programming Lab 2024

int main()
{
int i,j,count,temp,number[25];
clrscr();
printf("Number of elements to be entered:");
scanf("%d",&count);
printf("\n Enter %d elements:",count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
for(i=0;i<count;i++)
{
for(j=i+1;j<count;j++)
{
if(number[i]>number[j])
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
printf("\n Sorted elements: ");
for(i=0;i<count;i++)
printf("\t %d",number[i]);
getch();
return 0;
}

B.2 Input and Output Screenshot:

TEC[General Engineering] Page 51


C Programming Lab 2024

B.3 Conclusion:
In this way we have successfully executed the program for sorting
number using selection sort.

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.11
A.1 Aim:
Write a C Program to check whether string is palindrome or not.

A.2 Outcome:
After successful completion of this experiment students will be,
Represent data in arrays, strings and structures and manipulate them through a
program.

A.3 Theory:
String:

TEC[General Engineering] Page 52


C Programming Lab 2024

The string can be defined as the one-dimensional array of characters terminated


by a null ('\0'). The character array or the string is used to manipulate text such
as word or sentences. Each character in the array occupies one byte of memory,
and the last character must always be 0. The termination character ('\0') is
important in a string since it is the only way to identify where the string ends.
When we define a string as char s[10], the character s[10] is implicitly initialized
with the null in the memory.
There are two ways to declare a string in c language.
1. By char array
char ch[10]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
2. By string literal
char ch[]="javatpoint";
In this case, '\0' will be appended at the end of the string by the compiler.

Palindrome:
The characters in the string should remain the same after reversing the sequence
of characters, the word should be read the same in both forward and backward
then the string is known as "Palindrome".

Example:
“radar” word is a palindrome
4. Algorithm:
Step 1: Start
Step 2: Read str
Step 3: Initialize flag = 0
Step 4: call len = strlen(str) to find length of str
Step 5: for i = 0 to len
if str[i] != str[len-i-1]
flag = 1
break
Step 6: if flag == 1
Display strings are not palindrome
Else
Display strings are palindrome
Step 7: Stop

TEC[General Engineering] Page 53


C Programming Lab 2024

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment:13/02/2024 Date of Submission:

Grade:

B.1 Code:
//String is palindrome or not//
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char s1[1000],s2[1000];
clrscr();
printf("\n Enter the string:");
gets(s1);
strcpy(s2,s1);
strrev(s2);
if(!strcmp(s1,s2))
printf("\n String is palindrome");
else
printf("\n String is not palindrome");
getch();
return 0;
}
B.2 Input and Output Screenshot:

TEC[General Engineering] Page 54


C Programming Lab 2024

B.3 Conclusion: In this way we have successfully run the program to


check whether string is palindrome or not

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.12
A.1 Aim:
Compare two strings without using library function.

A.2 Outcome:

TEC[General Engineering] Page 55


C Programming Lab 2024

After successful completion of this experiment students will be,


Represent data in arrays, strings and structures and manipulate them through a
program.

A.3 Theory:
String Comparison:
Strings can be compared either by using the string function or without using
string function.

The string function which is pre-defined in a string.h header file is


a strcmp() function. The strcmp() function consider two strings as a parameter,
and this function returns an integer value where the integer value can
be zero, positive or negative. If ASCII value difference is zero strings are equal if
it is –ve or +ve value other than 0 then strings are not equal.

The syntax of the strcmp() function is given below:


int strcmp (const char* str1, const char* str2);

In the above syntax, two parameters are passed as strings, i.e., str1 and str2, and
the return type is int means that the strcmp() returns an integer value.

The strcmp() function compares the character of both the strings. If the first
character of both the strings are same, then this process of comparison will
continue until all the characters are compared or the pointer points to the null
character '\0'.

Possible return values from the strcmp() function

Return
Description
value
0 When both the strings are equal.
<0 If the ASCII value of a character of the first string is less than the ASCII
value of a character of the second string, then the function will return
negative value.
>0 If the ASCII value of a character of the first string is greater than the

TEC[General Engineering] Page 56


C Programming Lab 2024

ASCII value of a character of the second string, then the function will
return positive value.

4. Algorithm:
Step 1: Start
Step 2: Read str1 and str2
Step 3: Initialize i = 0
Step 4: while s[i] != ‘\0’ || d[i] != ‘\0’
if s[i] == d[i]
cmp = 0
i++
else
cmp = 1
break
Step 5: if cmp == 0
Display strings are equal
Else
Display strings are not equal
Step 6: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

TEC[General Engineering] Page 57


C Programming Lab 2024

Class: K Batch: HK3

Date of Experiment: 13/02/2024 Date of Submission:

Grade:

B.1 Code:
//Compare two string without using library function//
#include<stdio.h>
#include<conio.h>
int main()
{
char str1[30],str2[30];
int i;
clrscr();
printf("Enter two strings:");
gets(str1);
gets(str2);
i=0;
while(str1[i]==str2[i]&&str1[i]!='\0')
i++;
if(str1[i]>str2[i])
printf("str1>str2");
else
if(str1[i]<str2[i])
printf("str1<str2");
else
printf("str1=str2");
getch();
return 0;
}

B.2 Input and Output Screenshot:

TEC[General Engineering] Page 58


C Programming Lab 2024

B.3 Conclusion:
Thus we have successfully executed the program to compare two
string without the use of library string function & with use of looping
statement and else if ladder.

PART A
(PART A : TO BE REFFERED BY STUDENTS)

TEC[General Engineering] Page 59


C Programming Lab 2024

Experiment No.13
A.1 Aim:
Find GCD of two numbers.

A.2 Outcome:
After successful completion of this experiment students will be,
Use modular programming approach for diversified problems.

A.3 Theory:
Function:
In c, we can divide a large program into the basic building blocks known as
function. The function contains the set of programming statements enclosed by
{}. A function can be called multiple times to provide reusability and modularity to
the C program.

Function Aspects:
There are three aspects of a C function.
Function declaration: A function must be declared globally in a c program to tell
the compiler about the function name, function parameters, and return type.
Syntax: return_type function_name (argument list);

Function call: Function can be called from anywhere in the program. The
parameter list must not differ in function calling and function declaration. We
must pass the same number of functions as it is declared in the function
declaration.
Syntax: function_name (argument_list);

Function definition: It contains the actual statements which are to be executed. It


is the most important aspect to which the control comes when the function is
called. Here, we must notice that only one value can be returned from the
function.
Syntax: return_type function_name (argument list) {function body;}

TEC[General Engineering] Page 60


C Programming Lab 2024

Return Value:
A C function may or may not return a value from the function. If you don't have to
return any value from the function, use void for the return type.

GCD of two numbers:


The GCD is a mathematical term for the Greatest Common Divisor of two or
more numbers. It is the Greatest common divisor that completely divides two or
more numbers without leaving any remainder. Therefore, it is also known as
the Highest Common Factor (HCF) of two numbers. For example, the GCD of two
numbers, 20 and 28, is 4 because both 20 and 28 are completely divisible by 1, 2,
4 (the remainder is 0), and the largest positive number among the factors 1, 2,
and 4 is 4. Similarly, the GCD of two numbers, 24 and 60, is 12.

4. Algorithm:
Main Function:
Step 1: Start
Step 2: Read n1 and n2
Step 3: call result = gcd(n1, n2)
Step 4: Display result
Step 5: Stop

gcd function:
Step 1: Start
Step 2: for i = 0 to n1 & n2
if n1%i == 0 && n2%i == 0
result = i

TEC[General Engineering] Page 61


C Programming Lab 2024

Step 3: return result


Step 4: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: 27/02/2024 Date of Submission:

Grade:

B.1 Code:
B.2 Input and Output Screenshot:
B.3 Conclusion:

PART A

TEC[General Engineering] Page 62


C Programming Lab 2024

(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.14
A.1 Aim:
Search an element in the array using function.

A.2 Outcome:
After successful completion of this experiment students will be,
Represent data in arrays, strings and structures and manipulate them through a
program.

A.3 Theory:
Linear Search:
Linear search is a very simple search algorithm. In this type of search, a
sequential search is made over all items one by one. Every item is checked and if
a match is found then that particular item is returned, otherwise the search
continues till the end of the data collection.

Working:

Let the elements of array are -

Let the element to be searched is K = 41

Now, start from the first element and compare K with each element of the array.

The value of K, i.e., 41, is not matched with the first element of the array. So,

TEC[General Engineering] Page 63


C Programming Lab 2024

move to the next element. And follow the same process until the respective
element is found.

4. Algorithm:
Main Function:
Step 1: Start
Step 2: Read array arr[], key
Step 3: Call search(arr, key)

TEC[General Engineering] Page 64


C Programming Lab 2024

Step 3: Stop

search function:
Step 1: Start
Step 2: for i = 0 to n
if arr[i] == key
Display key is present in the array
break
Step 3: if i == n
Display key is not present in the array
Step 4: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: 27/02/2024 Date of Submission:

Grade:

B.1 Code:
//Search a number within the array//
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[5],num,i;
printf("\n Enter 5 array elements:");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
printf("\n Enter number to search:");
scanf("%d",&num);

TEC[General Engineering] Page 65


C Programming Lab 2024

for(i=0;i<5;i++)
{
if(num==arr[i])
{
printf("\n Number found");
break;
}
}
if(i==5)
printf("\n Number not found");
getch();
return 0;
}

B.2 Input and Output Screenshot:

B.3 Conclusion:
Thus We have successfully run the program and find the element in
array.

TEC[General Engineering] Page 66


C Programming Lab 2024

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.15
A.1 Aim:
Write a C program to print Fibonacci series of 20 numbers using recursion.

A.2 Outcome:
After successful completion of this experiment students will be,
Use modular programming approach for diversified problems.

A.3 Theory:
Recursion:
Recursion is the process which comes into existence when a function calls a
copy of itself to work on a smaller problem. Any function which calls itself is
called recursive function, and such function calls are called recursive calls.
Recursion involves several numbers of recursive calls. However, it is important to
impose a termination condition of recursion. Recursion code is shorter than
iterative code however it is difficult to understand.

Recursive Function:
A recursive function performs the tasks by dividing it into the subtasks. There is
a termination condition defined in the function which is satisfied by some
specific subtask. After this, the recursion stops and the final result is returned
from the function.
The case at which the function doesn't recur is called the base case whereas the
instances where the function keeps calling itself to perform a subtask, is called
the recursive case. All the recursive functions can be written using this format.
Each recursive call creates a new copy of that method in the memory. Once
some data is returned by the method, the copy is removed from the memory.
Since all the variables and other stuff declared inside function get stored in the
stack, therefore a separate stack is maintained at each recursive call. Once the
value is returned from the corresponding function, the stack gets destroyed.

TEC[General Engineering] Page 67


C Programming Lab 2024

Recursion involves so much complexity in resolving and tracking the values at


each recursive call. Therefore we need to maintain the stack and track the values
of the variables defined in the stack.

Fibonacci Series:
Fibonacci series is a series of numbers formed by the addition of the preceding
two numbers in the series. The first two terms are zero and one respectively. The
terms after this are generated by simply adding the previous two terms.

4. Algorithm:
Main Function:
Step 1: Start
Step 2: Read n
Step 3: for i = 0 to n
f = fibo(i)
display f
i++
Step 4: Stop

fibo function:
Step 1: Start
Step 2: if n==0 || n==1
Return n
Else
Return fibo(n-1)+fibo(n-2)
Step 3: Stop

TEC[General Engineering] Page 68


C Programming Lab 2024

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: 12/03/2024 Date of Submission:

Grade:

B.1 Code:
//WAP to print fibonacci series of 20 numbers using Recursion//
#include<stdio.h>
#include<conio.h>
void fibo(int);
void main()
{
int n;
clrscr();
printf("\n Enter number of terms:");
scanf("%d",&n);
printf("\n 0 \n 1");
fibo(n-2);
getch();
}
void fibo(int n)
{
static int n1=0,n2=1,n3;
if(n>0)
{
n3=n1+n2;
n1=n2;
n2=n3;
printf("\n %d",n3);
fibo(n-1);
}
}

TEC[General Engineering] Page 69


C Programming Lab 2024

B.2 Input and Output Screenshot:

B.3 Conclusion:
Hence, we have successfully executed the program with use of
recursion and display Fibonacci series.

TEC[General Engineering] Page 70


C Programming Lab 2024

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.16
A.1 Aim:
Find product of digits of a number using recursion.

A.2 Outcome:
After successful completion of this experiment students will be,
Use modular programming approach for diversified problems.

A.3 Theory:
Recursive Function:
A recursive function performs the tasks by dividing it into the subtasks. There is
a termination condition defined in the function which is satisfied by some
specific subtask. After this, the recursion stops and the final result is returned
from the function.

Product of digits of number:

TEC[General Engineering] Page 71


C Programming Lab 2024

4. Algorithm:
Main Function:
Step 1: Start
Step 2: Read n
Step 3: product = pro(n)
Step 4: Display product
Step 5: Stop

pro function:
Step 1: Start
Step 2: if n<10
Return n
Else
Return n%10*pro(n/10)
Step 3: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment:12/03/2024 Date of Submission:

Grade:

TEC[General Engineering] Page 72


C Programming Lab 2024

B.1 Code:
B.2 Input and Output Screenshot:
B.3 Conclusion:

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.17
A.1 Aim:
Store product details in structure and calculate total amount.

A.2 Outcome:
After successful completion of this experiment students will be,
Represent data in arrays, strings and structures and manipulate them through a
program.

A.3 Theory:

Structure in c is a user-defined data type that enables us to store the collection of


different data types. Each element of a structure is called a member.

The struct keyword is used to define the structure.

Syntax:
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};

Structure Example:
struct employee

TEC[General Engineering] Page 73


C Programming Lab 2024

{ int id;
char name[20];
float salary;
};

struct is the keyword; employee is the name of the structure; id, name,
and salary are the members or fields of the structure.
Declaring structure variable:
We can declare a variable for the structure so that we can access the member of
the structure easily. There are two ways to declare structure variable:

1. By struct keyword within main() function


struct employee
{ int id;
char name[50];
float salary;
};
struct employee e1, e2;

2. By declaring a variable at the time of defining the structure.


struct employee
{ int id;
char name[50];
float salary;
}e1,e2;

Accessing members of the structure:

By . (member or dot operator)


p1.id

TEC[General Engineering] Page 74


C Programming Lab 2024

4. Algorithm:
Step 1: Start
Step 2: Declare structure product with productID, product_name, price, quantity,
total_Product_price
Step 3: Declare array of structure product[10]
Step 4: Rad n and Initialize total = 0
Step 4: for i = 0 to n
Read productID, product_name, price, quantity
total_product_price = price*quantity
Display productID, product_name, price, quantity,
total_Product_price
Step 5: for i = 0 to n
total = total + total_product_price
Step 6: Display total
Step 7: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 0 7 Name: M e e t P o t d a r

Class: K Batch: HK3

Date of Experiment: 19/03/2024 Date of Submission:

Grade:

B.1 Code:

//Expt-17
//store product details in structure and calculate total amount
#include<stdio.h>

TEC[General Engineering] Page 75


C Programming Lab 2024

#include<conio.h>
struct student
{
int id,quantity;
char name[50];
int price,totalprice;
}
p[100];
int main()
{
int i,totalprice;
for (i=0;i<2;++i)
{
printf("\n\nProduct no.%d",i+1);
printf("\nEnter product id:");
scanf("%d",&p[i].id);
printf("Enter product name:");
scanf("%s",p[i].name);
printf("Enter product price:");
scanf("%d",&p[i].price);

TEC[General Engineering] Page 76


C Programming Lab 2024

printf("Enter product quantity:");


scanf("%d",&p[i].quantity);
}
printf("\nDisplaying product information:");
for (i=0;i<2;++i)
{
printf("\n\nEnter product id:%d",p[i].id);
printf("\nEnter product name:%s",p[i].name);
printf("\nEnter product price:%d",p[i].price);
printf("\nEnter product quantity:%d",p[i].quantity);
p[i].totalprice=p[i].price*p[i].quantity;
printf("\nTotal product price:%d",p[i].totalprice);
}
printf("\n\nTotalpriceofallproducts:%d",p[0].totalprice+p[1].total
price);
getch();
return 0;
}

TEC[General Engineering] Page 77


C Programming Lab 2024

B.2 Input and Output Screenshot:

B.3 Conclusion:
Hence, we have successfully executed the program with use of
store products details.

TEC[General Engineering] Page 78


C Programming Lab 2024

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.18
A.1 Aim:
Demonstrate the example of nested structure.

A.2 Outcome:
After successful completion of this experiment students will be,
Represent data in arrays, strings and structures and manipulate them through a
program.

A.3 Theory:
Nested Structure:
Nested structure in C is nothing but structure within structure. One structure can
be declared inside other structure as we declare structure members inside a
structure.
C provides us the feature of nesting one structure within another structure by
using which, complex data types are created. For example, we may need to store
the address of an entity employee in a structure. The attribute address may also
have the subparts as street number, city, state, and pin code. Hence, to store the
address of the employee, we need to store the address of the employee into a
separate structure and nest the structure address into the structure employee.
The structure can be nested in the following ways.
1. By separate structure
2. By Embedded structure

1) Separate structure
Here, we create two structures, but the dependent structure should be used
inside the main structure as a member.
struct Date
{
int dd;
int mm;

TEC[General Engineering] Page 79


C Programming Lab 2024

int yyyy;
};
struct Employee
{
int id;
char name[20];
struct Date doj;
}emp1;

2) Embedded structure

The embedded structure enables us to declare the structure inside the structure.
Hence, it requires less line of codes but it cannot be used in multiple data
structures.

struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}emp1;

Accessing Nested Structure:

We can access the member of the nested structure by


Outer_Structure.Nested_Structure.member as given below:

e1.doj.dd
e1.doj.mm
e1.doj.yyyy

4. Algorithm:
Step 1: Start
Step 2: Declare structure student with rollno, name, marks, structure address
with plotno, building, pin
Step 3: Display Enter student record
Step 4: Read rollno, name, marks, plotno, building, pin
Step 5: Display rollno, name, marks, plotno, building, pin

TEC[General Engineering] Page 80


C Programming Lab 2024

Step 6: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 0 7 Name: Meet Potdar

Class: K Batch: HK3

Date of Experiment: 19/3/2024 Date of Submission:

Grade:

B.1 Code
//Experiment 18
#include<stdio.h>
#include<conio.h>
struct student{
int rollno;
char name[20];
char div;
float marks;
struct adress
{
int plotno;
char building[20];
int pin;
}a;
};
int main()
{

TEC[General Engineering] Page 81


C Programming Lab 2024

struct student s1;clrscr();


printf("\nEnter student information(rollno,name,div,marks,plot
no,building name,pincode):");
scanf("%d %s %c %f %d %s
%d",&s1.rollno,s1.name,&s1.div,&s1.marks,&s1.a.plotno,s1.a.building,
&s1.a.pin);
printf("%d %s %c %f %d %s
%d",s1.rollno,s1.name,s1.div,s1.marks,s1.a.plotno,s1.a.building,s1.a.
pin);
getch();
return 0;
}
B.2 Input and Output Screenshot:

B.3 Conclusion: Hence, we have successfully executed the program


with use of nested structures.

TEC[General Engineering] Page 82


C Programming Lab 2024

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.19
A.1 Aim:
Swap two numbers by call by reference.

A.2 Outcome:
After successful completion of this experiment students will be,
Use modular programming approach for diversified problems.

A.3 Theory:
Call by Reference:
In call by reference, the address of the variable is passed into the function call as
the actual parameter. The value of the actual parameters can be modified by
changing the formal parameters since the address of the actual parameters is
passed.
In call by reference, the memory allocation is similar for both formal parameters
and actual parameters. All the operations in the function are performed on the
value stored at the address of the actual parameters, and the modified value gets
stored at the same address.

4. Algorithm:
Main function
Step 1: Start.
Step 2: Display “enter any two numbers”
Step 3: Read a, b

TEC[General Engineering] Page 83


C Programming Lab 2024

Step 4: Display values of a and b before swap


Step 5: Call swap(&a,&b)
Step 6: Display values of a nd b after swap
Step 7: Stop

Swap function
Step 1: Start
Step 2: temp=*p
Step 3: *p=*q
Step 4: *q=temp
Step 5: Display a, b
Step 6: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: 26/03/2024 Date of Submission:

Grade:

B.1 Code:
B.2 Input and Output Screenshot:
B.3 Conclusion:

TEC[General Engineering] Page 84


C Programming Lab 2024

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.20
A.1 Aim:
Print a string using pointer.

A.2 Outcome:
After successful completion of this experiment students will be,
Demonstrate the concept of call by reference, dynamic memory allocation using
pointers

A.3 Theory:
Pointer:

The pointer in C language is a variable which stores the address of another


variable. This variable can be of type int, char, array, function, or any other pointer.
The size of the pointer depends on the architecture.

Consider the following example to define a pointer which stores the address of a
character.

char n = ‘a’;
char* p = &n;
// Variable p of type pointer is pointing to the address of the variable n of type ch
aracter.

Declaring a pointer

The pointer in c language can be declared using * (asterisk symbol). It is also


known as indirection pointer used to dereference a pointer.

int *a;//pointer to int


char *c;//pointer to char

TEC[General Engineering] Page 85


C Programming Lab 2024

String and Pointer:

When we create an array, the variable name points to the address of the first
element of the array. The other way to put this is the variable name of the array
points to its starting position in the memory.

We can create a character pointer to string in C that points to the starting


address of the character array. This pointer will point to the starting address of
the string that is the first character of the string, and we can dereference the
pointer to access the value of the string.

// charater array storing the string 'String'


char str[7] = "String";
// pointer storing the starting address of the character array str
char *ptr = str;

Accessing String via a Pointer

An array is a contiguous block of memory, and when pointer to string in C are


used to point them, the pointer stores the starting address of the array. Similarly,
when we point a char array to a pointer, we pass the base address of the array to
the pointer. The pointer variable can be dereferenced using the asterisk * symbol
in C to get the character stored in the address.

TEC[General Engineering] Page 86


C Programming Lab 2024

In this case, ptr points to the starting character in the array arr i.e. H. To get the
value of the first character, we can use the * symbol, so the value of *ptr will be H.
Similarly, to get the value of ith character, we can add i to the pointer ptr and
dereference its value to get ith character.
Instead of incrementing the pointer manually to get the value of the string, we
can use a simple fact that our string terminates with a null \0 character and use
a while loop to increment pointer value and print each character till our pointer
points to a null character.

4. Algorithm:
Step 1: Start.
Step 2: Read string str
Step 3: Declare *ptr and initialize ptr = str
Step 4: Initialize i = 0
Step 4: while *ptr != ‘\0’
Display *ptr
Increment ptr
Step 5: Stop

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No. 18 Name: Atharv Wagh

Class: K Batch: HK3

Date of Experiment: 26/03/2024 Date of Submission:

Grade:

B.1 Code:
B.2 Input and Output Screenshot:
B.3 Conclusion:

TEC[General Engineering] Page 87

You might also like