0% found this document useful (0 votes)
6 views

c Lab Manual Final Copy

Uploaded by

rubasudhap
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

c Lab Manual Final Copy

Uploaded by

rubasudhap
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 146

S.A.

ENGINEERING COLLEGE- CHENNAI-77


(An Autonomous Institution Affiliated to Anna University)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

LAB MANUAL

Subject Code: CS3101

Subject Name: PROGRAMMING FOR PROBLEM SOLVING


USING C LABORATORY

Academic Year: 2024-2025(ODD)

Semester: ODD SEM

Year/ Semester: I/I

Submitted By: Mrs.M.Revathi ,Mrs.G.Saranya,Mrs.V.Saraswathy ,


Mrs.C.Lakshmi Priya

Faculty In-charge Signature of the HOD


S.A. ENGINEERING COLLEGE- CHENNAI-77
(An Autonomous Institution Affiliated to Anna University)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

VISION OF THE DEPARTMENT


To conceive our department as Centre of Academic Excellence by catering quality education
with ethical standards

MISSION OF THE DEPARTMENT


M1: To create a conducive atmosphere to achieve active professionalism by fortifying academic
proficiency with ethical standards.
M2: To enhance the confidence level to develop sustainable solution to upgrade the society
forever.
M3: To empower the students with prerequisite professional skills for enhancing employability
and entrepreneurship, continuing education and research.
S.A. ENGINEERING COLLEGE- CHENNAI-77
(An Autonomous Institution Affiliated to Anna University)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

PROGRAMME EDUCATIONAL OBJECTIVES

PEO 1: Graduates shall have professional competency in the field of Computer Science and
Engineering for pursuing higher education, research or as entrepreneurs.

PEO 2: Graduates shall work in a business environment with ethical standards, leadership
qualities and communication necessary for engineering principles.

PEO 3: Graduates shall adapt to emerging technologies and respond to the challenges of the
environment and society forever.

PROGRAMME SPECIFIC OUTCOMES

PSO 1: To analyze, design and develop computing solutions by applying foundational


concepts of Computer Science and Engineering.

PSO 2: To apply software engineering principles and practices for developing quality
software for scientific and business applications.

PSO 3: To adapt to emerging Information and Communication Technologies (ICT) to


innovate ideas and solutions to existing/novel problems.
S.A.ENGINEERING COLLEGE
(An Autonomous Institution Affiliated to Anna University)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

PROGRAMME OUTCOMES
Engineering Graduates will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modelling to complex
engineering activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give
and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member
and leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change.
CS3102 PROGRAMMING FOR PROBLEM SOLVING
USING C LABORATORY LTPC
0042
COURSE OBJECTIVES:

To develop C Programs with


 Basic programming constructs.
 Control Statements
 Arrays, Strings and Pointers
 Functions and structures.
 File Processing.

LIST OF EXPERIMENTS

1. Input and Output statements.


2. Control statements – Branching &Looping.
• Write a C program to generate Pascal's triangle.
• Write a C program, which takes two integer operands and one operator from the user,
Performs the operation and then prints the result. (Consider the operators +,-,*,/,% and
use
Switch Statement)
• Write a C program to find the sum of individual digits of a positive integer.
• A Fibonacci sequence is defined as follows: the first and second terms in the sequence
are
0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence.
• Write a C program to generate the first n terms of the sequence.
• Write a C program to generate all the prime numbers between 1 and n, where n is a
value
Supplied by the user.
•Write a C program to swap Numbers Using Temporary Variables.
3. Arrays
• Write a C program to search an array element using linear search.
• Write a C program to find both the largest and smallest number in a list of integers.
• Write a C program that uses functions to perform the following:
Addition of Two Matrices
Multiplication of Two Matrices
Write a C program to implement Bubble Sort.
4. Strings
• Write a C program that uses functions to perform following operations
(i)To insert a sub-string in to given main string from a given position.
(ii)To delete n Characters from a given position in a given string.
• Write a C program to determine if the given string is a palindrome or not
5. Functions &Pointers:
Write C programs that use recursive functions
(i) To find factorial of given number
(ii)To solve Towers of Hanoi Problem.
(iii)To swap the variables using call by value and call by reference.
6. Generate mark sheet of students using structures.
7. Compute Salary Slip for five employees using structures and functions Insert, Update, delete
and append telephone details of an individual or a company into a telephone directory using
random access file.
LIST OF EQUIPMENTS FOR A BATCH OF 30 STUDENTS:
HARDWARE: 30 terminals
SOFTWARE: C compiler
TOTAL: 60 PERIODS

COURSE OUTCOMES:
At the end of the course, students would develop C programs with
• Simple applications making use of basic constructs
• Control statements.
• Involving Arrays, strings and pointers.
• Involving functions, and structures.
• Sequential and random access files processing.
S.A. ENGINEERING COLLEGE, CHENNAI 600 077
(An Autonomous Institution, Affiliated to Anna University)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

COURSE OBJECTIVES
● To develop programs in C using basic constructs.
● To develop programs in C using control statements.
● To develop applications in C using arrays, strings, pointers.
● To develop applications in C using functions, structures.
● To develop applications in C using file processing
COURSE OUTCOMES
CS3102.1: Develop C programs for simple applications making use
of basic constructs.
CS3102.2: Develop C programs for control statements.
CS3102.3: Develop C programs involving arrays, strings and pointers.
CS3102.4: Develop C programs involving functions, and structures.
CS3102.5: Design applications using sequential and random access file processing.
CS3102 PROGRAMMING FOR PROBLEM SOLVING USING C
LABORATORY (CO-PO ,PSO MAP)
CO/P PO PO PO PO PO PO PO PO PO PO PO PO PS PS PS
O 1 2 3 4 5 6 7 8 9 10 11 12 O1 O2 O3

