0 ratings0% found this document useful (0 votes) 75 views44 pagesCS6212 / IT6212 - Programming and Data Structures Laboratory - I
CS6212 / IT6212 - Programming and Data Structures Laboratory - I
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Rajalakshmi Engineering College
Rajalakshmi Nagar, Thandalam, Chennai - 602 105
Department of Computer Science and Engineering
CS6212 / IT6212 -
Programming and Data Structures Laboratory - IC6212 / 176212 - PROGRAMMING AND DATA STRUCTURES LABORATORY - I
BPG
O30 Se
Objectives:
‘The students should be made to:
> Be familiar with e programming
> Be exposed to implementing abstract data types
> Lear to use files
> Learn to implement sorting and searching algorithms.
Experiments
. C Programs using Conditional and Control Statements.
2. C Programs using Arrays, Strings and Pointers and Functions
3. Representation of records using Structures in © — Creation of Linked List ~ Manipulation of
records in a Linked List.
4, File Handling in C ~ Sequemtial access ~ Random Access.
5. Operations on a Stack and Queue ~ infix to postfix — simple expression evaluation using stacks -
Linked Stack Implementation — Linked Queue Implementation,
Implementation of Sorting algorithms
7. Implementation of Linear search and Binary Search,
TOTAL: 45 PERIODS
Outcomes:
At the end of the course, the student should be able to:
v
Design and implement C programs for implementing stacks, queues, linked lists
v
Apply good programming design methods for program development.
> Apply the different data structures for implementing solutions to practical problems.
v
Develop searching and sorting programs,
BHUVANESWARAN B/ AP (SS)/ CSE / REC - 2Write a program in C to find whether the given number is a perfect
number or not.
/* To find the given no. is perfect no. or not - CHKFERNO.C */
#include
#include
#include
void main()
{
int ip n, 8 = 0;
elrser() ;
printf["Enter a number : ") ;
scanf("$d", &n) ;
for(i = 1; i
#include
void maint)
{
inta=-1,b=1,c-0,i,n;
elrserl) ¢
printf("Enter the limit : ") ¢
scanf("8d", @n) ¢
printf("\nThe fibonacci series is : \n\n") ;
forli= 1; i <= nj itt)
{
cs atb:
print£("$d \t", c) ¢
a=bi
bec:
getch() ;
OUTPUT
RUN 1
Enter the limit : 7
The fibonacci series is :
BHUVANESWARAN B/ AP (SS)/ CSE/ REC - 4Write a program in C to find the factorial of a number using
recursive functions.
/* Pactorial of a given no. using recursive function - FACTREC.C */
#include
f#linclude
int factlint);
void main()
{
int n, i, f= 1:
clrser(}s
printf ("Enter a number : ")i
seanf("d", en);
£ = factinis
printf ("\nFactorial value of #d is : 8d", n, £) 3
geteh() +
int fact{int n)
{
if(n ==0 || n
return 1;
else
return(n * fact(n = 1))3
t
ourpur
RUN 1
Enter a number : 5
Factorial value of 5 is : 120
BHUVANESWARAN B/ AP (SS) / CSE/ REC -5Write a program in C to swap two integers number using pointers.
/* Swap two numbers using pointers - SWAEPTR.C */
#include
#include
void swap{int *, int *);
void main()
t
int x, y;
elrser (1;
printf("Enter the first no. : "li
scanf("d", ax);
printf ("Enter the sec
scanf("8d", ay);
printf ("\nBefore swap :
swaplex, &y)?
printf("\nAfter swap +
getch()s
ond no
void swap(int *a, int *b}
{
int t7
t = 4a;
*a = *b;
te
ourpur
RUN 1
Enter the first no
Enter the second no. : 20
Before swap : x = 10
After swap : x =
BHUVANESWARAN B/ AP (SS) / CSE / REC - 6Write a program in C using dynamic memory allocation to print the
transpose of a matrix.
/*Transp
of a matrix using dynamic memory allocation-MATTDMA. C*/
#include
#include
void main()
{
int **a;
int row, col, i, 4¢
clrser(}s
printf("Enter the order of the matrix : ");
scanf("@d &d", grow, gcol);
a= (int **) malloc(row * siz
for(i = 0 7 i < col ; itt)
{
(int) );
ali] = (int *) malloc(cal * sizeof(int}];
printf("\nEnter the elements :\n");
forli = 0; i < row; i+)
for(] = 0} 3 < col ; J++)
scanf("sa", £4111):
print£("\nThe transpose matrix is : \n\n");
for(j = 0; 4) < col ; j++)
{
forli
{
O07 i < rows itt)
printé("$d \t", ali] [j])s
printf("\n");
ourTPUT
RUN 1
w
Enter the order of the matrix : 2
Enter the elements :
1 2 3
4 5 6
The transpose matrix is
wn
BHUVANESWARAN B/ AP (SS) / CSE/ REC -7Write a function in C to add two matrices and print the resultant
matrix. Write a main function to get the input and invoke the
function.
/* Add two matrices using functio:
- MATADDEN.c */
#include
#include
void readmat(int [][10], int, int);
void addmat(int [][10], int [][10], int [][10], int, int);
void printmat(int [][10], int, int);
void main()
{
int mata[10] [10], matb[10] [10], mate
int row, col;
elrser(};
printf["Enter the order of the matrix
scant ("@d %d", grow, gcol);
printf ("\nEnter the elements for first matrix :
readmat (mata, row, col];
printf ("\nEnter the elements for
readmat (matb, row, col];
print£["\nThe addition of matrix is : \n")s
addmat (mata, matb, mate, row, col);
printmat (mate, row, col);
getch(}s
[101;
"ys
void readmat{int mat[][10], int row, int col)
int i, Ji
for(i = 0; i < row; itt)
for(j = 0: j < col ¢ j++}
scanf("$a", smatlil (31)
}
void addmat (int mata[][10], int matb[][10], int matc[][10],
int row, int col)
{
int i, 43
for(i = 07 i
for(j = 0 3 < col ; j++}
[i115] = matalil [3] + matblil (ils
BHUVANESWARAN B/ AP (SS) / CSE/ REC -8void printmat (int mat[](10], int row, int col)
{
int 4, 4;
for(i = 0
aH)
printe("sd\t", mat(i] [ills
print£("\n");
ourpur
RUN 1
Enter the order of the matrix : 3
w
Enter the elements for first matrix
1 3 5
7 9 a1
13 1s a7
Enter the elements for second matrix :
2 4 6
8 10 ue
14 16 18
The addition of matrix is :
3 7 1
15 19 23
27 31 35
BHUVANESWARAN B/ AP (SS) / CSE/ REC - 9Write different functions in C that will compute and print the area
of any four geometric shapes. Write a main function to get the
input and invoke the function using switch case conditional
statement.
/* Program to compute areas of geometric shapes - AREAS.C */
f#include
#include
void main()
{
int choice;
float side, base, length, breadth, height, area, radius;
print£("1. Circle\n");
printfi"2. Rectangle\
printf("3. Triangle\n"
printf("4. square\n"];
print£("Enter your choice : "Iz
scanf("$d", &choice);
ch (choice)
{
el:
printf("Enter the radius +");
can£("$£", gradius};
area = 3.142 * radius * radius;
printf ("Area of a circle = &f", area);
break;
case 2:
printf("Enter the breadth and length : "
scanf("$f %£", sbreadth, slength);
area = breadth * length;
printf("Area of a Reactangle = $f", areal;
break;
case 3:
print£("Enter the base and height :
scanf("8f #f", sbase, gheight];
area = 0.5 * base * height;
printf("Area of a Triangle = %£", area);
break;
case 4:
printf ("Enter the side : "J;
scanf("8f", &side);
area - side * side;
printf ("Area of a Square = #f\n", area);
break;
default:
printf ("Invalid choice...1")¢
break;
getch(}+
BHUVANESWARAN B/ AP (SS) / CSE / REC - 10ourpur
RUN 1
1. Circle
2. Rectangle
3. Triangle
4. Square
Enter your choice : 1
Enter the radius : 5
Area of a circle = 78.550003
RUN 2
1. Circle
2. Rectangle
3. Triangle
4, Square
Enter your choice : 2
Enter the breadth and length : 3 4
Area of a Reactangle = 12.000000
RUN 3
1. Circle
2. Rectangle
3. Triangle
4. Square
Enter your choice : 3
ns
Enter the base and
Area of a Triangle
height
RUN 4
Circle
Rectangle
Triangle
Square
Enter your choice
Enter the side : 5
Area of a Squa
5.000000
BHUVANESWARAN B/ AP (SS) / CSE / REC - 11Write a program in C to find whether a substring is present in the
string or not.
/* Pind whether a substring is present in the string - SUBSTR.C
#include
#include
#include
void main()
{
char str[60], search[10]
int countl =
clrser()s
printfi"Enter the string
gets(str}z
printf("Enter search substring : "}
gets{search);
countl = strlen(str);
count2 strlen(search];
for (i = 0; i countl - count2; i++)
{
for (j = is 3
onio.h>
‘type
#include <
include <
#include <
h>
void main()
{
char text[80], ch ;
int vowels = 0, cons = 0, digits = 0, spaces = 0,
clrser() +
printf ("Enter the text :") ¢
gets(text) >
while{{ch = tolower(text litt] }] Nor)
{
if(ch=="a' ||
ttyowels ;
else if(ch
+tcons
else if(ch
t+digits ;
else if(ch
t+spaces ;
printf["\nThe text contains : ") ;
print£("\n\nNumber of vowels = $4", vowels) ;
print£("\n\nNumber of consonants = 8d", cons) ;
printf ("\n\nNumber of digits = $d", digits) ;
print£("\n\nNumber of white spaces = $d", spaces)
geten() ¢
ourpur
RUN 1
Enter the text : Kancheepuram Pincode 631501
The text contains :
Number of vowels = 8
Number of consonants = 11
Number of digits
Number of white spaces -
BHUVANESWARAN B/ AP (SS) / CSE / REC - 14
OFWrite a program in C to implement the concept of Sequential Access
in files.
/* Program to write and read data from a file - FILEWRRD.C */
tdio.h>
onio.h>
#include <
include <
void main()
t
FILE *fptr ;
char c ;
elrser() ¢
printf("Enter the text to be stored in the file.\n") +
printf("Use *Z or F6 at the end of the text and press
ENTER: \n\n") >
fptr = fopen( "COURSES. DAT", "w
while(({c = getchar(}} != EOF)
pute(c, fptr) ;
fclose(fptr) +
printf("\nThe content of the file is : \n\n") ;
fptr = fopen("COURSES.DAT™, "r") ;
while({c = getc{fptr)) != EOF}
printf("so", c) ;
felose(fptr) ¢
getch() ;
OUTPUT
RUN 1
Enter the text to be stored in the file.
Use *“Z or F6 at the end of the text and press ENTER:
Computer Science & Engineering
Information Technology
Electronics & Communication Engineering
The content of the file is :
Computer Science & Engineering
Information Technology
Electronics & Communication Engineering
BHUVANESWARAN B/ AP (SS) / CSE / REC - 15Write a program in C to implement the concept of Random Access in
files. (or) Write a C program to perform random access on file.
/* Implementation of random access files - RANDOM.C */
#include
#include <
onio.h>
void main{)
{
FILE *fptrs
long nj
char o
elrser(1;
fptr = fopen("ALPHABET.DAT", "w");
printf ("Enter the alphabets (*Z to end) : \n"I;
while(({c = getchar()} != EOF)
putc(c, fptris
printf("No. of characters entered = $1d\n", ftell(fptr))s
felose(fptr)s
fptrc = fopen("ALPHABET.DAT", "x")¢
n= 01;
while(feof(fptr) == 0}
{
fseek(fptr, n, 015
printf("Position of 8c is #1d\n",
gete(fptr}, ftell(fptr));
nen + Sl;
putchar('\n");
fseek(fptr, -1L, 2);
do
{
putchar(gete(fptr]};
} while(!fseek(fptr, -2L, 1))i
felose(fptr) +
geten() ;
t
ourPUT
RUN 1
Enter the alphabets (°Z to end) :
ABCDEFGHIJKLMNOPQRSTUVWKYZ°Z
No. of characters entered = 26
Position of A is
Position of F is
Position of
Position of
Position of U is
Position of Z is
Position of is
is
is
NouaAnS
web
SRSES
ZYXWVUTSROPONMLI
a
a
a
5
a
o
3
a
3
BHUVANESWARAN B/ AP (SS) / CSE / REC - 16Write a C program to find sum of digit, reversal of digit and
largest of digit.
/* Pind the sum, reverse and largest of digits - SUMREVLA.C */
tdio.h>
onio.h>
#include <
include <
void maint)
{
int n, rem, large
elrserl) ¢
printf ("Enter a number :") ¢
scanf("8d", @n) ¢
while{n > 0}
{
n& 103
sum + rem ¢
= rem + rev * 10;
n=n/10¢
if(rem > large)
large = rem;
printfi"\nThe sum of the digits is : ad", sum) ;
printf ("\nThe reverse of the digit is : 8d", r
printf("\nThe largest of the digit is : %a", largel:
geten() ¢
t
ourpur
RUN 1
Enter a number : 786
The sum of the digits is : 21
The reverse of the digit is : 687
The largest of the digit is : 8
BHUVANESWARAN B/ AP (SS) / CSE / REC - 17Write a C program to generate the Armstrong numbers from 1 - 1000.
/* Program to generate armstrong numbers - GENARMST.C */
#include
#include
void main{)
{
int i, a, ry si
clrser() ¢
print£("\nThe armstrong numbers from 1 to 1000 are z\n\n") ¢
for{i = 0 7 i <= 1000; i++)
{
a-i;
a- 0;
while(a > 0}
t
xr ;
5 eee che
a ;
if(i == s)
printf("sd\t", i) ¢
getch() ;
RUN 1
The armstrong numbers from 1 to 1000 are :
a 1 153 370 371 407
BHUVANESWARAN B/ AP (SS) / CSE / REC - 18Write a C program to find the product of two matrices using Array.
(or) Develop a ‘Cc’ program for matrix multiplication.
/* Program to multiply the given two matrices - MATMUL.C */
#include
f#linclude
void maint)
t
int mata[10] [10], matb[10] [10], matc[10] [10] +
int i, J, ky rowl, coll, row2, col2 i
elrser() ;
printf("Enter the order of first matrix i") 3
scanf("8d td", growl, &coll) ;
printf("\nEnter the order of second matrix : "
scant ("ad $d", @row2, &col2) ;
if(coll == row2)
{
printf("\nEnter the elements of first matrix : \n\n") +
for(i = 0 7 i < rowl ; i++)
for(j = 0; 3 < coll ; j+t]
scanf("8d", &matalil[i]}
printf("\nEnter the elements of s
for(i = 0; 1 < row2 ; i++)
nd matrix : \n\n") ¢
for(j = 0% 4 < col2 ; j++)
scanf("#d", amatb[1i][j]} ¢
for{i = 0; 1 < rowl 3 I++)
i
col2 ; j++]
mate(i](4] = 03
for{k = 0; k < coll ; k++)
mate(il(] = matelil[j] +
mata[i][k] * matb[k][j] ¢
}
printf("\nThe resultant matrix is : \n\n") ;
for(i = 07 i < rowl ; i++)
t
for({] = 0; } < col2 7 +4]
{
printf("’d \t", matelil(3]) ¢
print£("\n") ¢
t
else
printf ("\nMatrix Multiplication is not possible ...") 7
getch{) ;
BHUVANESWARAN B/ AP (SS) / CSE / REC - 19ourpur
RUN 1
Enter the
Enter the
Enter the
1 1
1 1
1 1
Enter the
1 1
1 1
1 1
order of first matrix
order of second matrix: 3 3
elements of first matrix :
elements of second matrix :
The resultant matrix is :
3a 3
3 3
3 3
RUN 2
Enter the
Enter the
wow
order of first matrix :3 3
order of second matrix
Matrix Multiplication is not possible ...
BHUVANESWARAN B/ AP (SS) / CSE / REC - 20Write a C program to perform addition, subtraction and
multiplication of two numbers using pointers and functions.
/* Arithmetic operations using pointers and functions-AOPTREN.C */
#include
#include <
onio.h>
int add{int *, int *);
int sub(int *, int *);
int mul(int *, int *);
void main{)
{
int first, second, sum, diff, pro
elrser();
printf ("Enter the first number : ");
scanf("8d", &first);
printf ("Enter the se
scanf("8d", &second);
sum = add(éfirst, &second);
diff = sub(sfirst, ssecond);
prod = mil (sfirst, &second);
printf£("\ntd + 8d = first, second, sum);
printf("\nsd - ad » first, second, diff);
printf("\ntd + &d first, second, prod);
getch()+
ond number : ")j
int add(int *x, int *y}
{
return *x + *
int sub(int *x, int *y)
{
return *x - *y:
F
int mul{int *x, int *y)
t
return *x * *y;
ourpur
RUN 1
Enter the first number : 10
Enter the second number : 5
BHUVANESWARAN B/ AP (SS) / CSE / REC - 21Write a C program to perform file copy and append operation on a
file.
/* Program to perform file copy and append - COPYAPP.C */
#include
#include
void maint)
t
char c 3
FILE *fptri, *fptr2 ;
elrser() +
printf ("Enter the text - FIRST.DAT : \n")
printf ("Press *Z or F6 at the end of the text\n");
fptrl = fopen("FIRST. DAT", "w"}
while({c = getchar(}} != EOF)
putc(c, fptrl) ;
felose(fptr1) ¢
fptrl = fopen{"FIRST.DAT","r") +
fptr2 = fopen("SECOND.DAT","w") 7
while({e = gete(fptr1)] != EOF)
pute(c, fptr2) ;
felose(fptrl) :
felose(fptr2) ;
print£("Content of file - SECOND.DAT : \n");
fptr2 = fopen{"SECOND.DAT","«") 7
while({c = getc(fptr2)) != EOF)
printf("se", ¢) ;
felose(fptr2) ;
printf ("Enter the text to append : \n") ;
printf ("Press “Z or F6 at the end of the text\n");
fptrl = fopen("FIRST.DAT","a") ¢
while({c = getchar()) != EOF)
pute(c, fptr1) ;
felose(fptrl] ;
printf ("Content of file - fIRST.DAT : \n");
fptrl = fopen{"FIRST.DAT","r")} +
while({c = getc(fptr1)] t= EOF}
print£("$o", c}
felose(fptrl) ¢
getch{) ;
t
BHUVANESWARAN B/ AP (SS) / CSE / REC - 22ourpur
RUN 1
Enter the text - FIRST.DAT :
Press “Z or F6 at the end of the text
Rajalakshmi Engineering College
Thandalam
Content of file - SECOND.DAT :
Rajalakshmi Engineering College
Thandalam
Enter the text to append :
Press “Z or F6 at the end of the text
Chennai - 602 105
Content of file - FIRST.DAT :
Rajalakehmi Engineering College
Thandalam
Chennai - 602 105
BHUVANESWARAN B/ AP (SS) / CSE / REC - 23Develop a ‘C’ program to implement a simple calculator using switch
case statement.
/* Performing arithmetic operations using switch...case-ARITH.C */
#include
#include
void main()
{
int nl, n2, ch;
elrser() +
printf ("Enter the first number : ")
scanf("8d", anl) ;
printfi"\nEnter the second number : ") ;
seanf("8d", en2)
printf£("\n[1] -> Addition ")
printf("\n[2] -> Subtraction ") ;
printf ("\n[3] -> Multiplication ") ;
printf£("\n[4] -> Division ")
printf("\n\nEnter your choice <1...4> 2");
scant ("@d", )
switch {ch}
{
case 1:
printf("\nd + $d = 8a", nl, n2, nl + n2
break ;
case 2:
printf("\nd - $d = 8d", nl, n2, nl - n2)
break ;
3:
print£("\ntd * $d = da", nl, n2, nl *
break ;
case 4:
printf("\ntd / #d = 8.21", nl, (float)yn1 / n2)
break ;
default
printf["\nInvalid choice");
break ¢
geteh():
BHUVANESWARAN B/ AP (SS) / CSE / REC - 24ourpur
Enter the first number
Enter the second number : 5
Addition
[2] -> Subtraction
(31 Multiplication
[4] -> Division
(1)
Enter your choice
10 * 5 = 50
BHUVANESWARAN B/ AP (SS) / CSE / REC - 25Develop a ‘Cc’ program to find the greatest of ‘N’ numbers stored in
an array.
/* Greatest of n numbers stored in an array - ARRGREAT.
#include
#include
void maint)
{
int a[10], n, i, g3
elrser();
printf ("Enter the number of elements :
scanf("8d",an)z
print£("\nEnter the elements
for(i = 07 i < nz i++)
scanf("8d", galills
g = al0];
forfi - 14 4 née
if(alil > 9)
g = ali);
att)
print£("\nThe greatest number is
getch(}7
ourPpur
RUN 1
Enter
the number of elements : 5
Enter the elements :
20
The greatest number is :
30 50 40
50
"8
An"):
BHUVANESWARAN B/ AP (SS) / CSE / REC - 26Develop a ‘C’ program to print the number of vowels in a given
paragraph.
/* Number of vowels in a given paragraph - VOWELS
#include
include
#include
void main()
{
char text[80], chy
int vowels=0, i;
elrser (lz
*f
printf ("Enter the paragraph : \n"};
gets(text) ¢
forli = 0% textfil "Nol; itt)
print ("Numb
qetch();
ourPpUT
RUN 1
Enter the paragraph
Anna University, Chennai
Number of vowels = 9
8a", vowels);
BHUVANESWARAN B/ AP (SS) / CSE / REC - 27Develop a ‘C’ program to find the transpose of a given matrix.
/* Program to transpose the given matrix - MATTRANS.C */
#include
#include
void main()
{
int mat [10] [10] ¢
int i, j, row, col ;
elrserl) ¢
printf ("Enter the order of the matrix : ")
scanf("ad 8d", grow, &col) 7
printf("\nEnter the elements of the matrix : \n\n") ;
for(i = 0; i < row; itt)
for(j = 0 7 3 < col + j++}
anf("$d", emat [i] (j])
printf ("\nThe transpose matrix is
for(j = 07 3 < col + j++
\n\n") ¢
forli = 0; i < row; itt)
printf("$d \t", mat(i](j]) +
printf£("\n")
geten() ¢
t
ourpur
RUN 1
Enter the order of the matrix
Enter the elements of the matrix :
2
4 5
ow
The transpose matrix is :
BHUVANESWARAN B/ AP (SS) / CSE / REC - 28Develop a ‘C’ program to add two matrices.
/* Program to add the given two matrices - MATADD.C */
#include
#include
void main()
{
int mata[10] [10], matb[10] [10], mate[10] [10] +
int i, j, row,
elrserl) ¢
printf ("Enter the order of the matrix : ") ;
scanf("ad 8d", grow, &col) 7
printfi"\nEnter the elements of first matrix : \n\n") +
for(i = 0; i < row; itt)
for(j = 0 7 3 < col + j++}
nf ("sd", gmatalil[j]) +
print£("\nEnter the elements of second matrix : \n\n") +
forli = 0; i < row; itt)
for(j = 0%} < col j++)
scanf("8d", ématbli][j]) +
7 i < row; itt)
0% 4 < cols J++)
mate[i][4] = matali] [J] + matb[il(3] +
print£("\nThe resultant matrix is : \n\n")
forli
for(j
or(L = 07 1 < row ; itt)
: for(j = 07 4 < cols i+)
' printf("$d \t", mateli](i]) ¢
printe("\n") 3
getch()
BHUVANESWARAN B/ AP (SS) / CSE / REC - 29ourpur
RUN 1
Enter the order of the matrix : 3
w
Enter the elements of first matrix :
1
5 a7
Bae
Enter the elements of s
2 4 6
8 10 12
14 16 18
The resultant matrix is :
3 1
15 19 23
27 31 35
BHUVANESWARAN B/ AP (SS) / CSE / REC - 30Define a structure called cricket that will describe the following
information:
Player Code
Player Name
Team Name
Batting Average
Using cricket, declare an array player with ‘N’ elements and
develop a ‘C’ program to read the information about all the ‘N’
players and print a team wise list containing names of players with
their batting average.
/* Cricket using structure ~ CRI
2 /
include
#include
struct cricket
{
char peode[10]z
char pname[20];
char tname[20]z
int avgs
} player[50], temp;
void main()
{
int i, 4,
clrser(};
printf ("Enter number of players : "1;
scanf("$d", en);
for(i = 0; 1 < nz i++)
{
printf("\nEnter player code : "
scanf("ss", player[1] .peode);
printf ("Enter player name : ");
scanf("ss", player[1].pname);
printf("Enter team name : ")i
scanf("$s", player[i].tname);
printf("Enter batting average : ");
scanf("$a", gplayer[i].avg)i
for(i = 1; i < nj it)
for(j = 07 j 0)
{
temp = player[i]+
player[j] = player[j + ll;
player[j + 1] = temp;
BHUVANESWARAN B/ AP (SS) / CSE / REC - 31printf ("\nTeam \t Player \t Average\n");
for(i = 0; 4
#include
void main{)
{
int i, J, n, count = 0;
clrser() 3
printf("Enter the no. of elements : ") +
seanf("8d", en) ¢
printf£("\nThe prime numbers are :\n\n") +
for (i = 1% count
#include
void main{)
{
char str[20], rev[20];
int i, j, length = 0, flag = 0;
elrser(};
printf ("Enter a string : ")3
scanf("8s", str):
for(i = 0; str[il NOE itt)
length++;
for(i = length - 1; i > iss)
rev[length - i - 1] = str[il
fori = 0; i < length; i++)
{
if(str[i] t= rev[i])
{
flag = 1;
break;
t
if(flag == 0)
printf("\nThe given string is a palindrome") ;
else
printf£("\nThe given string is not a palindrome") ;
getch() ;
ourPUT
RUN 1
Enter a string : madam
The given string is a palindrome
RUN 2
Enter a string : malayalam
The git
n string is a palindrome
RUN
Enter a string : bhuvan
The given string is not a palindrome
BHUVANESWARAN B/ AP (SS) / CSE / REC - 34Develop a ‘C’ program to reverse a given string.
/* Program to reverse the given string - STRREV.C */
#include
#include
void main()
{
char str(20], rev[20] +
int i, j, li
elrserl) ¢
printf ("Enter a string : ") ¢
scanf("8s", str) ;
for(1 = 0; str[1l
forli- 1-1, 4-0
rev[j] = str[il
rev[j] = '\0";
printf ("\nThe given string is : @s\n\n", str) ;
printf ("The reversed string is : $s", rev) ;
geteh() ;
"Nor p 1+4)
i >= 03 in-, i++)
ourpuT
RUN 1
Enter a string : bhuvan
The given string is : bhuvan
The reversed string is : navuhb
BHUVANESWARAN B/ AP (SS) / CSE / REC - 35Develop a ‘Cc’ function that will scan a character string passed as
an argument and convert all lower-case characters to their upper-
case equivalents.
/* Lower case to upper case using function ~ LCUCEUNC.C +*/
flinclude
void upper(char[]}s
void main()
{
str [20];
Og
printfi"Enter the string
seanf("%s", str}s
printf("The upper case equivalent is : "Is
upper (str);
getch(s
M
void upper(char str{]
int if
for(i = 0 ; str[i] ; i++)
if(str[i] > 96 @& str[i] < 123)
str[i] = str[i] - 32;
puts(str};
ourpur
RUN 1
Enter the string : University
The upper case equivalent is : UNIVERSITY
BHUVANESWARAN B/ AP (SS) / CSE / REC - 36Develop a ‘C’ program to sort ‘N’ names alphabetically.
/* To sort the given strings in alphabetical order - STRSORT.C */
#include
#include
f#include
void maint)
t
char atr[10] [20], temp[20] +
int n, i, ji
elrser() ;
printf£("Enter the number
scanf("8d", an) ;
printf ("\nEnter the strings : \n\n") ¢
for(i = 0; i 0)
{
f strings : ") ¢
strepy(temp, str[jl} +
strepy(stc[}], str[j + 1]) 7
strepy(str[} + 1], temp) ;
printf("\nThe sorted order of strings are : \n\n™) ;
for(i = 0; 1
#include
void main{)
{
int n, i, count =
clrser(\i
printf ("Enter the no. of elements : "};
scanf("8d", 6);
print£("\nThe numbers divisible by 5 are :\n\n");
for(i = 1; count
f#linclude
long fact(int n};
void main{)
[
long n, res ;
elrsert) +
printf ("Enter a number : ") ¢
scanf("@ld", &n) ¢
res = fact(n) ¢
printf ("\nFactorial value of $ld is : $1ld", n, res)
geteh() +
fact (int nj
long i, f= 13
for(i = 1; i <=n; itt)
fo 6" 4
return f;
ourpur
RUN 1:
Enter a number
Factorial value of 5 is : 120
BHUVANESWARAN B/ AP (SS) / CSE / REC - 39Develop a ‘C’ program that will create a data file containing the
list of telephone numbers in the following table:
Name Telephone No
John 23456
Peter 9876
Use sequential access file. Perform search operation.
/* Sequential file acc
s and search op
#include
#include
void main()
{
FILE *fptr ;
inti, nz
char name[10] ¢
long phoneno, search;
elrser{} +
fptr = fopen("TELDIR.DAT", "w") 7
printf("Enter the number of persons : ") ;
scanf("’d", an) ;
forfi= 1; i
#include
void maint)
{
intn, r, 8-07
elrserl) ¢
printf ("Enter a number :") ¢
scanf("8d", @n) ¢
while{n > 0}
{
a
+
n=n/
printf("\nThe sum of the digits is : 8d", 5) ;
getch(} ¢
OUTPUT
RUN 1
Enter a number : 12345
The sum of the digits is : 15
BHUVANESWARAN B/ AP (SS) / CSE / REC - 44