CP Manual PDF
CP Manual PDF
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()
clrscr();
scanf(%d, &n);
scanf(%d, &a[i]);
max=a[0];
1
Page
max=a[0];
Department of CSE
max=a[i];
min=a[i];
getch();
Output:
78 45 67 23 14 49
Maximum element = 78
Minimum element = 14
2
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>.
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
4
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>.
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
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
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
}
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
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()
clrscr();
scanf("%ld", &binary);
while(binary != 0)
decimal = decimal+rem*j;
j = j*2;
}
Department of CSE
getch();
Output:
Enter any number any binary number:
1101
10
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>.
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;
11
}
Page
printf("%d", binary[j]);
getch();
Output:
Enter any decimal number:
50
12
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>.
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%ld", &binary);
while(binary != 0)
octal = octal+rem*j;
j = j*2;
}
Page
getch();
Output:
Enter any number any binary number:
1101
14
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])
15
{
Page
break;
break;
break;
break;
break;
break;
break;
break;
i++;
16
}
Page
getch();
Department of CSE
Output:
Enter any octal number:
123
001010011
17
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>.
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%ld", &binary);
while(binary != 0)
hexadecimal = hexadecimal+rem*j;
j = j*2;
}
Page
getch();
Output:
Enter any number any binary number:
1101
19
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])
20
{
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
23
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(" ");
24
printf("\n");
getch();
long f = 1;
int i = 1;
f = f*i;
i++;
return f;
25
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
26
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();
}
27
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
28
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();
}
29
int fact(int n)
Page
Department of CSE
if(n = = 1)
return 1;
else
return(n*fact(n-1));
Output:
Enter a number:
30
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();
31
}
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 32
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);
33
}
Page
return x;
Department of CSE
}
Output:
Enter two numbers:
366
60
34
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>.
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
if(m != p || n != q)
else
{
35
Page
Department of CSE
scanf("%d", &a[i][j]);
scanf("%d", &b[i][j]);
printf("%4d", c[i][j]);
printf("\n");
36
}
Page
}
Department of CSE
getch();
Output:
6
37
5
Page
4
Department of CSE
7 7 7
7 7 7
Output:
38
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>.
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
if(n != p)
else
scanf("%d", &a[i][j]);
scanf("%d", &b[i][j]);
c[i][j] = 0;
printf("%4d", c[i][j]);
Page
printf("\n");
Department of CSE
getch();
Output:
Output:
3
41
1
Page
Department of CSE
2
3
32
42
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>.
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%d", &n);
scanf("%d", &a[i]);
{
43
Department of CSE
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
printf("%4d", a[i]);
getch();
Output:
78 95 62 30 12 39
Page
12 30 39 62 78 95
45
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;
46
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.
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>
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:
49
5
Page
Enter 5 elements:
Department of CSE
25 30 12 54 60
60
50
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>.
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);
{
Page
Department of CSE
if(a[i] = = x)
xloc = i;
break;
if(xloc = = -1)
else
getch();
Output:
78 45 67 23 14 49
23
Page
23 is found at location 3
Department of CSE
Output:
1 2 34 5 6
89
53
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);
{
Page
Department of CSE
scanf("%d", &list[i]);
low = 0;
high = num - 1;
scanf("%d", &key);
if(position != -1)
else
getch();
int mid;
55
return -1;
Department of CSE
if (x = = a[mid])
return mid;
else
Output:
11 22 33 44 55
Page
33
Number present at 2
57
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>.
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%d", &n);
scanf("%d", &a[i]);
scanf("%d", &x);
low = 0;
58
high = n-1;
Page
Department of CSE
{
mid = (low+high) / 2;
if(a[mid] = = x)
xloc = mid;
break;
high = mid - 1;
else
low = mid + 1;
if(xloc = = -1)
else
getch();
}
59
Page
Output
Department of CSE
78 45 67 23 14 49
23
23 is found at location 3
Output:
1 2 34 5 6
89
60
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();
Output:
61
Page
Department of CSE
62
Page
Department of CSE
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 target[20] ;
cltscr();
strcpy(target, source) ;
printf("Source String = %s \n", source ) ;
printf("Target String = %s \n", target ) ;
getch();
Output:
Department of CSE
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()
cltscr();
getch();
Output:
Department of CSE
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();
gets(a);
strrev(a);
getch();
}
65
Output:
Page
Department of CSE
66
Page
Department of CSE
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()
int i, j, k;
cltscr();
i = strcmp(string1, Methodist);
j = strcmp(string1, string2);
Department of CSE
Output:
i=0
j = 34
k = -32
68
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]);
putchar(' ');
69
Page
while(s[i] != '\0')
Department of CSE
putchar(s[i+1]);
putchar(' ');
i++;
getch();
Output:
Enter a String:
MCET
70
Page
Department of CSE
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()
FILE *fw,*fr;
clrscr();
gets(fname);
fr = fopen(fname, "r");
if(fr = = NULL)
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
73
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
language to create a file.
74
Bye.
Page
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.
75
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);
}
getch();
76
}
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
77
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)
{
ch = fgetc (fs);
78
if(ch = = EOF)
Page
break;
Department of CSE
else
{
fputc (ch, ft);
puts("File copied");
}
}
fclose (fs);
fclose (ft);
getch();
}
Output:
File copied
79
Page
Department of CSE