CO1 2 2 2 2 1 1 2 1
CO2 2 2 2 2 2 2 1
CO3 2 3 2 1
CO4 3 2 1
CO5 1 1 1 1 1 1 2 1
CO6 2 2 2 1

Enter correlation levels as 1, 2, 3


1 Slight (Low) 2 Moderate (Medium) 3. Substantial (High)
EX.NO: 1A AREA OF THE CIRCLE

AIM: To write a C program to compute find the Area of the circle.


ALGORITHM:

Step 1: Start the program.


Step 2: Declare the variables of respective data type. Area->integer

Radius->integer
Step 3: Get the radius.

Step 4: Calculate area=3.14*radius*radius Step 5:

Step 5: Print the area of the circle.


Step 6: Stop the Program.

PROGRAM: /*Area of Circle*/


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

void main()

float radius,area;
printf("Enter the Radius of the circle:\n");

scanf("%f",&radius);
printf(“\nradius=%f”,radius);

area=3.14*radius*radius;

printf("\nArea=%f",area);
getch();
clrscr();
}

OUTPUT:

Enter the Radius of the circle: 5


Radius=5

Area=78.500000

RESULT:

Thus, the program was compiled and executed

EX.NO: 1B Implement different I/O Functions

AIM: To write a C program to implement different I/O Functions.


ALGORITHM:

Step 1: Start the program.


Step 2: Declare the variables of respective data type.
Step 3: Get the variable value with different Input functions.
Step 4: Print the value with different Output functions.
Step 5: Stop the program.

PROGRAM-1:

#include <stdio.h>
void main()

char ch;
printf(“\nEnter any character : “);
ch = getchar();

printf(“\nYou have entered : %c”,ch);


}

OUTPUT: Enter any character : A

You have entered : A

PROGRAM-2:

#include<stdio.h>

void main()

char ch;

printf("\nEnter any character : ");

ch = getch();
putchar(ch);

OUTPUT:

Enter any character : A

You have entered : A

PROGRAM-3:

#include <stdio.h>

void main()

{
char name[30];

printf("\nEnter your website: ");

gets(name);

puts(name);

OUTPUT:

Enter your website: www.saec.ac.in

www.saec.ac.in

PROGRAM-4:
#include <stdio.h>
int main()

int num;

printf("C Programming"); //displays the content


inside quotation scanf(“%d”,&num);

printf(%d”,num);

return 0;

OUTPUT:

C Programming
5
5

RESULT:

Thus, the program was compiled and executed successfully.

EX.NO: 1C USAGE OF SIZEOF ()

AIM: To write a C program to find the size of variables using sizeof function.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables of respective data type.

Step 3: Get the variable value with different


Input functions. Step 4: Print the variable size
using sizeof function.

Step 5: Stop the program.

C PROGRAM

#include<stdio.h>

int main()

int intType;
float floatType;

double doubleType;
char charType;

// sizeof evaluates the size of a variable

printf("Size of int: %zu bytes\n", sizeof(intType));

printf("Size of float: %zu bytes\n",


sizeof(floatType)); printf("Size of double:
%zu bytes\n", sizeof(doubleType));
printf("Size of char: %zu byte\n",
sizeof(charType));
return 0;

OUTPUT:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte

RESULT:

Thus, the program was compiled and executed successfully.

EX.NO: 1D ASCII VALUE OF CHARACTER

AIM: To write a C program to find ASCII value of a character.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the variables of


respective data type. Step 3: Print the
variable ASCII value with variable.
Step 4: Stop the program.

C PROGRAM

//Program to Print ASCII Value

#include <stdio.h>

int main()

{
char c;

printf("Enter a character: ");


scanf("%c", &c);

// %d displays the integer value of a


character // %c displays the actual
character
printf("ASCII value of %c = %d", c,);

return 0;

}
OUTPUT:

Enter a character: G

ASCII value of G = 71

RESULT: Thus, the program was compiled and executed successfully.

EX.NO: 1E SWAPPING OF 2 NUMBERS WITHOUT


THIRD VARIABLE
AIM: To write a C program to swap two numbers without using third variable.

ALGORITHM: -

Step 1: Start the program.

Step 2: Declare the variables x,y and get two


values from user. Step 3: swap two numbers
with the following logic.

x = x + y;
y = x - y;

x = x - y;

Step 4:Print the variables x,y.

Step 5: Stop the program.


C PROGRAM

#include<stdio.h>

int main()

int x, y;
printf("Enter the value of x and y?");
scanf("%d %d",&x,&y);
printf("before swapping numbers: %d %d\n",x,y);
/*swapping*/
x = x + y;
y = x - y;
x = x - y;
printf("After swapping: %d %d",x,y);
return 0;

OUTPUT: Enter the value of x and y?


13

22

before swapping numbers: 13 22

After swapping: 22 13

RESULT:
Thus, the program was compiled and executed successfully.
EX.NO: 1F ILLUSTRATION OF UNARY OPERATORS

AIM: To write a C program to illustrate the unary operators.

ALGORITHM:

Step 1: Start the program.


Step 2: Declare the variable and get a value from the user.
Step 3: implement the unary operator logic with the variable.
Unary -/+: negative number becomes positive and positive number
becomes negative ++/--: increment or decrement value by 1 before
assigning the value Step 4: Print the variable value after operation.

Step 5: Stop the program.

PROGRAM-1- Unary minus

//used to include basice c library files


//unary minus operation performed, negative number becomes positive and
positive number becomes negative

#include <stdio.h>

int main()

int a, unaryMinus;

printf("Please enter any number for unary - symbol: \n");


scanf("%d",&a);

unaryMinus=-(a);

printf("Unary minus operation of %d is = %d ",a, unaryMinus);


return 0;

OUTPUT:

Please enter any number for unary - symbol:

-56

Unary minus operation of -56 is = 56


PROGRAM-2- Unary increment operator

//used to include basice c library files

#include <stdio.h>

int main()

int a, pre_increment,post_increment;

printf("Please enter any number \n");

scanf("%d",&a);
//take temp variable for showing actual
number in output int temp=a;

//increment value by 1 before assigning the value

pre_increment=++a;

//displaying output

printf("Pre increment operation of %d is =%d ",temp,


pre_increment); return 0;

OUTPUT:

Please enter any number

10
Pre increment operation of 10 is =11

RESULT:

Thus, the program was compiled and executed successfully.

EX NO:- 2A A PASCAL’S TRIANGLE

AIM:
To write the c Program to generate pascal’s triangle.

ALGORITHM

1. Start the program

2. Declare variables n,row,r,space,ncr.

3. Take input (num) for number of rows

4. Iterate loop1 for 'num' times

5. Iterate loop2 inside loop1 for (num-1) times

6. Iterate loop3 inside loop1 after loop2 for 0-i times


7. Inside loop3 print num if(n==0||r==0)
8. Inside loop3 else than condition 7 calculate coefficients according
to this formula ncr=ncr*(n-r+1)/r;

9. Print numbers after 3 spaces under loop2

10. Print new line under loop1


11. End

PROGRAM:

#include <stdio.h>

int main()

int n,row,r,space,ncr;
printf("enter the row value");

scanf("%d",&row);

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

for(space=1;space<row-n;space++)

{ printf(" "); }

for(r=0;r<=n;r++) {

if(n==0||r==0)
{ ncr=1;
printf("%d ",ncr);
}

else

{ ncr=ncr*(n-r+1)/r;
printf("%d ",ncr);
}
} printf("\n");
}
return 0;

OUTPUT:

enter the row value: 5


1
11
121
1331
14641

RESULT: Thus the c Program to generate Pascal’s triangle is executed


successfully.

EXNO: - 2B) ARITHMETIC CALCULATOR

