C PROGRAMMING FOR ENGINEERS LAB MANUAL
C PROGRAMMING FOR ENGINEERS LAB MANUAL
DESCRIPTION:
Ex: 1234
Summation =1+2+3+4=10
ALGORITHM:
Step 1: Start
Step 2: Read n
Step 4: while(n!=0)
Begin
Step 5: r←n%10
Step 6: sum←sum+r
Step 7: n←n/10
End
Step 9: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int n,r,sum=0;
clrscr();
1
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
scanf("%d",&n);
while(n!=0)
r=n%10;
sum=sum+r;
n=n/10;
getch();
INPUT:
5321
OUTPUT:
2
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
2) Fibonacci Sequence is defined as follows: the first and second terms in the
sequence are 0 and 1. Subsequent terms are found by adding the preceding
two terms in the sequence. Write a C program to generate the first n terms
of the sequence.
AIM: To generate the first n terms of the Fibonacci sequence..
DESCRIPTION: Initial Fibonacci numbers are 0 and 1. Next number can be generated by
adding two numbers. So 0+1=1. Therefore next number can be generated by adding two
previous . so Fibonacci series is 0 1 1 2 3 5 ……
ALGORITHM:
Step 1 : Start
Step 2 : Read n
Step 3 : Initialize f0 ← 0, f1 ← 1, f ← 0
Step 4 :i=0
printf("%d\t",f0);
f=f0+f1;
f0=f1;
f1=f;
i=i+1;
Step 6 : Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int f0,f1,f,n,i;
clrscr();
3
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
scanf("%d",&n);
f0=0;
f1=1;
i=0;
while(i<n)
printf("%d\t",f0);
f=f0+f1;
f0=f1;
f1=f;
i=i+1;
INPUT:
10
OUTPUT:
0 1 1 2 3 5 8 13 21 34
4
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
ALGORITHM:
Step 1: Start
Step 2: Read n
Step 3: Initialize f0 ← 0, f1 ← 1, f ← 0
Step 4:i=0
Step 5:
while(i<=n) do as follows
printf("%d\t",f0);
f=f0+f1;
f0=f1;
f1=f; i=i+1;
Step 6: Stop
PROGRAM:
include <stdio.h>
#include <conio.h>
void main()
clrscr();
scanf("%d", &n);
f0 = 0;
5
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
f1 = 1;
i = 0;
while (i < n)
printf("%d\t", f0);
f = f0 + f1;
f0 = f1;
f1 = f;
i = i + 1;
INPUT:
10
OUTPUT:
6
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
DESCRIPTION:
Prime number is a number which is exactly divisible by one and itself only
Ex: 2, 3,5,7,………;
ALGORITHM:
Step 1: start
Step 2: read n
ii) increment j
Step 8: increment i
#include<stdio.h>
#include<conio.h>
void main()
int n,i,fact,j;
7
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
{ fact=0;
for(j=1;j<=i;j++)
if(i%j==0)
fact++;
if(fact==2)
printf("\n %d",i);
getch( );
OUTPUT:
235
8
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
Step 1: Start
Step 5: root1=(-b+sqrt(disc))/(2*a)
Step 6: root2=(-b-sqrt(disc))/(2*a)
End
Step 9: root1=-b/(2*a)
End
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
9
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
int a,b,c;
float disc,root1,root2;
float img,real;
scanf("%d%d%d",&a,&b,&c);
disc=(float)b*b-4*a*c;
if(disc>0)
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("Root1=%f\n",root1);
printf("Root2=%f\n",root2);
else if(disc==0)
root1=-b/(2*a);
root2=root1;
printf("Root1=%f\n",root1);
printf("Root2=%f\n",root2);
else
disc=-disc;
img=(float)disc/2*a;
real=(float)-b/2*a;
if (img>0)
10
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
{ printf("Root1=%f + i%f\n",real,img);
printf("Root2=%f - i%f\n",real,img);
else
{ img=-img;
printf("Root1=%f + i%f\n",real,img);
printf("Root2=%f - i%f\n",real,img);
return 0;
INPUT:
144
OUTPUT:
11
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
6a) Write a C program to find the factorial of a given integer using non-
recursive function.
AIM:
ALGORITHM:
Step 1: Start
Step 2: Read n
Initialize f ← 1
End
PROGRAM:
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
int n,i,f;
clrscr();
scanf("%d",&n);
12
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
f=fact(n);
getch();
int fact(int n)
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return(f);
INPUT:
OUTPUT:
13
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
6b) Write a C program to find the factorial of a given integer using recursive
function.
AIM:
ALGORITHM:
main program:
Step 1: start
Step 2: read n
Step 5: stop
Sub program:
PROGRAM:
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
int n,res;
clrscr();
printf("ENETR A NUMBER:\n");
scanf("%d",&n);
14
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
res=fact(n);
getch();
int fact(int n)
int r;
if(n==0)
return(1);
else
r=n*fact(n-1);
return(r);
}
INPUT:
5
OUTPUT:
15
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
7a) Write a C program to find the GCD of two given integers by using the
recursive function
AIM: To find the Gcd of two given integers by using the recursive function
ALGORITHM:
Main program:
Step 1: start
Step 3: call the sub program GCD(a,b) for print the value
Step 4: stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
if(n>m)
return gcdrecursive(n,m);
if(n==0)
return m;
else
void main()
16
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
scanf("%d%d",&a,&b);
INPUT:
OUTPUT:
GCD of a,b is : 5
17
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
7b) Write a C program to find the GCD of two given integers using non-
recursive function.
AIM:To find the GCD of two given integers by using the non recursive function
DESCRIPTION:
GCD means Greatest Common Divisor. i.e the highest number which divides the given
number
Ex: GCD(12,24) is 12
ALGORITHM:
Step 1: start
Step 5: stop
Sub program:
Step 2: remainder=p-(p/q*q)
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int remainder;
remainder=m-(m/n*n);
if(remainder==0)
18
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
return n;
else
gcdnonrecursive(n,remainder);
void main()
int a,b,igcd;
clrscr();
scanf("%d%d",&a,&b);
printf("GCD of %d",gcdnonrecursive(a,b));
getch();
OUTPUT:
GCD of a,b is : 5
19
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
PROGRAM:
#include <stdio.h>
if (n == 1)
return;
int main()
return 0;
}
OUTPUT:
20
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
21
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
9) Write a C program, which takes two integer operands and one operator from
the user, performs the operation and then prints the result. (Consider the
operators +,-,*, /, % and use Switch Statement)
AIM: To takes two integer operands and one operator from the user,
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
while(1) {
printf("************\n");
printf("1.Addition\n");
printf("2.Subtraction\n");
printf("3.Multiplication\n");
printf("4.Division\n");
printf("5.Modulus\n");
printf(“6.Exit\n”);
printf("\n");
printf("ENTER UR CHOICE\n");
scanf("%d",&ch);
switch(ch) {
case 1: c=a+b;
case 2: c=a-b;
22
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
case 3: c=a*b;
case 4: c=a/b;
case 5: c=a%b;
getch();
INPUT:
OUTPUT:
MENU OPTIONS
6.Exit
ENTER UR CHOICE 1
23
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
10) Write a C program to find both the largest and smallest number in a list of
integers.
AIM:To find the largest and smallest number in a list of integers.
ALGORITHM:
Step 1: start
Step 2: read n
Read a[i]
Increment i
Goto step 4
If a[i]<small
Assign small=a[i]
If a[i]>large
Assign large=a[i]
Step 9: stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
24
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
int a[10],i,n,small,large;
clrscr();
scanf("%d",&n);
scanf("%d",&a[i]);
small=a[0];
large=a[0];
small=a[i];
large=a[i];
getch();
INPUT:
7 10 9 8 6 5 2 3 4 1
OUTPUT:
largest value is : 10
smallest value is : 1
25
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
ALGORITHM:
Step 1: Start
for j is 0 to 2 by step 1
Step 8: Stop
#include<stdio.h>
#include<conio.h>
void main()
int a[3][3],b[3][3],c[3][3];
int i,j;
clrscr();
printf("ENTER A MATRIX\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("ENTER B MATRIX\n");
for(i=0;i<3;i++)
26
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
printf("\n");
getch();
27
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
INPUT:
ENTER a MATRIX
123
456
789
ENTER b MATRIX
111
111
111
OUTPUT:
234
567
8 9 10
28
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
Two Matrices.
AIM: To perform multiplication of two matrices.
ALGORITHM:
Step 1: Start
for j is 0 to 2 by step 1
Step 8: Stop
Program:
#include<stdio.h >
#include<conio.h>
int i,j,k;
void main()
int a[10][10],b[10][10],c[10][10],m,n,p,q;
clrscr();
scanf("%d%d",&m,&n);
29
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
scanf("%d%d",&p,&q);
if(n!=p)
else
read(a,m,n);
read(b,m,n);
mul(a,b,c,m,n,p,q);
display(a,m,n);
display(b,m,n);
display(c,m,n);
getch();
for (i=0;i<m;i++)
for(j=0;j<q;j++)
z[i][j]=0;
for(k=0;k<n;k++)
30
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
z[i][j]+= x[i][k]*y[k][j];
for (i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&x[i][j]);
for (i=0;i<m;i++)
for(j=0;j<n;j++)
printf("%5d",x[i][j]);
printf("\n");
printf("\n");
INPUT:
10
26
31
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
34
42
OUTPUT:
A matrix is:
10
26
B Matrix is:
34
42
C matrix is:
34
24 20
32
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
ALGORITHM:
Step 1: start
Step 7: copy sub string into main string from position p-1
Step 8: copy temporary string into main string from position p+n-1
PROGRAM:
#include<stdio.h>
#include<string.h>
main()
char a[3qq0],b[30],c[30];
int pos=0,i=0,l,la,lb,lc,j;
puts("Enter a string");
gets(a);
gets(b);
scanf("%d",&pos);
33
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
la=strlen(a);
lb=strlen(b);
l=pos+lb;
lc=la+lb;
for(i=0;i<pos;i++)
c[i]=a[i];
j=0;
for(i=pos;i<=l;i++)
c[i]=b[j];
j++;
j=pos;
for(i=l;i<lc;i++)
c[i]=a[j];
j++;
c[i]='\0';
printf("%s",c);
INPUT:
Comer
34
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
put
OUTPUT:
Computer
35
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
ALGORITHM:
Step 1: start
Program:
#include<stdio.h>
#include<string.h>
main()
char a[30],c[30];
int pos=0,i=0,L,La,j,n;
puts("Enter a string");
gets(a);
scanf("%d",&pos);
scanf("%d",&n);
La=strlen(a);
L=pos+n;
for(i=0;i<pos;i++)
c[i]=a[i];
36
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
j=pos;
for(i=L;i<=La;i++)
c[j]=a[i];
j++;
printf("%s",c);
INPUT:
jayapal
OUTPUT:
Jayal
37
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
13) Write a C program using user defined functions to determine whether the
DESCRIPTION :
ALGORITHM:
Step 1: start
Step 8: stop
PROGRAM:
#include <stdio.h>
#include <string.h>
void main()
gets(string);
38
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
length++;
reverse_string[length - i - 1] = string[i];
if (reverse_string[i] != string[i])
flag = 0;
if (flag == 1)
else
INPUT:
Enter a string
madam
OUTPUT:
madam is a palindrome
39
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
14) Write a C program that displays the position or index in the string S.
or - 1 if S doesn't contain T
ALGORITHM:
Step 1: start
Step 4: searching the string T in string S and then perform the following steps
i. found=strstr(S,T)
ii. if found print the second string is found in the first string at the
Step 6: stop
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
char *found;
clrscr();
gets(s);
40
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
gets(t);
found=strstr(s,t);
if(found)
else
printf("-1");
getch();
INPUT:
computer
mp
OUTPUT:
41
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
ALGORITHM:
Step 1: Start
Step 3: Compare each character with newline char ‘\n’ to count no of lines
Step 4: Compare each character with tab char ‘\t\’ or space char ‘ ‘ to count no
of words
Step 5: Compare first character with NULL char ‘\0’ to find the end of text
Step 8: Stop.
PROGRAM:
#include <stdio.h>
void main()
int i,c,
end = 0,
characters = 0,
words = 0,
lines = 0;
while( end == 0)
42
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
c = 0;
while((ctr=getchar()) != '\n')
line[c++] = ctr;
line[c] = '\0';
if(line[0] == '\0')
break ;
else
words++;
words++;
printf ("\n");
INPUT:
43
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
OUTPUT:
44
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
PROGRAM:
#include <stdio.h>
void printPascal(int n)
printf(" ");
int coef = 1;
// is always 1
printf("%4d", coef);
printf("\n");
// Driver code
int main()
45
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
int n = 5;
printPascal(n);
return 0;
OUTPUT:
1 1
1 2 1
1 3 3 1
1 4 6 4 1
46
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
PROGRAM:
#include <stdio.h>
int main()
int rows;
scanf("%d", &rows);
// rows
printf("\n");
return 0; }
OUTPUT :
Number of rows: 5
22
333
4444
55555
47
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
18) Write a C program to read in two numbers, x and n, and then compute the
sum of this geometric progression:1+x+x2+x3+………….+xn For example: if
n is 3 and x is 5, then the program computes 1+5+25+125. Print x, n, the sum
Perform error checking. For example, the formula does not make sense for
negative exponents – if n is less than 0.Have your program print an error
message if n<0, then go back and read in the next pair of numbers of without
computing the sum. Are any values of x also illegal ? If so, test for them too.
AIM: To read in two numbers ,x and n and then compute the sum of given geometric
progression
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
int s_sum,i,x,n;
clrscr();
scanf("%d %d",&x,&n);
if(n<=0 || x<=0)
else
s_sum=1;
for(i=1;i<=n;i++)
s_sum=s_sum+pow(x,i);
48
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
printf("Sum of series=%d\n",s_sum);
getch();
OUTPUT:
Sum of series=31
49
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
int a[10],i,n;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
if(a[i]==0)
a[i]=1;
else
a[i]=0;
for(i=n-1;i>=0;i--)
if(a[i]==0)
50
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
a[i]=1;
break;
else
a[i]=0;
if(a[i-1]==0)
a[i-1]=1;
break;
} }
for(i=0;i<n;i++)
printf("%d",a[i]);
getch(); }
OUTPUT:
Enter no of bits
00100
51
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
PROGRAM:
#include <stdio.h>
#include <conio.h>
main(){
char roman[30];
int deci=0;
int length,i,d[30];
");
printf("Decimal:.........Roman
");
printf("%5d............%3c
",1,'I');
printf("%5d............%3c
",5,'V');
printf("%5d............%3c
",10,'X');
printf("%5d............%3c
",50,'L');
printf("%5d............%3c
",100,'C');
printf("%5d............%3c
",500,'D');
printf("%5d............%3c
",1000,'M');
52
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
scanf("%s",roman);
length=strlen(roman);
for(i=0;i<length;i++){
switch(roman[i]){
case 'm':
case 'd':
case 'c':
case 'l':
case 'x':
case 'v':
case 'i':
for(i=0;i<length;i++){
if(i==length-1 || d[i]>=d[i+1])
deci += d[i];
else
deci -= d[i];
53
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
OUTPUT:
Decimal:.........Roman
1............ I
5............ V
10............ X
50............ L
100............ C
500............ D
1000............ M
54
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
21) Write a C program that uses functions to perform the following operations:
ALGORITHM:
Step 1:start
Step 3: c3=c1+c2
Step 4:print c3
Step 5: c3=c1-c2
Step 6: print c3
Step 7: c3=c1*c2
Step 8: print c3
Step 9: c3=c1/c2
Step 11:print c
Step 12:stop
PROGRAM:
#include <stdio.h>
#include <conio.h>
struct complex
}a, b, c;
55
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
void main ()
clrscr();
a = read();
write(a);
b = read();
write(b);
printf("Addition\n ");
c = add(a, b);
write(c);
printf("Substraction\n ");
c = sub(a, b);
write(c);
printf("Multiplication\n");
c = mul(a, b);
write(c);
printf("Division\n");
c = div(a, b);
write(c);
56
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
getch();
struct complex t;
scanf("%f", &t.real);
scanf("%f", &t.imag);
return t;
printf("\n");
struct complex t;
return t;
struct complex t;
57
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
return t;
struct complex t;
return t;
struct complex t;
return(t);
Complex number is
2.0 + i4.0
58
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
Complex number is
4.0 + i2.0
Addition
Complex number is
6.0 + i6.0
Subtraction
Complex number is
-2.0 + i2.0
Multiplication
Complex number is
0.0 + i20.0
Division
Complex number is
0.6 + i0.8
59
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
(Note: The file name and n are specified on the command line.)
ALGORITHM:
Step 1: Start
Step 5: if NULL pointer, then print source file can not be open
Step 7: if NULL pointer, then print destination file can not be open
Step 8 : read a character from source file and write to destination file until EOF
PROGRAM:
#include<stdio.h>
#include<process.h>
#include<conio.h>
void main()
FILE *ft,*fs;
int c=0;
clrscr();
fs=fopen("a.txt","r");
ft=fopen("b.txt","w");
if(fs==NULL)
60
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
exit(1);
else
if(ft==NULL)
exit(1);
while(!feof(fs))
fputc(fgetc(fs),ft);
c++;
c=fcloseall();
INPUT:
a.txt
OUTPUT:
2 files closed
61
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
ALGORITHM:
Step 1: Start
K= *argv[2]-48
Step 8: Store chars from last position to initial position in another string(temp)
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
char a[15];
char s[20];
char n;
int k;
int j=0;
62
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
int i;
int len;
FILE *fp;
if(argc!=3)
exit(0);
fp = fopen(argv[1],"r");
if(fp == NULL)
exit(0);
k=*argv[2]-48;
n = fread(a,1,k,fp);
a[n]='\0';
len=strlen(a);
for(i=len-1;i>=0;i--)
s[j]=a[i];
printf("%c",s[j]);
j=j+1;
s[j+1]='\0';
getch();
63
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
INPUT:
source.c
this is source
ouput.c
this is source
OUTPUT:
source.c ouput.c
source.c
this is source
ecruos si siht
64
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
ALGORITHM:
Step 1: Start
Step 5: if NULL pointer, then print source file can not be open
Step 7: if NULL pointer, then print destination file can not be open
Step 8 : read a character from source file and write to destination file until EOF
PROGRAM:
#include<stdio.h>
#include<process.h>
#include<conio.h>
void main()
FILE *ft,*fs;
int c=0;
clrscr();
fs=fopen("a.txt","r");
ft=fopen("b.txt","w");
if(fs==NULL)
65
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
exit(1);
else
if(ft==NULL)
exit(1);
while(!feof(fs))
fputc(fgetc(fs),ft);
c++;
c=fcloseall();
INPUT:
a.txt
OUTPUT:
66
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
23b) Write a C program to merge two files into a third file (i.e., the contents of
the first file followed by those of the second are put in the third file).
PROGRAM :
#include<stdio.h>
#include<conio.h>
int main()
FILE *fp1,*fp2,*fp3;
char file1[20],file2[20],file3[20],ch;
gets(file1);
gets(file2);
gets(file3);
fp1=fopen(file1,"r");
fp2=fopen(file2,"r");
fp3=fopen(file3,"w");
if(fp1==NULL&&fp2==NULL)
else
if(fp3==NULL)
else
67
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
while((ch=fgetc(fp1))!=EOF)
putc(ch,fp3);
while((ch=fgetc(fp2))!=EOF)
putc(ch,fp3);
fcloseall();
getch();
INPUT:
source.c
this is source
ouput.c
this is source
OUTPUT:
source.c ouput.c
source.c
this is source
ecruos si siht
68
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
24) Write a C program that implements the following sorting methods to sort a
given list of integers in ascending order
PROGRAM:
#include <stdio.h>
#define MAXSIZE 10
void main()
int array[MAXSIZE];
scanf("%d", &num);
scanf("%d", &array[i]);
printf("%d\n", array[i]);
69
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
temp = array[j];
array[j + 1] = temp;
printf("%d\n", array[i]);
OUTPUT:
23
45
67
89
12
70
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
34
Input array is
23
45
67
89
12
34
12
23
34
45
67
89
ii)Insertion sort
AIM: C program for implementation of Insertion sort
PROGRAM:
#include <math.h>
#include <stdio.h>
71
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
int i, key, j;
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
// an array of size n
int i;
72
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
printf("\n");
// Driver code
int main()
insertionSort(arr, n);
printArray(arr, n);
return 0;
OUTPUT:
5 6 11 12 13
PROGRAM:
#include <stdio.h>
*xp = *yp;
73
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
*yp = temp;
int i, j, min_idx;
min_idx = i;
min_idx = j;
if(min_idx != i)
swap(&arr[min_idx], &arr[i]);
int i;
74
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
printf("\n");
int main()
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printArray(arr, n);
return 0;
OUTPUT:
Sorted array:
11 12 22 25 64
75
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
25) Write C programs that use both recursive and non recursive functions to
perform the following searching operations for a Key value in a given list of
integers:
PROGRAM:
#include <stdio.h>
#define MAX_LEN 10
/* Non-Recursive function*/
l1 = 0;
i = num-1;
while(l1 <= i)
j = (l1+i)/2;
flag =1;
break;
else
l1 = j+1;
76
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
else
i = j-1;
if( flag == 0)
/* Recursive function*/
int m,pos;
if (arrayStart<=arrayEnd)
m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a<l[m])
return b_search_recursive(l,arrayStart,m-1,a);
else
return b_search_recursive(l,m+1,arrayEnd,a);
return -1;
int i;
for(i=0;i<n;i++)
77
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
scanf("%d",&l[i]);
int i;
for(i=0;i<n;i++)
printf("%d\t",l[i]);
/*main function*/
main()
int ch,pos;
//clrscr(); printf("==================================================");
printf("\n\t\t\tMENU"); printf("\
n================================================");
scanf("%d",&ch);
scanf("%d",&num);
read_list(l,num);
print_list(l,num);
78
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
scanf("%d",&ele);
switch(ch)
pos=b_search_recursive(l,0,num,ele);
if(pos==-1)
else
//getch();
break;
b_search_nonrecursive(l,num,ele);
//getch();
break;
//getch();
OUTPUT:
79
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
12
22
32
42
52
12 22 32 42 52
42
Recursive method:
80
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
LAB MANUAL
ON
81
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
Vision
To acknowledge quality education and instill high patterns of discipline
making the students technologically superior and ethically strong which involves
the improvement in the quality of life in human race.
Mission
To achieve and impart holistic technical education using the best of
infrastructure, outstanding technical and teaching expertise to establish the
students in to competent and confident engineers.
PEO1: Graduates of program will have complete command over all key principles of
hardware and software areas.
PEO2: Graduates of program will develop skills to identify a real life situation, analyze the
problem,formulate a solution and help industry and individuals overcome issues.
PEO3: Graduates of program will cultivate professional skills, develop soft skills and be ready
with complete skill set demanded by industry or higher learning centers.
82
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
PSO-2: Computer product and Application Development: Interpret and analyze the problem,
formulate an efficient hardware and software solution for the real world. Socio - industry
related problems and needs using computing methodologies and latest technologies.
2. Problem analysis: Identify, formulate review research literature and analyze complex
engineering problems reaching substantiated conclusions using first principle of
mathematics, natural science and engineering science.
and design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex engineering
6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
solutions in societal and environmental contexts, and demonstrate the knowledge of, and
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
83
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one's own work, as a
member and leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change.
INDEX:
84
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
2 Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0
and 1. Subsequent terms are found by adding the preceding two terms in the sequence.
3 Write a C program to generate the first n terms of the sequence
4 Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user.
5 Write a C program to find the roots of a quadratic equation
7 Write a C program to find the GCD (greatest common divisor) of two given integers
9 Write a C program, which takes two integer operands and one operator from the user,
performs the operation and then prints the result. (Consider the operators +,-,*, /, % and
use Switch Statement)
10 Write a C program to find both the largest and smallest number in a list of integers.
11 Write a C program that uses functions to perform the following: i) Addition of Two
Matrices ii) Multiplication of Two Matrices
12 Write a C program that uses functions to perform the following operations: i) To insert a
sub-string in to a given main string from a given position. ii) To delete n Characters from a
given position in a given string.
13 Write a C program to determine if the given string is a palindrome or not
14 Write a C program that displays the position or index in the string S where the string T
begins, or – 1 if S doesn’t contain T.
15 Write a C program to count the lines, words and characters in a given text.
18 Write a C program to read in two numbers, x and n, and then compute the sum of this
geometric progression: 1+x+x2+x3+………….+xn For example: if n is 3 and x is 5, then the
program computes 1+5+25+125. Print x, n, the sum Perform error checking. For example,
the formula does not make sense for negative exponents – if n is less than 0. Have your
program print an error message if n
19 2’s complement of a number is obtained by scanning it from right to left and
complementing all the bits after the first appearance of a 1. Thus 2’s complement of
11100 is 00100. Write a C program to find the 2’s complement of a binary number
20 Write a C program to convert a Roman numeral to its decimal equivalent.
21 Write a C program that uses functions to perform the following operations: i) Reading a
complex number ii) Writing a complex number iii) Addition of two complex numbers iv)
Multiplication of two complex numbers (Note: represent complex number using a
structure.)
85
KODADA INSTITUTE OF TECNOLOGY &SCIENCE FOR WOMEN CSE
22 i. Write a C program which copies one file to another. ii. Write a C program to reverse the
first n characters in a file. (Note: The file name and n are specified on the command line.)
23 i. Write a C program to display the contents of a file. ii. Write a C program to merge two
files into a third file (i.e., the contents of the first file followed by those of the second are
put in the third file)
24 Write a C program that implements the following sorting methods to sort a given list of
integers in ascending order i) Bubble sort ii) Selection sort iii)Insertion sort
25 Write C programs that use both recursive and non recursive functions to perform the
following searching operations for a Key value in a given list of integers: i) Linear search ii)
Binary search
86