Documente Academic
Documente Profesional
Documente Cultură
Aim:
To print any given message like C test program. For input & output statements we include
the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 2: print message like C test program
Step 3: stop
1
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("C test program");
getch();
}
Output:
C test program
2
Page
Department of CSE
3
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float x;
int y;
char a;
double k;
clrscr();
x = 123.456;
y = 20;
a = 'z';
k = 9.3456677;
printf("x = %f \n", x);
printf("y = %d \n", y);
printf("a = %c \n", a);
printf("k = %lf \n", k);
getch();
}
Output:
x = 123.456001
y = 20
a=z
k = 9.345668
4
Page
Department of CSE
To write a program to swap two given integer values. For input & output statements we
include the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 4: set a = b
Step 7: stop
5
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, temp;
clrscr();
printf("Enter the values of a, b: \n");
scanf("%d%d", &a, &b);
temp = a;
a = b;
b = temp;
printf("a = %d \n b = %d \n", a, b);
getch();
}
Output:
Enter the values of a, b
5
3
a=3
b=5
Exercise:
1. Swap two integer numbers without using temporary variable.
6
Page
Department of CSE
Aim:
To write a program to evaluate simple interest. For input & output statements we include
the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read the values for p, n, r
Step 3: substitute the values in the expression (p * n * r) / 100
Step 4: evaluate and store the result in SI
Step 5: display the value of SI
Step 6: stop
7
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float p, n, r;
float simple = 0.0;
clrscr();
printf("Enter the values of p n r: \n");
scanf("%f%f%f", &p, &n, &r);
simple = (p*n* r) / 100.0;
printf("Simple Interest is %f \n", simple);
getch();
}
Output:
Enter the values of p n r
1000
2
20
Simple Interest is 400.000000
8
Page
Department of CSE
To calculate the area and perimeter of a circle. For input & output statements we include
the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read value for radius
Step 7: stop
9
Page
Department of CSE
Program
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%f", &r);
area = 3.14*r*r;
peri = 2*3.14*r;
getch();
Output:
Enter any radius:
Area is 78.500000
10
Page
Perimeter is 31.400000
Department of CSE
Exercise:
11
Page
Department of CSE
Aim:
To Find the maximum of three given integers by using conditional operator. For input &
output statements we include the header file <stdio.h> and for clear the screen include the
<conio.h>.
Algorithm:
Step 1: start
Step 2: read a, b, c
Step 5: stop
12
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
int a, b, c, max;
clrscr();
max = a>b?(a>c?a:c):(b>c)?b:c;
getch();
Output:
24 76 48
Max is 76
Exercise:
Department of CSE
Aim:
To check whether the given integer number is even or odd. For input & output statements
we include the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 2: read n
Step 4: stop
14
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter a number: \n");
scanf("%d", &n);
if(n%2 = = 0)
printf("The number %d is even", n);
else
printf("The number %d is odd", n);
getch();
}
Output:
Enter a number:
23
The number 23 is odd
Output:
Enter a number:
34
The number 34 is even
15
Page
Department of CSE
Aim:
To check whether the given year is leap year or not. For input & output statements we
include the header file <stdio.h> and for clear the screen include the <conio.h>.
Algorithm:
Step 1: start
Step 3: if (year % 4 = 0 and year % 100 = 0 or year % 400 = 0) then write "Leap Year" else write
"not leap year"
Step 4: stop
16
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter the year: \n");
scanf("%d", &year);
if(((year % 4) = = 0) && ((year % 100) != 0) || ((year % 400) = = 0))
printf("The year %d is leap year", year);
else
printf("The year %d is not a leap year", year);
getch();
}
Output:
Enter the year:
1884
The year 1884 is leap year.
17
Page
Department of CSE
Step 5: if(m1 < 40 or m2 < 40 or m3 < 40) then set grade = F else goto step 6
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int m1, m2, m3, tot;
float avg;
char gr;
clrscr();
printf("Enter the marks of 3 subjects: \n");
scanf("%d%d%d", &m1, &m2, &m3);
tot = m1+m2+m3;
avg = (float)(m1+m2+m3) / 3;
if(m1 < 40 || m2 < 40 || m3 < 40)
gr = 'F';
else if(avg < 50)
gr = 'E';
else if(avg < 60)
gr = 'D';
else if(avg < 70)
gr = 'C';
else if(avg < 80)
gr = 'B';
else
gr = 'A';
printf("Total = %d \n", tot);
19
Department of CSE
getch();
}
Output:
Enter the marks of 3 subjects:
89 76 57
Total = 222
Average = 74.000000
Grade = B
20
Page
Department of CSE
Step 2: read m
Step 13: if(m = = 10) then write "October" else goto step 14
Step 14: if(m = = 11) then write "November" else goto step 15
Step 15: if(m = = 12) then write "December" else goto step 16
21
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int m;
clrscr();
printf("Enter any month number: \n");
scanf("%d", &m);
switch(m)
{
case 1: printf("January \n");
break;
case 2: printf("February \n");
break;
case 3: printf("March \n");
break;
case 4: printf("April \n");
break;
case 5: printf("May \n");
break;
case 6: printf("June \n");
break;
case 7: printf("July \n");
break;
case 8: printf("August \n");
22
break;
case 9: printf("September \n");
Page
Department of CSE
break;
case 10: printf("October \n");
break;
Department of CSE
11. Finding the sum of natural numbers up to given N using while loop.
Aim:
To write a program to sum of natural numbers up to given N using while. For input &
output statements we include the header file <stdio.h> and for clear the screen include the
<conio.h>.
Algorithm:
Step 1: start
Step 2: read value for n, i
Step 3: if(i < = n) then goto step 4
Step 4: evaluate & set sum = sum + i goto step 5
Step 5: evaluate & set i = i + i goto step 3
Step 4: display value of sum
Step 5: stop
24
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, sum = 0;
clrscr();
printf("Enter the value of n: \n");
scanf("%d", &n);
i = 1;
while(i <= n)
{
sum = sum + i;
i++;
}
printf("Sum is %d \n", sum);
getch();
}
Output:
Enter the value of n:
150
Sum is 11325
Exercise:
1. Finding the sum of natural numbers up to given N using for loop.
25
Page
Department of CSE
Step 1: start
26
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
long fact = 1;
clrscr();
printf(Enter n integer: \n);
scanf(%d, &n);
for(i = 1;i <= n;i++)
fact = fact * i;
printf("Factorial is %ld \n", fact);
getch();
}
Output:
Enter an integer:
5
Factorial is 120
27
Page
Department of CSE
28
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
int a = 0, b = 1, c;
clrscr();
printf("Enter the n value: \n");
scanf("%d", &n);
printf(%d \t %d \t, a, b);
c = a+b;
while(c <= n)
{
printf("%d", c);
a = b;
b = c;
c = a+b;
}
getch();
}
Output:
enter the n value
8
0 1 1 2 3 5 8
Exercise:
1. Printing fibonacci series within the given range using functions.
29
Page
Department of CSE
30
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("Enter n: \n");
scanf("%d", &n);
for(i = 1;i <= 10;i++)
printf("%d * %d = %d \n", n, i, n * i);
getch();
}
Output:
Enter n:
4
4*1=4
4*2=8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
31
Page
Department of CSE
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>.
Algorithm:
Step 1: start
Step 2: read n
Step 3: set i = 0
Step 4: if(i < n) then goto step 5 else goto step 7
Step 5: read a[i]
Step 6: set i = i + 1 and goto step 4
Step 7: set max = a[0], min = a[0] and i = 1
Step 8: if(i < n) then goto step 9 else goto step 11
Step 9: if(a[i] > max) then max = a[i] else goto step 7
32
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf(%d, &n);
scanf(%d, &a[i]);
max = a[0];
max = a[0];
{
33
max = a[i];
Department of CSE
min = a[i];
getch();
Output:
78 45 67 23 14 49
Maximum element = 78
Minimum element = 14
34
Page
Department of CSE
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>.
Algorithm:
Step 1: start
Step 4: set d = b * b - 4 * a * c
Step 9: go to step 18
Department of CSE
36
Page
Department of CSE
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
{
printf("Roots are imaginary \n");
rp = -b / (2*a);
ip = sqrt(-d) / (2*a);
37
printf("Roots are (%f , %f) and (%f , %f) \n", rp, ip, rp, -ip);
Page
}
Department of CSE
}
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)
38
Page
Department of CSE
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>.
Algorithm:
Step 1: start
Step 2: read the values for n & x
Step 3: compute x = (x * 3.14) / 180
Step 4: set sum = x
Step 5: set term = x
Step 6: if(i <= n) then goto step 7
Step 7: compute & set term = (term * (-1) * x * x) / ((2 * i) * (2 * i + 1))
Step 8: compute & set sum = sum + term
Step 9: compute i = i + 1 then goto step 6
Step10: display the value of sum
Step11: stop
39
Page
Department of CSE
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;
}
printf("Sin valve of given angle is %f", sum);
getch();
}
Output:
Enter the no of terms:
3
40
30
Department of CSE
41
Page
Department of CSE
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>.
Algorithm:
Step 1: start
Step 2: read the values for n & x
Step 3: compute x = (x * 3.14) / 180
Step 4: set sum = 1
Step 5: set term = 1
Step 6: if(i <= n) then goto step 7
Step 7: compute & set term = (term * (-1) * x * x) / ((2 * i) * (2 * i - 1))
Step 8: compute & set sum = sum + term
Step 9: compute i = i + 1 then goto step 6
Step10: display the value of sum
Step11: stop
42
Page
Department of CSE
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);
getch();
}
Output:
Enter the no of terms:
3
43
30
Department of CSE
44
Page
Department of CSE
45
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n, temp, rem, sum = 0;
clrscr();
printf("Enter a number: \n");
scanf("%d", &n);
temp = n;
while(n != 0)
{
rem = n % 10;
sum += rem;
n = n / 10;
}
printf("The sum of digits of %d is %d \n", temp, sum);
getch();
}
Output:
Enter a number:
234
The sum of digits of 234 is 9
46
Page
Department of CSE
47
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n, rev = 0, temp, rem;
clrscr();
printf("enter a number: \n");
scanf("%d", &n);
temp = n;
while(n != 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
if(temp = = rev)
printf("The number %d is a palindrome \n", temp);
else
printf("The number %d is not a palindrome\n", temp);
getch();
}
Output:
Enter a number:
121
The number 121 is a palindrome
48
Page
Department of CSE
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>.
Algorithm:
Step 1: start
Step 2: read binary
49
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%ld", &binary);
while(binary != 0)
decimal = decimal+rem*j;
j = j*2;
getch();
}
50
Page
Output:
Department of CSE
1101
51
Page
Department of CSE
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>.
Algorithm:
Step 1: start
Step 2: read decimal
Step 8: set j = i - 1
52
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
int binary[100], i = 1, j;
clrscr();
scanf("%ld", &decimal);
q = decimal;
while(q != 0)
binary[i++] = q % 2;
q = q / 2;
printf("%d", binary[j]);
53
Page
getch();
Department of CSE
Output:
Enter any decimal number:
50
54
Page
Department of CSE
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>.
Algorithm:
Step 1: start
Step 2: read binary
55
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%ld", &binary);
while(binary != 0)
octal = octal+rem*j;
j = j*2;
getch();
}
56
Page
Output:
Department of CSE
1101
57
Page
Department of CSE
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>
void main()
char octal[MAX];
long int i = 0;
clrscr();
scanf("%s", octal);
while(octal[i])
switch(octal[i])
58
{
Page
break;
break;
break;
break;
break;
break;
break;
break;
i++;
59
}
Page
getch();
Department of CSE
Output:
Enter any octal number:
123
001010011
60
Page
Department of CSE
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>.
Algorithm:
Step 1: start
Step 2: read binary
61
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%ld", &binary);
while(binary != 0)
hexadecimal = hexadecimal+rem*j;
j = j*2;
getch();
}
62
Page
Output:
Department of CSE
1101
63
Page
Department of CSE
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>
void main()
long int i = 0;
clrscr();
scanf("%s", hexadecimal);
while(hexadecimal[i])
switch(hexadecimal[i])
64
{
Page
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
Page
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
Page
break;
break;
i++;
getch();
Output:
Enter any hexadecimal number:
2AD5
0010101011010101
67
Page
Department of CSE
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();
scanf("%d", &n);
printf(" ");
68
printf("\n");
getch();
long f = 1;
int i = 1;
f = f*i;
i++;
return f;
69
Page
Department of CSE
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
70
Page
Department of CSE
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();
}
71
Page
Department of CSE
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
72
Page
Department of CSE
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();
scanf("%d", &num);
f = fact(num);
getch();
}
73
int fact(int n)
Page
Department of CSE
if(n = = 1)
return 1;
else
return(n*fact(n-1));
Output:
Enter a number:
74
Page
Department of CSE
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();
scanf("%d", &n);
fibonacci(n);
getch();
75
}
Page
Department of CSE
void fibonacci(int n)
if(n > 0)
first = second;
second = 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 76
Page
Department of CSE
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);
77
}
Page
return x;
Department of CSE
}
Output:
Enter two numbers:
366
60
78
Page
Department of CSE
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>.
Algorithm:
Step 1: start
80
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
if(m != p || n != q)
else
scanf("%d", &a[i][j]);
81
Department of CSE
scanf("%d", &b[i][j]);
printf("%4d", c[i][j]);
printf("\n");
getch();
}
82
Output:
Page
2
83
1
Page
7 7 7
7 7 7
Output:
84
Page
Department of CSE
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>.
Algorithm:
Step 1: start
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
if(n != p)
else
scanf("%d", &a[i][j]);
86
scanf("%d", &b[i][j]);
c[i][j] = 0;
printf("%4d", c[i][j]);
printf("\n");
}
87
}
Page
getch();
Department of CSE
Output:
Output:
1
88
Page
Department of CSE
2
3
32
89
Page
Department of CSE
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>.
Algorithm:
step 1: start
step 2: read n
a[i]=a[j]
90
a[j]=temp
Page
91
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%d", &n);
scanf("%d", &a[i]);
{
92
temp = a[j];
Page
a[j] = a[j+1];
Department of CSE
a[j+1] = temp;
printf("%4d", a[i]);
getch();
Output:
78 95 62 30 12 39
12 30 39 62 78 95
93
Page
Department of CSE
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()
clrscr();
scanf("%d", &n);
scanf("%d", &a[i]);
maxloc = 0;
94
Page
Department of CSE
{
if(a[j] > a[maxloc])
maxloc = j;
temp = a[n-1-i];
a[n-1-i] = a[maxloc];
a[maxloc] = temp;
printf("%4d", a[i]);
getch();
Output:
78 95 62 30 12 39
12 30 39 62 78 95
Department of CSE
Program on Linear Search and Binary Search using Recursive and Non Recursive
Procedures.
36. 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>
void main()
int a[100], n, i, x;
clrscr();
scanf("%d", &n);
scanf("%d", &a[i]);
scanf("%d", &x);
Department of CSE
if(i != -1)
else
getch();
if(n<= 0)
return -1;
if(a[n] = = x)
return n;
else
Output:
Enter Size:
97
5
Page
Enter 5 elements:
Department of CSE
25 30 12 54 60
60
98
Page
Department of CSE
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>.
Algorithm:
step 1: start
step 2: read n
step 8: if(ele==a[i]) then set check=1 goto step 9 else goto step 10
step 9: if(check) then write element is found else write element not
found
99
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%d", &n);
for(i=0;i<n;i++)
scanf("%d", &a[i]);
scanf("%d", &x);
if(a[i] = = x)
{
100
xloc = i;
Page
break;
Department of CSE
if(xloc = = -1)
else
getch();
Output:
78 45 67 23 14 49
23
23 is found at location 3
Output:
5
Page
1 2 34 5 6
89
102
Page
Department of CSE
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
void main()
clrscr();
scanf("%d", &n);
scanf("%d", &list[i]);
Page
Department of CSE
low = 0;
high = n - 1;
scanf("%d", &key);
if(position != -1)
else
getch();
int mid;
return -1;
if (x = = a[mid])
104
return mid;
Page
else
Output:
11 22 33 44 55
33
Number present at 2
105
Page
Department of CSE
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>.
Algorithm:
step 1: start
step 2: read n
step 11: if(ele==a[mid-1]) then set check=1 goto step else goto STEP 12
step 14 if(check) then write element is found else write element not
106
found
Page
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%d", &n);
scanf("%d", &a[i]);
scanf("%d", &x);
low = 0;
high = n-1;
{
107
mid = (low+high) / 2;
Page
if(a[mid] = = x)
Department of CSE
xloc = mid;
break;
high = mid - 1;
else
low = mid + 1;
if(xloc = = -1)
else
getch();
Output
6
108
78 45 67 23 14 49
Department of CSE
23
23 is found at location 3
Output:
1 2 34 5 6
89
109
Page
Department of CSE
40. Printing all prime numbers in the given range (using function).
Aim:
To Print all prime numbers in the given range (using function). For input & output
statements we include the header file <stdio.h> and for clear the screen include the <conio.h>
Algorithm:
Step 1: start
Step 9: stop
110
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
int main()
int m,n;
void primerange(int,int);
clrscr();
scanf("%d%d",&m,&n);
primerange(m,n);
getch();
return 0;
int i,j;
111
for(i=m;i<=n;i++)
Page
{
Department of CSE
for(j=2;j<i;j++)
if(i % j == 0)
break;
if(i==j)
printf("%d\n",i);
Output:
20
5
112
7
Page
11
Department of CSE
13
17
19
113
Page
Department of CSE
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()
clrscr();
getch();
}
114
Output:
Page
Department of CSE
115
Page
Department of CSE
42. 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 target[20] ;
cltscr();
strcpy(target, source) ;
printf("Source String = %s \n", source ) ;
printf("Target String = %s \n", target ) ;
getch();
Output:
Department of CSE
43. 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()
cltscr();
getch();
Output:
117
Department of CSE
44. 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();
gets(a);
strrev(a);
getch();
}
118
Output:
Page
Department of CSE
119
Page
Department of CSE
45. 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()
int i, j, k;
cltscr();
i = strcmp(string1, Methodist);
j = strcmp(string1, string2);
getch();
Page
Department of CSE
Output:
i=0
j = 34
k = -32
121
Page
Department of CSE
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();
gets(s);
i = 0;
i++;
putchar(s[i]);
122
putchar(' ');
Page
while(s[i] != '\0')
Department of CSE
putchar(s[i+1]);
putchar(' ');
i++;
getch();
Output:
Enter a String:
MCET
123
Page
Department of CSE
47. Generating the student records with total, average if marks of 3 subjects are given as inputs
by using an array of student structures.
Aim:
To Generate the student records with total, average if marks of 3 subjects are given as inputs by
using an array of student structures. For input & output statements we include the header file
<stdio.h> and for clear the screen include the <conio.h>
Algorithm:
Step 1:start
Step 2:read n
Step 12:stop
124
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
struct student
{
int sno;
char name[20];
int m1,m2,m3;
int tot;
float avg;
};
main()
int n,i;
clrscr();
scanf("%d",&n);
%d students",n);
Page
Department of CSE
for(i=0;i<n ; i++)
fflush(stdin);
scanf("%d",&s[i].sno);
fflush(stdin);
gets(s[i].name);
fflush(stdin);
scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3);
if(i<n-1)
for(i=0;i<n;i++)
s[i].avg=(float)s[i].tot/3;
{
Page
printf("\n \n \n");
Department of CSE
getch();
Output:
ROLL NO = 1001
NAME = rama
TOTAL = 265
AVERAGE = 88.333336
ROLL NO = 1002
NAME = krishna
127
TOTAL = 247
Page
AVERAGE = 82.333336
Department of CSE
ROLL NO = 1003
NAME = ranga
TOTAL = 190
AVERAGE = 63.333332
128
Page
Department of CSE
48. 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()
FILE *fw,*fr;
clrscr();
gets(fname);
fr = fopen(fname, "r");
if(fr = = NULL)
{
129
exit(0);
Department of CSE
ch = fgetc(fr);
while(ch != EOF)
noc++;
now++;
if(ch = = '\n')
nol++;
now++;
ch = fgetc(fr);
fclose(fr);
getch();
Page
}
Department of CSE
Output:
namecount.c
Total no of lines = 34
131
Page
Department of CSE
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("text.dat", "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
132
Department of CSE
text.dat:
This is my first program in C
language to create a file.
Bye.
To see the contents of this file
open text.dat in any
editor program.
133
Page
Department of CSE
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("text.dat", "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);
}
134
getch();
}
Page
Department of CSE
Output:
Given text.dat
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 text.dat
in any editor.
Output:
Given example.dat
No such file
135
Page
Department of CSE
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 ("text.dat", "r");
if( fs == NULL )
{
puts ("Cannot open source file");
}
ft = fopen ("example.dat", "w");
if(ft == NULL)
{
puts("Cannot open target file");
fclose(fs);
}
while (1)
{
136
ch = fgetc (fs);
if(ch = = EOF)
Page
break;
Department of CSE
else
{
fputc (ch, ft);
puts("File copied");
}
}
fclose (fs);
fclose (ft);
getch();
}
Output:
File copied
137
Page
Department of CSE
Aim:
To write a program which diplay the student details like Rno,name,address. For input &
output statements we include the header file <stdio.h> and for clear the screen include the
<conio.h>
Algorithm:
Step 1:start
Step2:read n
Step 3:create file student.dat
Step 4: initialize I=0
Step 5: if(I<=n) then goto step 6 else goto step 11
Step 6: read rno,name,addr from keyboard
Step 7: store on disk
Step 8: open student.dat
Step 9: read rno,name,addr from student.dat
Step 10:diplay rno,name,addr
Step 11:stop
138
Page
Department of CSE
Program:
#include<stdio.h>
#include<conio.h>
struct student
{
int rno;
char name[20];
char addr[30];
};
void main()
{
int i,n;
struct student s;
FILE *fp;
clrscr();
printf("How many records you want to store in the file\n");
scanf("%d",&n);
printf("Enter any %d student records\n",n);
printf("For each record enter rollnumber,name and addr\n");
fp = fopen("student.dat","wb");
for(i=1;i<=n;i++)
{
printf("enter record\n");
scanf("%d",&s.rno);
fflush(stdin);
gets(s.name);
139
scanf("%d",&s.addr);
fwrite(&s,sizeof(s),1,fp);
Page
printf(rollno=%d\n\nname=%s\n\naddress=%s);
Department of CSE
}
fclose(fp);
printf("file created\n");
getch();
}
Output:
How many records you want to store in the file
2
Enter any 2 student records
For each record enter rollnumber,name and addr
enter record
rllno=12
name=rama rao
address=hyderabad
enter record
rollno=20
name=sita kumari
address=ameerpet
file created
140
Page
Department of CSE