AIM:-

To write a C program to design a calculator to perform the operations


namely, addition, subtraction, multiplication, division of number.

ALGORITHM:-
Step 1: Start the program.
Step 2: Declare the variables of respective data type.
Step 3: Get the Number.
Step 4: Read the choice to perform the operation using switch case.
Step 5: Print the result.
Step 6: Stop.

PROGRAM:-

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

void main()

int a,b,choice;

printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");


printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :

printf("Sum of %d and %d is :
%d",a,b,a+b); break;

case 2 :

printf("Difference of %d and %d is :
%d",a,b,a-b); break;

case 3 :

printf("Multiplication of %d and %d is :
%d",a,b,a*b); break;
case 4 :

printf("Division of Two Numbers is %d


: ",a/b); break;

default :
printf(" Enter Your Correct Choice.");
break;
}

getch();

clrscr();

}
OUTPUT:

1.Addition

2.Subtraction
3.Multiplication

4.Division

Enter the values of a & b: 7

Enter your Choice : 1

Sum of 7 and 2 is : 9

RESULT:

Thus a C program to design a calculator to perform the operations


namely, addition, subtraction, multiplication, division of number is
executed.
EXNO:- 2C) SUM OF DIGITS

AIM:-

To write a program to find sum of digits

ALGORITHM:-

Step 1: Start the program.


Step 2: Declare the variables of respective data type.

n,r,sum->integer.

Step 3: Set sum=0


Step 4: If n>0 then
Step 5: Calculate r=n%10
Sum=sum+r
n=n/10

Step 6: Print sum


Step 7: Stop

PROGRAM:-

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

void main()

int number, r, sum=0;

printf("Enter the number:\n");


scanf("%d",&number);

while(number>0)

{
r=number%10;

sum=sum+r;

number=number/10;
}

printf("\nSum of digits =%d",sum);

getch();
clrscr();

}
OUTPUT :-
Enter the number: 92

Sum of digits=11

RESULT:-

Thus the sum of digits using C program has been executed and verified
successfully

EX NO:- 2D) FIBONACCI SERIES

AIM:

To write a c program to compute Fibonacci series.


Algorithm:

Step 1: Start
Step 2: Declare variable n, first, second, next, c;

Step 3: Initialize variable first = 0, second = 1


Step 4: Read n from user

Step 5: Print fib series

Step 6: Repeat until c < n : next = first + second; first = second; second = next;
Step 7: Stop

Program:

#include<stdio.h>

main()
{

int n, first = 0, second = 1, next, c;


printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
return 0;

Output:

Enter the number of terms


5

First 5 terms of Fibonacci series are :-

01123

Result:

Thus a c program to compute fibonacci series is executed successfully.

EXNO:- 2E) PRIME NUMBERS BETWEEN 1


TO N

Aim:

Write a c program to find Prime Numbers


between 1 to N.
Algorithm:

Step 1:START the program

step2:Declare the variablenum,i,count,n;

Step 3perform num%i==0 and track the count.

Step 4:exactly find the factors, if(count==0


&& num!= Step 5 print num

Step 6:stop

program:

#include<stdio.h>
void main(){
int i, num, n, count;
printf("Enter the range: \n");
scanf("%d", &n);
printf("The prime numbers in between the range 1 to %d:",n);
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d ",num);
}
}

Output:

Enter max range: 30

2 3 5 7 11 13 17 19 23 29

Result:

Thus a C program to find Prime Numbers between 1 to N is executed Successfully.


EX.NO 3A) LINEAR SEARCH

AIM

To write the c Program to search an array element


using Linear Search ALGORITHM

Step1: Start the program


Step2: Declare the variables with the respective data type
Step3: Get the number of elements for array
Step4: Get the elements
Step5: Get the number to be searched

Step6: If the number searched is found, it will return the position. If,
not will return the searched element is not found

Steps7: Stop the program


PROGRAM

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

int main()

int array[100], search, c, n;


clrscr();
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element
is found */ {

printf("%d is present at location %d.\n",


search, c+1); break;

} }
if (c == n)
printf("%d isn't present in the array.\n", search);

getch();

return 0;

} OUTPUT
RESULT:

Thus the program to search an element from array is executed successfully.

EXNO: 3B) LARGEST AND SMALLEST NUMBER IN


LIST

AIM

To write the C program to find both the largest and smallest number
in a list of integers
ALGORITHM
Step1: Start the program
Step2: Declare the variables with respective data type.

Step3: Get the number of elements and get the elements for
the array given Step4: Compare the elements in the array

Step5: Print the largest and Smallest element in array given


Step6: Stop the Program

PROGRAM

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

int main()

{
int a[50],i,n,large,small;

clrscr();

printf("How many elements:");

scanf("%d",&n);

printf("Enter the Array:");

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

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

large=small=a[0];
for(i=1;i<n;++i)
{

if(a[i]>large)

large=a[i];

if(a[i]<small)

small=a[i];

printf("The largest element is %d",large);


printf("\nThe smallest element is %d",small);

getch();
return 0;

OUTPUT

RESULT:

Thus the program to find both the largest and smallest number in a list of
integers is executed successfully.

EX.NO 3C I) MATRIX ADDITION

AIM:
To write the C program to perform the
addition of matrix
ALGORITHM:

Step1: Start the program


Step2: Declare the variables with respective data type Step3: Enter the number of
rows and columns for each matrix Step4: Enter the elements for each matrix

Step5: Add the two matrix and print the


resultant matrix Step6: Stop the program

PROGRAM

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

int main() {

int r, c, a[100][100], b[100][100], sum[100]


[100], i, j; clrscr() ; printf("Enter the number of
rows (between 1 and 100): "); scanf("%d", &r);
printf("Enter the number of columns (between 1
and 100): "); scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {

printf("Enter element a%d%d: ", i +


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

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {

printf("Enter element b%d%d: ", i +


1, j + 1); scanf("%d", &b[i][j]);

}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
} }

getch();

return 0;

OUTPUT
RESULT:

Thus the program to perform the addition of matrix is executed successfully.

EX.NO 3C II) MATRIX


MULTIPLICATION

AIM
To Write the C program to perform
multiplication of matix ALGORITHM

Step1: Start the program

Step2: Declare the variables with respective


data type Step3: Enter the number of rows and
columns for each matrix Step4: Enter the
elements for each matrix

Step5: Multiply the matrix and print the


resultant matrix Step6: Stop the program

PROGRAM

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

int main()
{

int m, n, p, q, c, d, k, sum = 0;

int first[10][10], second[10][10],


multiply[10][10]; clrscr();

printf("Enter number of rows and columns of


first matrix\n"); scanf("%d%d", &m, &n);

printf("Enter elements of first matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter number of rows and columns of


second matrix\n"); scanf("%d%d", &p, &q);

if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");

for (c = 0; c < p; 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 the matrices:\n");

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

getch();

return 0;

}
OUTPUT

RESULT

Thus the program for multiplying the matrix is executed successfully.

EX.NO 3D) BUBBLE SORT


AIM: To write the C program to implement Bubble sort.

ALGORITHM
Step1: Start the program
Step2: Declare the variables with their respective data type

Step3: Get the number of elements for the array and get the values
for the array given Step4: Compare the adjacent elements and do
swapping if needed. Step5: Once all the elements are in sorted
order, print the sorted list of elements Step6: Stop the program

PROGRAM

/* Bubble sort code */

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

int main()

{
int array[100], n, c, d, swap;

clrscr();

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

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


scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{

if (array[d] > array[d+1]) /* For decreasing order use '<'


instead of '>' */ {

swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
} printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
getch();

return 0;

OUTPUT

RESULT

Thus the program for arranging the elements in sorted order using
Bubble sort is executed successfully.
EX NO 4 A) WRITE A C PROGRAM THAT USES FUNCTIONS TO
PERFORM THE FOLLOWING OPERATIONS

I) TO INSERT A SUB-STRING IN TO A GIVEN MAIN STRING FROM


A GIVEN POSITION.

AIM: - Write a C - program to insert a sub-string in to a given main string from a


given position

ALGORITHM:

Step 1: Start
Step 2: read main string and sub string
Step 3: find the length of main string(r)
Step 4: find length of sub string(n)
Step 5: copy main string into sub string
Step 6: read the position to insert the sub string(p)
Step 7: copy sub string into main string from position p - 1
Step 8: copy temporary string into main string from position p + n - 1
Step 9: print the strings
Step 10: Stop
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()

char str1[20], str2[20];


int l1, l2, n, i;
clrscr();
puts("Enter the string 1\n");
gets(str1);
l1 = strlen(str1);
puts("Enter the string 2\n");
gets(str2);
l2 = strlen(str2);
printf("Enter the position where the string is to be inserted\n");
scanf("%d", &n);
for(i = n; i < l1; i++)
{
str1[i + l2] = str1[i];
}
for(i = 0; i < l2; i++)
{
str1[n + i] = str2[i];
}
str2[l2 + 1] = '\0';
printf("After inserting the string is %s", str1);
getch();

Input & Output:

Enter the string 1

sachin

Enter the string 2

tendulkar

Enter the position where the string is to be inserted


4
After inserting the string is sachtendulkarin
RESULT:-

Thus the a sub-string in to a given main string from a given position has
been inserted using C program and verified successfully

EX NO 4 A) II) To delete n Characters from a given position in a given


string.

AIM: To Write a C - program to delete n Characters from a given position in a


given string

ALGORITHM:

Step 1: Start
Step 2: read string
Step 3: find the length of the string

Step 4: read the value of number of characters to be deleted and


positioned Step 5: string copy part of string from position to end, and
(position + number of characters to end)

Step 6: Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()

char str[20];
int i, n, l, pos;
clrscr();
puts("Enter the string\n");
gets(str);
printf("Enter the position where the characters are to be deleted\n");
scanf("%d", &pos);
printf("Enter the number of characters to be deleted\n");
scanf("%d", &n);
l = strlen(str);
for(i = pos + n; i < l; i++)

str[i - n] = str[i];
}
str[i - n] = '\0';
printf("The string is %s", str);
getch();

