CP Lab Manual
CP Lab Manual
CP Lab Manual
Lab Manual
Of
C Programming Lab
F.E. (ALL)
SEM II
F. H. 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
**** **
***** *
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
17. Store product details in structure and calculate total amount. LO4
PART A
(PART A : TO BE REFFERED BY STUDENTS)
Experiment No.01
A.1 Aim:
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++).
Example:
int main()
{
int a;
.
.
Example:
int main()
{
int a;
printf("%d", a);
.
.
int main()
{
int a;
printf("%d", a);
return 0;
}
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
Grade:
void main()
{
clrscr();
printf("Hello World \n Atharv Wagh");
getch();
}
B.2 Output:
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.
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;
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
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
Grade:
scanf("%lf",&d);
getch();
}
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.
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.
//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;
}
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)
Grade:
{
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 .
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:
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
}
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
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
Grade:
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
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
}
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
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)
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;
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:");
scanf("%f",&side);
area=side*side;
printf("Area of square=%f\n",area);
break;
default:
printf("Error in shape\n");
break;
}
getch();
}
PART A
(PART A : TO BE REFFERED BY STUDENTS)
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
}
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)
Grade:
B.1 Code:
//Display sq of a number inspecified range//
#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:
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;
}
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
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
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();
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.
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.
}
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
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)
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//
{
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//
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.
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.
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
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.
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).
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]
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
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:");
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];
}
}
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.3 Conclusion:
In this way have successfully executed this program to find
multiplication of two matrix and transpose of the resultant matrix.
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.
6, 2, 11, 7, 5
Step-01: For i = 0
Step-02: For i = 1
Step-03: For i = 2
Step-04: For i = 3
Step-05: For i = 4
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)
Grade:
B.1 Code:
//Sorting numbers using selection sort//
#include<stdio.h>
#include<conio.h>
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.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:
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
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
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:
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:
A.3 Theory:
String Comparison:
Strings can be compared either by using the string function or without using
string function.
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'.
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
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)
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.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)
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);
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.
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
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
Grade:
B.1 Code:
B.2 Input and Output Screenshot:
B.3 Conclusion:
PART A
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:
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,
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)
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)
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);
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.3 Conclusion:
Thus We have successfully run the program and find the element in
array.
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.
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
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
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);
}
}
B.3 Conclusion:
Hence, we have successfully executed the program with use of
recursion and display Fibonacci series.
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.
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)
Grade:
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:
Syntax:
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
Structure Example:
struct employee
{ 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:
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)
Grade:
B.1 Code:
//Expt-17
//store product details in structure and calculate total amount
#include<stdio.h>
#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);
B.3 Conclusion:
Hence, we have successfully executed the program with use of
store products details.
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;
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;
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
Step 6: Stop
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
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()
{
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
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)
Grade:
B.1 Code:
B.2 Input and Output Screenshot:
B.3 Conclusion:
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:
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
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.
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)
Grade:
B.1 Code:
B.2 Input and Output Screenshot:
B.3 Conclusion: