0% found this document useful (0 votes)
46 views79 pages

C Programming Lab Manual

The document provides code examples for various numeric conversions and calculations in C programming, including: 1) Finding the maximum and minimum of an array of numbers 2) Calculating the roots of a quadratic equation 3) Computing sine and cosine values using series expansions 4) Converting between binary, decimal, and octal number systems The code samples demonstrate input/output statements, loops, conditional statements, and mathematical functions in C.

Uploaded by

satyam sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views79 pages

C Programming Lab Manual

The document provides code examples for various numeric conversions and calculations in C programming, including: 1) Finding the maximum and minimum of an array of numbers 2) Calculating the roots of a quadratic equation 3) Computing sine and cosine values using series expansions 4) Converting between binary, decimal, and octal number systems The code samples demonstrate input/output statements, loops, conditional statements, and mathematical functions in C.

Uploaded by

satyam sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Programming Lab Manual

1. Finding the maximum and minimum of given set of numbers.

Aim:

To find the maximum and minimum of given set of numbers. For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

int a[50], i, n, max, min;

clrscr();

printf(Enter size of the array: \n);

scanf(%d, &n);

printf(Enter elements in the array: \n);

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

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

max=a[0];
1
Page

max=a[0];

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

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

if(a[i] > max)

max=a[i];

if(a[i] < min)

min=a[i];

printf(Maximum element = %d \n, max);

printf(Minimum element = %d \n, min);

getch();

Output:

Enter size of the array:

Enter elements in the array:

78 45 67 23 14 49

Maximum element = 78

Minimum element = 14
2
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

2. Finding the roots of a given Quadratic equation.

Aim:
To find the roots of a given quadratic equation. For input & output statements we include
the header file <stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a, b, c, d, r1, r2, rp, ip;
clrscr();
printf("Enter a b c values: \n");
scanf("%f%f%f", &a, &b, &c);
if(a = = 0)
printf(" not a QE");
else
{
d = b*b-4*a*c;
if(d >= 0)
{
printf("Roots are real \n");
r1 = (-b + sqrt(d)) / (2*a);
r2 = (-b - sqrt(d)) / (2*a);
printf("Roots are %f and %f \n", r1, r2);
}
else
3

{
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

printf("Roots are imaginary \n");


rp = -b / (2*a);
ip = sqrt(-d) / (2*a);
printf("Roots are (%f , %f) and (%f , %f) \n", rp, ip, rp, -ip);
}
}
getch();
}
Output:
Enter a b c values:
3 5 2
Roots are real
Roots are -0.666667 and -1.000000
Output:
Enter a b c values:
4 2 8
Roots are imaginary
Roots are (-0.250000, 1.391941) and (-0.250000, -1.391941)

4
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Sin x and Cos x values using series expansion.

3. Sin x value using series expansion.

Aim:
To find the value of sin x using series expansion. For input & output statements we
include the header file <stdio.h>, for arithmetic operations include the <math.h> and for clear
the screen include the <conio.h>.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x, sum, term;
int i, n;
clrscr();
printf("Enter the no of terms: \n");
scanf("%d", &n);
printf("Enter the angle in degrees x: \n");
scanf("%f", &x);
x = (x*3.14) / 180;
sum = x;
term = x;
for(i = 1;i <= n;i++)
{
term = (term*(-1)*x*x) / ((2*i)*(2*i+1));
sum+ = term;
}
5
Page

printf("Sin valve of given angle is %f", sum);


Department of CSE

Methodist College of Engineering


C Programming Lab Manual

getch();
}
Output:
Enter the no of terms:
3
Enter the angle in degrees x:
30
Sin valve of given angle is 0.499770

6
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

4. Cos x value using series expansion.

Aim:
To find the value of cos x using series expansion. For input & output statements we
include the header file <stdio.h>, for arithmetic operations include the <math.h> and for clear
the screen include the <conio.h>.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x, sum, term;
int i, n;
clrscr();
printf("Enter the no of terms: \n");
scanf("%d", &n);
printf("Enter the angle in degrees x: \n");
scanf("%f", &x);
x = (x*3.14) / 180;
sum = 1;
term = 1;
for(i = 1;i <= n;i++)
{
term = (term*(-1)*x*x) / ((2*i)*(2*i-1));
sum+ = term;
}
printf("Cos valve of given angle is %f", sum);
7

getch();
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

}
Output:
Enter the no of terms:
3
Enter the angle in degrees x:
30
Cos valve of given angle is 0.866158

8
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Conversion of Binary to Decimal, Octal, Hexa and Vice Versa.

5. Conversion of Binary to Decimal.

Aim:

To convert binary to decimal. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>

#include<conio.h>

void main()

long int binary, decimal = 0, j = 1, rem;

clrscr();

printf("Enter any number any binary number: \n");

scanf("%ld", &binary);

while(binary != 0)

rem = binary % 10;

decimal = decimal+rem*j;

j = j*2;

binary = binary / 10;


9
Page

}
Department of CSE

Methodist College of Engineering


C Programming Lab Manual

printf("Equivalent decimal value: %ld", decimal);

getch();

Output:
Enter any number any binary number:

1101

Equivalent decimal value: 13

10
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

6. Conversion of Decimal to Binary.

Aim:

To convert decimal to binary. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>

#include<conio.h>

void main()

long int decimal, rem, q;

int binary[100], i = 1, j;

clrscr();

printf("Enter any decimal number: \n");

scanf("%ld", &decimal);

q = decimal;

while(q != 0)

binary[i++] = q % 2;

q = q / 2;
11

}
Page

printf("Equivalent binary value of decimal number %d: ", decimal);


Department of CSE

Methodist College of Engineering


C Programming Lab Manual

for(j = i -1 ;j > 0;j--)

printf("%d", binary[j]);

getch();

Output:
Enter any decimal number:

50

Equivalent binary value of decimal number 50: 110010

12
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

7. Conversion of Binary to Octal.

Aim:

To convert binary to octal. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>

#include<conio.h>

void main()

long int binary, octal = 0, j = 1, rem;

clrscr();

printf("Enter any number any binary number: \n");

scanf("%ld", &binary);

while(binary != 0)

rem = binary % 10;

octal = octal+rem*j;

j = j*2;

binary = binary / 10;


13

}
Page

printf("Equivalent octal value: %lo", octal);


Department of CSE

Methodist College of Engineering


C Programming Lab Manual

getch();

Output:
Enter any number any binary number:

1101

Equivalent octal value: 15

14
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

8. Conversion of Octal to Binary.

Aim:

To convert octal to binary. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>

#include<conio.h>

#define MAX 1000

void main()

char octal[MAX];

long int i = 0;

clrscr();

printf("Enter any octal number: \n");

scanf("%s", octal);

printf("Equivalent binary value: \n");

while(octal[i])

switch(octal[i])
15

{
Page

case '0': printf("000");


Department of CSE

Methodist College of Engineering


C Programming Lab Manual

break;

case '1': printf("001");

break;

case '2': printf("010");

break;

case '3': printf("011");

break;

case '4': printf("100");

break;

case '5': printf("101");

break;

case '6': printf("110");

break;

case '7': printf("111");

break;

default: printf("Invalid octal digit %c ", octal[i]);

i++;
16

}
Page

getch();
Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Output:
Enter any octal number:

123

Equivalent binary value:

001010011

17
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

9. Conversion of Binary to Hexadecimal.

Aim:

To convert binary to hexadecimal. For input & output statements we include the header
file <stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>

#include<conio.h>

void main()

long int binary, hexadecimal = 0, j = 1, rem;

clrscr();

printf("Enter any number any binary number: \n");

scanf("%ld", &binary);

while(binary != 0)

rem = binary % 10;

hexadecimal = hexadecimal+rem*j;

j = j*2;

binary = binary / 10;


18

}
Page

printf("Equivalent hexadecimal value: %lX", hexadecimal);


Department of CSE

Methodist College of Engineering


C Programming Lab Manual

getch();

Output:
Enter any number any binary number:

1101

Equivalent hexadecimal value: D

19
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

10. Conversion of Hexadecimal to Binary.

Aim:

To convert hexadecimal to binary. For input & output statements we include the header
file <stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>

#include<conio.h>

#define MAX 1000

void main()

char binary[MAX], hexadecimal[MAX];

long int i = 0;

clrscr();

printf("Enter any hexadecimal number: \n");

scanf("%s", hexadecimal);

printf("Equivalent binary value: \n");

while(hexadecimal[i])

switch(hexadecimal[i])
20

{
Page

case '0': printf("0000");


Department of CSE

Methodist College of Engineering


C Programming Lab Manual

break;

case '1': printf("0001");

break;

case '2': printf("0010");

break;

case '3': printf("0011");

break;

case '4': printf("0100");

break;

case '5': printf("0101");

break;

case '6': printf("0110");

break;

case '7': printf("0111");

break;

case '8': printf("1000");

break;

case '9': printf("1001");


21

break;
Page

case 'A': printf("1010");


Department of CSE

Methodist College of Engineering


C Programming Lab Manual

break;

case 'B': printf("1011");

break;

case 'C': printf("1100");

break;

case 'D': printf("1101");

break;

case 'E': printf("1110");

break;

case 'F': printf("1111");

break;

case 'a': printf("1010");

break;

case 'b': printf("1011");

break;

case 'c': printf("1100");

break;

case 'd': printf("1101");


22

break;
Page

case 'e': printf("1110");


Department of CSE

Methodist College of Engineering


C Programming Lab Manual

break;

case 'f': printf("1111");

break;

default: printf("Invalid hexadecimal digit %c ", hexadecimal[i]);

i++;

getch();

Output:
Enter any hexadecimal number:

2AD5

Equivalent binary value:

0010101011010101

23
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Generating a Pascal Triangle and Pyramid of Numbers.

11. Generating a Pascal Triangle.

Aim:

To print pascal triangle. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>

#include<conio.h>

long fact(int);

void main()

int n, i, j;

clrscr();

printf("Enter the no. of lines: \n");

scanf("%d", &n);

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

for(j = 0;j < n-i-1;j++)

printf(" ");
24

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


Page

printf("%ld ", fact(i) / (fact(j)*fact(i-j)));


Department of CSE

Methodist College of Engineering


C Programming Lab Manual

printf("\n");

getch();

long fact(int num)

long f = 1;

int i = 1;

while(i <= num)

f = f*i;

i++;

return f;

25
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Output:
Enter the no. of lines:

1
11
121
1331
14641
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1

26
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

12. Generating a Pyramid of Numbers (Floyds Triangle).

Aim:

To print Pyramid of Numbers (Floyds Triangle). For input & output statements we
include the header file <stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, r, k = 1;
clrscr();
printf("Enter the range: \n");
scanf("%d", &r);
printf("FLOYD'S TRIANGLE: \n \n");
for(i = 1;i <= r;i++)
{
for(j = 1;j <= i;j++,k++)
printf(" %d", k);
printf("\n");
}
getch();
}

27
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Output:
Enter the range:
10
FLOYD'S TRIANGLE

1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 7 48 49 50 51 52 53 54 55

28
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Recursion: Factorial, Fibonacci, GCD.

13. Recursion: Factorial.

Aim:

To find factorial of a given number using recursion. For input & output statements we
include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:
#include<stdio.h>

#include<conio.h>

int fact(int);

void main()

int num,f;

clrscr();

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

scanf("%d", &num);

f = fact(num);

printf("Factorial of %d is: %d", num, f);

getch();

}
29

int fact(int n)
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

if(n = = 1)

return 1;

else

return(n*fact(n-1));

Output:
Enter a number:

Factorial of 5 is: 120

30
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

14. Recursion: Fibonacci.

Aim:

To print fibonacci series using recursion. For input & output statements we include the
header file <stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>

#include<conio.h>

void fibonacci(int);

void main()

int k, n;

long int i = 0, j = 1, f;

clrscr();