INPUT & OUTPUT:

Enter the string

sachin
Enter the position where characters are to be deleted

Enter the number of characters to be deleted

The string is sain

RESULT:-

Thus the n Characters from a given position in a given string has been
deleted using C program and verified successfully

EX NO 4 B) PALINDROME OR NOT
AIM: To Write a C - program to determine if the given string is palindrome or not

ALGORITHM:

Step 1: start
Step 2: read string A

Step 3: copy string A into B


Step 4: reverse string B
Step 5: compare A & B

If A equals B to got step 6

Else goto step 7

Step 6: print given string A is palindrome


Step 7: print given string is not palindrome
Step 8: stop
PROGRAM:

#include <stdio.h>
#include <string.h>
#include<conio.h>

int main()

char a[100], b[100];

clrscr();

printf("Enter a string to check if it's a palindrome\n");


gets(a);

strcpy(b, a); // Copying input string


strrev(b); // Reversing the string
if (strcmp(a, b) == 0) // Comparing input string with the
reverse string printf("The string is a palindrome.\n");

else
printf("The string isn't a palindrome.\n");

getch();

return 0;

INPUT: Enter a string :madam

OUTPUT:

The length of the string 'madam' = 5


madam is a palindrome

RESULT: Thus the given string whether palindrome or not has been executed using
C program and verified successfully.

EX NO 5A) WRITE C PROGRAMS THAT USE RECURSIVE


FUNCTIONS TO FIND FACTORIAL OF GIVEN NUMBER

AIM: To find the factorial of a given number using recursive function.

ALGORITHM:

Main Program

Step 1: start
Step 2: read n
Step 3: call sub program as f=fact(n)
Step 4: print f value
Step 5: stop

Sub Program:

Step 1: initialize the f

Step 2: if n= = 0 or n == 1 return 1 to main program if


not goto step 3 Step 3: return n*fact(n-1) to main
program.

Program

#include <stdio.h>

int factorial(int n);


int main()

int n,result;
printf("Enter a positive integer: ");
scanf("%d", &n);
result=factorial(n);
printf("Factorial of %d = %d", n,result);
return 0;

int factorial( int n ) /*Function Definition*/

int temp ;
if( n == 0)

return 1 ;
else

temp = n * factorial( n-1 ) ; // Recursive Function Call

return temp ;

output:

Enter a positive integer: 5


Factorial of 5 = 120

RESULT:-

Thus the given number’s factorial value using C program has been executed
and verified successfully.
EX NO:-5B TOWERS OF HANOI

AIM:- To solve tower of Hanoi problem using recursion in C with Turbo C IDE.

ALGORITHM:-

1. START

2. Procedure Hanoi(disk, source, dest, aux)

IF disk == 1, THEN move disk from source to dest


ELSE
Hanoi(disk - 1, source, aux, dest)
move disk from source to dest
Hanoi(disk - 1, aux, dest, source)

3. STOP
PROGRAM:-

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

void hanoi(int , char , char , char );

void main()

int n = 4; // Number of disks


hanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
getch();

void hanoi(int n, char from_rod, char to_rod, char aux_rod)