printf("Enter the range of the Fibonacci series: \n");

scanf("%d", &n);

printf("Fibonacci Series: \n");

printf("%d %d ", 0, 1);

fibonacci(n);

getch();
31

}
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

void fibonacci(int n)

static long int first = 0, second = 1, sum;

if(n > 0)

sum = first + second;

first = second;

second = sum;

printf("%ld ", sum);

fibonacci(n-1);

Output:
Enter the range of the Fibonacci series:

10

Fibonacci Series:

0 1 1 2 3 5 8 13 21 34 55 89 32
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

15. Recursion: GCD.

Aim:

To find gcd of a number using recursion. For input & output statements we include the
header file <stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>
#include<conio.h>
int findgcd(int, int)
void main()
{
int n1, n2, gcd;
clrscr();
printf("Enter two numbers: \n");
scanf("%d %d", &n1, &n2);
gcd = findgcd(n1, n2);
printf("GCD of %d and %d is: %d \n", n1, n2, gcd);
getch();
}
int findgcd(int x, int y)
{
while(x != y)
{
if(x > y)
return findgcd(x-y, y);
else
return findgcd(x, y-x);
33

}
Page

return x;
Department of CSE

Methodist College of Engineering


C Programming Lab Manual

}
Output:
Enter two numbers:

366

60

GCD of 366 and 60 is:

34
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Matrix Addition and Multiplication using Arrays.

16. Matrix Addition using Arrays.

Aim:

To perform addition of two matrices. For input & output statements we include the
header file <stdio.h> and for clear the screen include the <conio.h>.
Program:

#include<stdio.h>
#include<conio.h>
void main()

int a[5][5], b[5][5], c[5][5], i, j, n, m, p, q;

clrscr();

printf("Enter first matrix size: \n");

scanf("%d%d", &m, &n);

printf("Enter second matrix size: \n");

scanf("%d%d", &p, &q);

if(m != p || n != q)

printf("Size mismatch, Addition is not possible \n");

else

{
35
Page

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

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

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

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

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

printf("Enter second matrix elements: \n");

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

for(j = 0;j < q;j++)

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

printf("Addition of two Matrices is \n");

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

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

c[i][j] = a[i][j] + b[i][j];

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

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

printf("%4d", c[i][j]);

printf("\n");
36

}
Page

}
Department of CSE

Methodist College of Engineering


C Programming Lab Manual

getch();

Output:

Enter first matrix size:

Enter second matrix size:

Enter first matrix elements:

Enter second matrix elements:

6
37

5
Page

4
Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Addition of two Matrices is

7 7 7

7 7 7

Output:

Enter first matrix size:

Enter second matrix size:

Size mismatch, Addition is not possible

38
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

17. Matrix Multiplication using Arrays.


Aim:

To multiply two matrices using arrays. For input & output statements we include the
header file <stdio.h> and for clear the screen include the <conio.h>.
Program:
#include<stdio.h>
#include<conio.h>
void main()

int a[5][5], b[5][5], c[5][5], i, j, n, m, p, q;

clrscr();

printf("Enter first matrix size: \n");

scanf("%d%d", &m, &n);

printf("Enter second matrix size: \n");

scanf("%d%d", &p, &q);

if(n != p)

printf("Not compatible, Multiplication is not possible \n");

else

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


39

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


Page

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


Department of CSE

Methodist College of Engineering


C Programming Lab Manual

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

printf("Enter second matrix elements: \n");

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

for(j = 0;j < q;j++)

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

printf("Product of two Matrices is \n");

for(I = 0;I < m;i++)

for(j = 0;j < q;j++)

c[i][j] = 0;

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

c[i][j] += a[i][k] * b[k][j];

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

for(j = 0;j < q;j++)


40

printf("%4d", c[i][j]);
Page

printf("\n");
Department of CSE

Methodist College of Engineering


C Programming Lab Manual

getch();

Output:

Enter first matrix size:

Enter second matrix size:

Not compatible, Multiplication is not possible

Output:

Enter first matrix size:

Enter second matrix size:

3
41

1
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Enter first matrix elements:

Enter second matrix elements:

2
3

Product of two Matrices is


14

32

42
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Bubble Sort, Selection Sort.

18. Bubble Sort.

Aim:
To implement bubble sort. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

int a[20], i, j, n, temp;

clrscr();

printf("Enter array size: \n");

scanf("%d", &n);

printf("Enter any %d elements: \n", n);

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

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

for(i = 1;i <= n-1;i++)

{
43

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


Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

if(a[j] > a[j+1])

temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

printf("After sorting, the element are \n");

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

printf("%4d", a[i]);

getch();

Output:

Enter array size:

Enter any 6 elements:


44

78 95 62 30 12 39
Page

After sorting, the element are


Department of CSE

Methodist College of Engineering


C Programming Lab Manual

12 30 39 62 78 95

45
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

19. Selection Sort.

Aim:

To implement selection sort. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

int a[20], i, j, n, temp, maxloc, k;

clrscr();

printf("Enter array size: \n");

scanf("%d", &n);

printf("Enter any %d elements: \n", n);

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

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

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

maxloc = 0;
46
Page

for(j = 0;j <= (n-1-i);j++)

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

{
if(a[j] > a[maxloc])

maxloc = j;

temp = a[n-1-i];

a[n-1-i] = a[maxloc];

a[maxloc] = temp;

printf("After sorting, the element are \n");

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

printf("%4d", a[i]);

getch();

Output:

Enter array size:

Enter any 6 elements:

78 95 62 30 12 39

After sorting, the element are


47
Page

12 30 39 62 78 95

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Program on Linear Search and Binary Search using Recursive and Non Recursive
Procedures.
20. Program on Linear Search using Recursive Procedure.
Aim:

To implement linear search using recursive procedure. For input & output statements
we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

int linear(int[], int, int);

void main()

int a[100], n, i, x;

clrscr();

printf("Enter Size: \n");

scanf("%d", &n);

printf("Enter %d elements: \n", n);

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

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

printf("Enter element to search: \n");


48
Page

scanf("%d", &x);

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

i=linear(a, n-1, x);

if(i != -1)

printf("The element %d found at %d location", x, i);

else

printf("The element %d is not found", x);

getch();

int linear(int a[100], int n, int x)

if(n<= 0)

return -1;

if(a[n] = = x)

return n;

else

return linear(a, n-1, x);

Output:

Enter Size:
49

5
Page

Enter 5 elements:
Department of CSE

Methodist College of Engineering


C Programming Lab Manual

25 30 12 54 60

Enter element to search:

60

The element 60 found at 4 location

50
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

21. Program on Linear Search using Non - Recursive Procedure.

Aim:

To implement linear search using non recursive procedure. For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

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

void main()

int a[20], i, n, x, xloc = -1;

clrscr();

printf("Enter array size: \n");

scanf("%d", &n);

printf("Enter any %d elements: \n", n);

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

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

printf("Enter the element you want to search for \n");

scanf("%d", &x);

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


51

{
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

if(a[i] = = x)

xloc = i;

break;

if(xloc = = -1)

printf("%d is not found in the array \n", x);

else

printf("%d is found at location %d \n", x, xloc);

getch();

Output:

Enter array size

Enter any 6 elements

78 45 67 23 14 49

Enter the element you want to search for


52

23
Page

23 is found at location 3
Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Output:

Enter array size

Enter any 5 elements

1 2 34 5 6

Enter the element you want to search for

89

89 is not found in the array

53
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

22. Program on Binary Search using Recursive Procedure.


Aim:

To implement binary search using recursive procedure. For input & output statements
we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#define size 10

int binary(int[], int, int, int);

void main()

int n, i, key, position;

int low, high, list[size];

clrscr();

printf("\nEnter the total number of elements: \n");

scanf("%d", &n);

printf("Enter the elements of list: \n");

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


54

{
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

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

low = 0;

high = num - 1;

printf("Enter element to be searched: \n");

scanf("%d", &key);

position = binary(list, key, low, high);

if(position != -1)

printf("Number present at %d \n", position);

else

printf("The number is not present in the list \n");

getch();

int binary(int a[], int x, int low, int high)

int mid;
55

if (low > high)


Page

return -1;
Department of CSE

Methodist College of Engineering


C Programming Lab Manual

mid = (low + high) / 2;

if (x = = a[mid])

return mid;

else if (x < a[mid])

binary(a, x, low, mid - 1);

else

binary(a, x, mid + 1, high);

Output:

Enter the total number of elements:

Enter the elements of list:


56

11 22 33 44 55
Page

Enter element to be searched:


Department of CSE

Methodist College of Engineering


C Programming Lab Manual

33

Number present at 2

57
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

23. Program on Binary Search using Non - Recursive Procedure.


Aim:

To implement binary search using non recursive procedure. For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

int a[20], i, n, x, xloc = -1, low, high, mid;

clrscr();

printf("Enter array size: \n");

scanf("%d", &n);

printf("Enter any %d elements in the sorted order: \n",n);

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

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

printf("Enter the element you want to search for \n");

scanf("%d", &x);

low = 0;
58

high = n-1;
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

while(low <= high)

{
mid = (low+high) / 2;

if(a[mid] = = x)

xloc = mid;

break;

else if(x < a[mid])

high = mid - 1;

else

low = mid + 1;

if(xloc = = -1)

printf("%d is not found in the array \n", x);

else

printf("%d is found at location %d \n", x, xloc);

getch();

}
59
Page

Output

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Enter array size:

Enter any 6 elements:

78 45 67 23 14 49

Enter the element you want to search for

23

23 is found at location 3

Output:

Enter array size:

Enter any 5 elements:

1 2 34 5 6

Enter the element you want to search for

89

89 is not found in the array

60
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Functions (Library Functions) of String Manipulations.

24. Find the Length of a given String using Library Function.

Aim:

To find the length of a given string using library function. For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

char arr[ ] = "Methodist College" ;


int len1, len2 ;

clrscr();

len1 = strlen (arr) ;


len2 = strlen ("Engineerimg") ;

printf ("String = %s length = %d \n", arr, len1) ;


printf (String = %s length = %d \n", "Engineerimg", len2) ;

getch();

Output:
61
Page

string = Methodist College length = 17

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

string = Engineerimg length = 11

62
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

25. Copy the given String to another String using Library Function.

Aim:

To copy the given string to another string using library function. For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

char source[ ] = "Methodist College of Engineering & Technology" ;

char target[20] ;

cltscr();

strcpy(target, source) ;
printf("Source String = %s \n", source ) ;
printf("Target String = %s \n", target ) ;

getch();

Output:

Source String = Methodist College of Engineering & Technology


63

Target String = Methodist College of Engineering & Technology


Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

26. Appendding one String at the end of another String using Library Function.

Aim:

To appends one string at the end of another string using library function. For input &
output statements we include the header file <stdio.h> and for clear the screen include the
<conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

char source[ ] = "Methodist College";

char target[30] = Welcome to;

cltscr();

strcat ( target, source) ;


printf("Source String = %s \n", source);
printf("Target String = %2s \n", target);

getch();

Output:

Source String = Methodist College


64
Page

Target String = Welcome to Methodist College

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

27. Reverse the Contents of the given String using Library Function.

Aim:

To reverse the contents of the given string using library function. For input & output
statements we include the header file <stdio.h>, for clear the screen include the <conio.h> and
for string functions include <string.h>.

Program:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char a[20];

cltscr();

printf(Enter any string: \n);

gets(a);

strrev(a);

printf(Reverse string: \n, a);

getch();

}
65

Output:
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Enter any string: methodist

Reverse string: tsidohtem

66
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

28. Compare the Contents of given two Strings using Library Function.

Aim:

To reverse the contents of the given two strings using library function. For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

char string1[ ] = "Methodist";

char string2[ ] = MCET;

int i, j, k;

cltscr();

i = strcmp(string1, Methodist);

j = strcmp(string1, string2);

k = strcmp(string1, Methodist College);

printf("i = %d \n", i);


printf("j = %d \n", j);
printf("k = %d \n", k);
getch();
67
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Output:

i=0

j = 34

k = -32

68
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

29. Abbreviate the Contents of given Strings using Library Function.

Aim:

To abbreviate the contents of the given string using library function. For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

char s[100];

int i;

clrscr();

printf("Enter a String: \n");

gets(s);

i = 0;

while(s[i] = = ' ')

i++;

putchar(s[i]);

putchar(' ');
69
Page

while(s[i] != '\0')

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

if(s[i] = = ' ' && s[i+1] != ' ')

putchar(s[i+1]);

putchar(' ');

i++;

getch();

Output:

Enter a String:

Methodist College Engineering Technology

MCET

70
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

30. Finding the No. of Characters, Words and Lines of given Text File.

Aim:

To find the no. of characters, words and lines of given text file. For input & output statements
we include the header file <stdio.h> and for clear the screen include the <conio.h>.

Program:

#include<stdio.h>

#include<conio.h>

void main()

int noc = 0, now = 0, nol = 0;

FILE *fw,*fr;

char fname[20], ch;

clrscr();

printf("Enter the source file name: \n");

gets(fname);

fr = fopen(fname, "r");

if(fr = = NULL)

printf("\n error \n");


71
Page

exit(0);

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

ch = fgetc(fr);

while(ch != EOF)

noc++;

if(ch = = ' ');

now++;

if(ch = = '\n')

nol++;

now++;

ch = fgetc(fr);

fclose(fr);

printf("Total no. of character = %d \n", noc);

printf("Total no of words = %d \n", now);

printf("Total no of lines = %d \n", nol);


72

getch();
Page

}
Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Output:

Enter the source file name:

namecount.c

Total no. of character = 488

Total no of words = 522

Total no of lines = 34

73
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

File Handling Programs

31. Program to create a file.


Aim:

To create a file. For input & output statements we include the header file <stdio.h> and for
clear the screen include the <conio.h>.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp = fopen("[Link]", "w");
printf("Enter text, to stop press Ctrl Z: \n");
while((ch = getchar()) != EOF)
{
fputc(ch,fp);
}
fclose(fp);
getch();
}
Output:
Enter text, to stop press Ctrl-Z:
This is my first program in C
language to create a file.
74

Bye.
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

To see the contents of this file


open [Link] in any
editor program.
^Z

File Saved on disk

[Link]:
This is my first program in C
language to create a file.
Bye.
To see the contents of this file
open [Link] in any
editor program.

75
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

32. Program to print the contents of the file on the monitor.


Aim:

To print the contents of the file on the monitor. For input & output statements we include the
header file <stdio.h> and for clear the screen include the <conio.h>.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp = fopen("[Link]", "r");
if(fp==NULL)
printf("No such file \n");
else
{
printf("File contents are: \n");
while(!feof(fp))
{
ch = fgetc(fp);
putchar(ch);
}
fclose(fp);
}
getch();
76

}
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

Output:
Given [Link]
File contents are:
This is my first program
in C to create a file with some text.
Bye
To see the contents of the file open [Link]
in any editor.
Output:
Given [Link]
No such file

77
Page

Department of CSE

Methodist College of Engineering


C Programming Lab Manual

33. Program to Copy one File in to another.


Aim:

To copy one file in to another. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fs, *ft ;
char ch;
clrscr();
fs = fopen ("[Link]", "r");
if( fs == NULL )
{
puts ("Cannot open source file");
}
ft = fopen ("[Link]", "w");
if(ft == NULL)
{
puts("Cannot open target file");
fclose(fs);
}
while (1)
{
ch = fgetc (fs);
78

if(ch = = EOF)
Page

break;
Department of CSE

Methodist College of Engineering


C Programming Lab Manual

else
{
fputc (ch, ft);
puts("File copied");
}
}
fclose (fs);
fclose (ft);
getch();
}
Output:
File copied

79
Page

Department of CSE

Methodist College of Engineering

You might also like