{

if (n == 1)
{

printf("\n Move disk 1 from rod %c to rod %c",


from_rod, to_rod); return;

}
hanoi(n-1, from_rod, aux_rod, to_rod);

printf("\n Move disk %d from rod %c to rod %c", n,


from_rod, to_rod); hanoi(n-1, aux_rod, to_rod,
from_rod);

OUTPUT:- Move disk 1 from rod A to rod B


Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 3 from rod A to rod B
Move disk 1 from rod C to rod A
Move disk 2 from rod C to rod B
Move disk 1 from rod A to rod B
Move disk 4 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 2 from rod B to rod A
Move disk 1 from rod C to rod A
Move disk 3 from rod B to rod C
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C

RESULT:-

Thus the C program to solve the towers of Hanoi problem is


developed and executed successfully using Turbo C IDE.

EX NO:-5C SWAPPING OF TWO NUMBERS

To swap the variables using call by value:


Aim: To write a C program to swap the variables using call by value method

Algorithm:

Main Function

Step 1: Start
Step 2: Declare the sub function

Step 3: Declare variables num1, num2


Step 4: Read values num1, num2
Step 5: Display num1, num2 before calling swap function

Step 6: call sub program as swap(num1, num2) by passing


arguments using call by value method

Step 7: Display num1, num2 after returned from swap function

Step 8: Stop

Sub Function
Step 1: initialize a, b, temp
Step 2: swap the value, a to temp, b to a, temp to b

Step 3: return to main program without passing value

Program:

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

void swap(int,int) ;

void main()

int num1, num2 ;

num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(num1,num2) ;
printf("\nAfter swap: num1 = %d\nnum2 = %d", num1, num2);

getch() ;

void swap(int a, int b) // called function

int temp ;
temp = a ;
a=b;
b = temp ;

Output:
Before swap: num1 = 10, num2 = 20

After swap: num1 = 10


num2 = 20

Result: Thus the program to swap the variables using call by value in C
programming is developed and executed successfully.

EX NO:-5D To swap the variables using Call by reference

Aim:-To write a C program to swap the variables using call by reference

Algorithm:

Main Function

Step 1: Start
Step 2: Declare the sub function
Step 3: Declare variables num1, num2
Step 4: Read values num1, num2

Step 5: Display num1, num2 before calling swap function

Step 6: call sub program as swap(num1, num2) by passing


arguments using call by reference method

Step 7: Display num1, num2 after returned from swap function


Step 8: Stop
Sub Function
Step 1: initialize a, b, temp
Step 2: swap the value, a to temp, b to a, temp to b
Step 3: return to main program without passing value

Program: #include <stdio.h>

#include<conio.h>

void swap(int *,int *) ;


void main()
{

int num1, num2 ;

num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(&num1,&num2) ;

printf("\nAfter swap: num1 = %d\nnum2 = %d", num1, num2);

getch() ;

void swap(int *a, int *b)

int temp ;
temp = *a ;
*a = *b ;
*b = temp ; }

OUTPUT:

Before swap: num1 = 10, num2 = 20

After swap: num1 = 20


num2 = 10

Result: Thus the program to swap the variables using call by reference in C
programming is developed and executed successfully.

EX: NO:6 A GENERATE STUDENT MARKSHEET –STRUCTURES

AIM: Write a C program to generate marksheet of students and compute grade for
five different subjects using structures.
ALGORITHM:-
1. Start the program

2. Declare a structure with structure elements to hold


marks in 5 subjects 3. Get the user input for the elements
in step 2
4. compute grades,total marks of students using an array of
structure members. 5. Print the student internal mark details

6. Stop the program


PROGRAM:-

#include<stdio.h>
#include<conio.h>
struct mark_sheet

{ char name[20];

long int rollno;


int marks[10];
int total;

float average;
char res[10];
char cl[20];
}students[100];

int main() {
int a,b,n,flag=1;

char ch;

clrscr();

printf("How many students : \n");

scanf("%d",&n);

for(a=1;a<=n;++a){

clrscr();

printf("\n\nEnter the details of %d students : ", n-a+1);

printf("\n\nEnter student %d Name : ", a);

scanf("%s", students[a].name);

printf("\n\nEnter student %d Roll Number : ",


a);

scanf("%ld", &students[a].rollno);
students[a].total=0;

for(b=1;b<=5;++b)

printf("\n\nEnter the mark of subject-%d : ", b);

scanf("%d", &students[a].marks[b]);

students[a].total += students[a].marks[b];

if(students[a].marks[b]<40)

flag=0;

students[a].average = (float)(students[a].total)/5.0;

if((students[a].average>=75)&&(flag==1))

strcpy(students[a].cl,"Distinction");
else if((students[a].average>=60)&&(flag==1)) strcpy(students[a].cl,"First
Class");

else if((students[a].average>=50)&&(flag==1)) strcpy(students[a].cl,"Second


Class");

else if((students[a].average>=40)&&(flag==1)) strcpy(students[a].cl,"Third


Class");

if(flag==1)

strcpy(students[a].rem,"Pass");

else

strcpy(students[a].rem,"Fail");

flag=1;

for(a=1;a<=n;++a){

clrscr();

printf("\n\n\t\t\t\tMark Sheet\n");

printf("\nName of Student : %s", students[a].name);

printf("\t\t\t\t Roll No : %ld", students[a].rollno);


printf("\n------------------------------------------------------------------------");

for(b=1;b<=5;b++)
{

printf("\n\n\t Subject %d \t\t :\t %d", b, students[a].marks[b]);


}

printf("\n\n------------------------------------------------------------------------\n");

printf("\n\n Totl Marks : %d", students[a].total);

printf("\t\t\t\t Average Marks : %5.2f", students[a].average);

printf("\n\n Class : %s", students[a].cl);


printf("\t\t\t\t\t Status : %s", students[a].rem);
printf("\n\n\n\t\t\t\t Press Y for continue . . . ");
ch = getch();
if((ch=="y")||(ch=="Y"))
continue; }
return(0);
}

OUTPUT:-
RESULT:-

Thus the program to generate marksheet of students and


compute grade for five different subjects using structures in C
programming was developed and executed successfully.

EX.NO:6.B EMPLOYEE PAYSLIP-STRUCTURES AND FUNCTIONS

AIM:- Write a C program to Generate Salary Slip of Employees Using Structures


and Pointers.

ALGORITHM:-

1. Start the program

2. Declare a structure with structure elements to hold values of emp


ID, name, basic, HRA, DA, PF, GROSS, NET

3. Get the user input for the elements in step 2

4. Calculate the Gross salary by adding the percentage of basic salary


with respect to HRA and DA

5. Calculate the net salary by deducting the percentage of PF from


the basic salary 6. Print the employee salary slip details

7. Stop the program


PROGRAM CODING:-
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

struct employee {

int empId;

char name[32];

int basic, hra, da;

int pf;

float gross, net; };

void printSalary(struct employee e1)

printf("Salary Slip of %s:\n", e1.name);

printf("Employee ID: %d\n", e1.empId);

printf("Basic Salary: %d\n", e1.basic);

printf("House Rent Allowance: %d\n", e1.hra);

printf("daily allowance:%d",e1.da);

printf("\nProvident Fund : %d\n", e1.pf);


printf("Gross Salary: %.2f Rupees\n", e1.gross);

printf("\nNet Salary: %.2f Rupees\n\n",


e1.net);

return;

void main()

int i, ch, num, flag, empID;

struct employee *e1;

clrscr();

printf("Enter the number of employees:");

scanf("%d", &num);

e1 = (struct employee *)malloc(sizeof(struct employee)


* num);
for (i = 0; i < num; i++)

printf("Employee ID:");

scanf("%d", &(e1[i].empId));

printf("Employee Name:");

scanf("%s",e1[i]);

e1[i].name[strlen(e1[i].name) - 1] = '\0';
printf("Basic Salary");

scanf("%d", &(e1[i].basic));

printf("House Rent Allowance in %");

scanf("%d", &(e1[i].hra));

printf("Daily Allowance in %");

scanf("%d", &(e1[i].da));

printf("Provident Fund in %:");

scanf("%d",&e1[i].pf);

printf("\n");

}
for (i = 0; i < num; i++)

e1[i].gross = e1[i].basic + ((e1[i].hra * e1[i].basic) /


100) + ((e1[i].da * e1[i].basic) / 100 );

e1[i].net = e1[i].gross-(e1[i].basic*e1[i].pf/100) ;

while (1)

printf("Enter employee ID to get payslip:");

scanf("%d", &empID);

flag = 0;

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


{

if (empID == e1[i].empId)

printSalary(e1[i]);

flag = 1;

if (!flag)

printf("No Record Found!!\n");

printf("Do You Want To Continue(1/0):");

scanf("%d", &ch);
if (!ch)

break;

}
getch();

OUTPUT:-

Enter the number of employees : 1


Employee ID : 123 Employee name :
bala Basic salary : 10000 House rent
allowance in % : 80 Daily allowance
in % : 20 Provident fund in % : 14

Enter employee ID to get pay slip :


123 Salary slip of employee ID : 123
Basic salary : 10000 House rent
allowance : 80 Daily allowance : 20

Provident fund : 14
Gross salary : 10168.00 Rupees
Net salary : 10079.00 Rupees
Do you want to continue(1/0) : 0

RESULT:-

Thus the program to generate the payslip of the employees using structure and
functions using C programming is developed and executed successfully.

EX.NO: 7 TELEPHONE DIRECTORY- RANDOM ACCESS FILE

AIM:

To implement a C program to create a telephone directory using


random access file.

ALGORITHM:

Step 1: Start the program


Step 2: Create a structure for telephone directory

Step 3: To insert a record in random file create a file pointer and get
necessary details from user.

Step 4: Using SEEK command move file pointer position to the desired
location and perform file write operation

Step 5: To update a record in a random access file create a file pointer


and get necessary details from user.

Step 6: To delete a record get the record number from the user. If it is
matched in the record number in the file then the details are deleted

Step 7: Stop the program

PROGRAM:-
#include "stdio.h"

#include "string.h"
struct dir

char name[20];

char number[10];

};

void insert(FILE *);

void update(FILE *);

void del(FILE *);

void display(FILE *);

void search(FILE *);

int record = 0;

int main(void)
{

int choice = 0;

FILE *fp = fopen( "data4.dat",


"w+" ); if (fp == NULL ) perror
("Error opening file"); while
(choice != 6)

printf("\n1 insert\t 2 update\n");

printf("3 delete\t 4 display\n");

printf("5 search\t 6 Exit\n


Enter choice:"); scanf("%d",
&choice);

switch(choice)
{

case 1: insert(fp); break;

case 2: update(fp); break;

case 3: del(fp); break;

case 4: display(fp); break;

case 5: search(fp); break;

default: ;

fclose(fp);

return 0;
}

void insert(FILE *fp)

struct dir contact, blank;

//fseek( fp, -sizeof(struct dir),


SEEK_END ); //fread(&blank,
sizeof(struct dir), 1, fp);
printf("Enter individual/company
name: "); scanf("%s",
contact.name);

printf("Enter telephone number: ");

scanf("%s", contact.number);
fwrite(&contact, sizeof(struct
dir), 1, fp); }

void update(FILE *fp)

char name[20], number[10];

int result;

FILE *fptr;

struct dir contact,blank;

fptr=fopen("data4.dat","a+");
printf("Enter name:");

scanf("%s", name);

rewind(fp);

while(!feof(fptr))

result = fread(&contact, sizeof(struct


dir), 1, fptr); if(result != 0 &&
strcmp(name, contact.name) == 0) {

printf("Enter number:");
scanf("%s",number);

strcpy(contact.number,number);

fseek(fptr, -sizeof(struct dir),


SEEK_CUR); fwrite(&contact,
sizeof(struct dir), 1, fptr);
printf("Updated successfully\
n");

fclose(fptr);

return;

printf("Record not found\n");

}
void del(FILE *fp)

char name[20], number[10];

int result, record=0;

struct dir contact, blank = {"", ""};

printf("Enter name:");

scanf("%s", name);

rewind(fp);
while(!feof(fp))

result = fread(&contact, sizeof(struct dir), 1, fp);

if(result != 0 && strcmp(name, contact.name) == 0) {

fseek(fp, record*sizeof(struct dir),


SEEK_SET);

fwrite(&blank, sizeof(struct dir), 1, fp);

printf("%d Deleted successfully\n", record-


1);

return;
}

record++;

printf("not found in %d records\n", record);

void display(FILE *fp)

struct dir contact;


int result;

rewind(fp);

printf("\n\n Telephone directory\n");

printf("%20s %10s\n", "Name",


"Number");
printf("*************************
******\n"); while(!feof(fp))

result = fread(&contact, sizeof(struct dir), 1, fp);

if(result != 0 && strlen(contact.name) > 0)

printf("%20s %10s\n",contact.name, contact.number); }

printf("*************************
******\n"); }
void search(FILE *fp)

struct dir contact;

int result; char name[20];

rewind(fp);

printf("\nEnter name:");

scanf("%s", name);

while(!feof(fp))
{

result = fread(&contact, sizeof(struct dir), 1, fp);

if(result != 0 && strcmp(contact.name, name) == 0) {

printf("\n%20s %10s\n",contact.name, contact.number);


return;

printf("Record not found\n");

OUTPUT:-

1 insert 2 update
3 delete 4 display

5 search 6 Exit

Enter choice: 4

Telephone directory

Name Number

*******************************

bb 11111

*******************************

1 insert 2 update

3 delete 4 display
5 search 6 Exit

Enter choice: 5

Enter name: bb

bb 11111

1 insert 2 update

3 delete 4 display

5 search 6 Exit

Enter choice: 1

Enter
individual/company
name:

aa Enter telephone
number:

222222
1 insert 2 update

3 delete 4 display

5 search 6 Exit

Enter choice: 2

Enter name: aa

Enter number: 333333

Updated successfully

1 insert 2 update

3 delete 4 display

5 search 6 Exit
Enter choice: 4

Telephone directory

Name Number

**********************
********* bb 11111

aa 333333

*******************************

1 insert 2 update
3 delete 4 display

5 search 6 Exit

Enter choice: 3

Enter name: aa

1 Deleted successfully

1 insert 2 update

3 delete 4 display

5 search 6 Exit

Enter choice: 4
Telephone directory

Name Number

**********************
********* bb 11111

*******************************

1 insert 2 update

3 delete 4 display

5 search 6 Exit

Enter choice: 6
RESULT:

Thus the C program on telephone directory was executed successfully.

CONTENT BEYOND SYLLABUS

EXNO: 8 SINGLY LINKED LISTS

Aim:

To Write a C Program for linked list implement of List using


structure and pointer.
Algorithm:

Step1: Create node and set the value as NULL.


Step 2: Read the list operation type.
Step 3: If operation type is create then process the following steps.

1. Allocate memory for node.

2. Read data and assign neat field as NULL.

Step 4: If operation type is Insert then process the following steps.

1. Allocate memory for node.

2. Read data for data area of the node.

3. Read the position the Data to be inserting.


4. Availability of the position is true then assign node as first.

5. If it is in the middle then insert based on the position.

Step5: If operation type is deleting then do the following steps.

● Read the position.


● Check list is Empty .If it is true display the message List empty.
● If position is first then assign head to the next element.

● If the position is last, then move the current node to previous and
make its next as null.

● If it is in middle, then move to that position and link the next


element to the previous node.

1.If position is enter Mediate.

1. Move the cur to required postion.


2. Move the Previous to cur's previous position

3. Move the Next to cur's Next position.

4. Now Assign previous of node as next.

step 6: If operation is traverse.

1. Assign current as first.

2. Repeat the following steps untill cur becomes NULL.

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void insertAtBeginning(int);
void insertAtEnd(int);
void insertBetween(int,int,int);
void display();
void removeBeginning();
void removeEnd();
void removeSpecific(int);

struct Node

{ int data;

struct Node *next;

}*head = NULL;

void main()

{ int choice,value,choice1,loc1,loc2;
clrscr();
while(1){

mainMenu: printf("\n\n********* MENU ************\n1. Insert\


n2. Display\n3. Delete\n4. Exit\nEnter your choice: ");

scanf("%d",&choice);
switch(choice)
{ case 1: printf("Enter the value to be insert: ");

scanf("%d",&value);

while(1){

printf("Where you want to insert: \n1. At Beginning\


n2. At End\n3. Between\nEnter your choice: ");

scanf("%d",&choice1);
switch(choice1)

{ case 1: insertAtBeginning(value);
break;

case 2: insertAtEnd(value);

break;

case 3: printf("Enter the two values where you


wanto insert: "); scanf("%d%d",&loc1,&loc2);

insertBetween(value,loc1,loc2);

break;

default: printf("\nWrong Input!! Try again!!!\n\n");

goto mainMenu;

}
goto subMenuEnd;

subMenuEnd:

break;

case 2: display();

break;

case 3: printf("How do you want to Delete: \n1. From Beginning\n2.


From End\n3. Spesific\nEnter your choice: ");

scanf("%d",&choice1);
switch(choice1)
{

case 1: removeBeginning();

break;

case 2: removeEnd(value);

break;

case 3: printf("Enter the value which you wanto delete: ");

scanf("%d",&loc2);

removeSpecific(loc2);

break;
default: printf("\nWrong Input!! Try again!!!\n\n");

goto mainMenu;

break;

case 4: exit(0);
default: printf("\nWrong input!!! Try again!!\n\n");
}
}

void insertAtBeginning(int value)

{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(head == NULL)
{
newNode->next = NULL;
head = newNode;
}
else
{
newNode->next = head;
head = newNode;
}
printf("\nOne node inserted!!!\n");

void insertAtEnd(int value)

struct Node *newNode;


newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if(head == NULL)
head = newNode;

else
{
struct Node *temp = head;
while(temp->next != NULL)

temp = temp->next;

temp->next = newNode;
}
printf("\nOne node inserted!!!\n");

void insertBetween(int value, int


loc1, int loc2) {

struct Node *newNode;

newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = value;

if(head == NULL)
{
newNode->next = NULL;
head = newNode;
}
else
{
struct Node *temp = head;

while(temp->data != loc1 && temp-


>data != loc2) temp = temp->next;

newNode->next = temp->next;
temp->next = newNode;
}
printf("\nOne node inserted!!!\n");

void removeBeginning()
{

if(head == NULL)

printf("\n\nList is Empty!!!");

else
{
struct Node *temp = head;
if(head->next == NULL)
{

head = NULL;

free(temp);

}
else
{

head = temp->next;
free(temp);

printf("\nOne node deleted!!!\n\n");

} }}

void removeEnd()

if(head == NULL)
{
printf("\nList is Empty!!!\n");
}
else
{
struct Node *temp1 = head,*temp2;
if(head->next == NULL)

head = NULL;
else
{

while(temp1->next != NULL)

temp2 = temp1;
temp1 = temp1->next;

temp2->next = NULL;

}
free(temp1);
printf("\nOne node deleted!!!\n\n");
}

}
void removeSpecific(int delValue)

struct Node *temp1 = head, *temp2;


while(temp1->data != delValue)
{
if(temp1 -> next == NULL){

printf("\nGiven node not found in


the list!!!"); goto functionEnd;

}
temp2 = temp1;
temp1 = temp1 -> next;
}
temp2 -> next = temp1 -> next;
free(temp1);
printf("\nOne node deleted!!!\n\n");
functionEnd:

void display()
{

if(head == NULL)
{
printf("\nList is Empty\n");
}
else
{
struct Node *temp = head;
printf("\n\nList elements are - \n");
while(temp->next != NULL)
{

printf("%d --->",temp->data);

temp = temp->next;

}
printf("%d --->NULL",temp->data);
}

Output:
Result: Thus the C Program to implement List using Linked list has been
executed successfully.

You might also like