B Tech - Lab Manuals - CSE - CP C Programming Lab Manuals
B Tech - Lab Manuals - CSE - CP C Programming Lab Manuals
com
om
C PROGRAMMING LAB
r.c
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 1
www.FirstRanker.com www.FirstRanker.com
DEPARTMENT OF CSE
EXPERIMENT LIST
a) The total distance traveled by vehicle in‘t’ seconds is given by distance = ut+1/2at2
where ‘u’ and ‘a’ are the initial velocity (m/sec.) and acceleration (m/sec2). Write C
ke
program to find the distance traveled at regular intervals of time given the values of ‘u’
an
and ‘a’. The program should provide the flexibility to the user to select his own time
intervals and repeat the calculations for different values of ‘u’ and ‘a’.
tR
b) Write a C program, which takes two integer operands and one operator form the user,
irs
performs the operation and then prints the result. (Consider the operators +,-,*, /, % and
use Switch Statement)
.F
w
www.FirstRanker.com 2
www.FirstRanker.com www.FirstRanker.com
(Note: The file name and n are specified on the command line.)
w
w
Week 13 :
a) Write a C program to display contents of a file.
b) 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)
www.FirstRanker.com 3
www.FirstRanker.com www.FirstRanker.com
www.FirstRanker.com 4
www.FirstRanker.com www.FirstRanker.com
WEEK-1
A) AIM: Write a C program to find the sum of individual digits of a positive integer.
Algorithm:
2. Initialize sum 0
3. while n > 0
4. d n%10
5. sum sum+d
6. n n/10
7. print sum.
Flow chart:
Start
Read n
om
No
r.c
Is
ke
n >0
Program:
an
#include<conio.h>
d = n % 10
irs
void main()
{ Stop
.F
int n, sum=0,d;
w
scanf(“%d”, &n);
while(n>0) n = n / 10
{
d=n%10;
sum=sum+d;
n=n/10;
}
Printf(“sum of individual digits is %d”,sum);
getch();
}
Result:
www.FirstRanker.com 5
www.FirstRanker.com www.FirstRanker.com
B) AIM: A 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.
Algorithm:
b. c a+b
Print “The Fibonacci
c. print c value sequence is”
Print “a, b”
d. a b
i=3
e. b c
No
i<=n
Program:
om
Yes
#include<stdio.h>
r.c
#include<conio.h> c=a+b
void main()
ke
{
an
is:”);
w
a=b
printf(“%d%d”, a,b); b=c
w
for(i=3;i<=n;i++)
w
{
stop
c=a+b;
printf(“%d”,c);
a=b;
b=c;
}
getch();
}
Result:
Enter no of items: 5
The Fibonacci sequence is
01123
www.FirstRanker.com 6
www.FirstRanker.com www.FirstRanker.com
C) AIM: Write a C program to generate all the prime numbers between 1 and n is a
value supplied by the user.
Algorithm:
1. Read n value
2. Initialize count 0
3. for i 2 to n
a. for j 1 to i
b. if i mod j is equal to 0
c. then increment count
d. if count is equal to 2
e. then print i value.
Flow chart:
start
Read n
count = 0
om
No
r.c
for(i=2; i<=n;i++)
ke
Yes
an
No
tR
for(j=1;j<=i;j++)
irs
Yes No
.F
No
if(i%j= if(cou
w
=0) nt==2
w
)
w
Yes
Yes
Count++
Print i value
Stop
Program:
#incloude<stdio.h>
#Include<conio.h>
void main()
{
int i, j, n, count=0;
www.FirstRanker.com 7
www.FirstRanker.com www.FirstRanker.com
clrscr();
printf(“Enter the limit:”);
scanf(“%d”, &n);
printf(“The prime numbers are:”);
for(i=2;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf(“%d\t”, i);
}
getch();
}
Result:
www.FirstRanker.com 8
www.FirstRanker.com www.FirstRanker.com
WEEK-2
Sum=1-x^2/2!+x^4/4!-x^6/6!+x^8/8!-x^10/10!
Algorithm:
3. for i 1 to n
a. fact fact*i
b. if i mod 2 is equal to 0
e. else sum+=pow(x,i)/fact
FLOWCHART: start
4. print sum
Print “Enter the value of x”
om
read x
r.c
n=10, fact =1
Sum=1, i=1
ke
No
for(i=1;i<=n;i++)
an
tR
Yes
irs
fact * = 1
.F
w
No
w
if(i%2= =0)
w
Yes
i =2 || No
Sum += pow(x,i)
i = 10 ||
fact
i=6
Yes
Sum += - pow(x,i)
fact
Print “sum”
stop
www.FirstRanker.com 9
www.FirstRanker.com www.FirstRanker.com
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n=10,x;
long int fact=1;
float sum=1;
printf(“Enter the x value:”);
scanf(“%d”, &x);
for(i=1;i<=n;i++)
{
fact=fact*i;
if(i%2==0)
{
if(i==2||i=10||i==6)
sum+= -pow(x,i)/fact;
else
sum+=pow(x,i)/fact;
}
}
Printf(“sum is %f”, sum);
}
om
Result:
r.c
Enter x value: 2
Sum is: 0
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 10
www.FirstRanker.com www.FirstRanker.com
Algorithm:
2. Initialize d b*b-4*a*c
3. if d==0
b. r1 -b/2*a,r2 r1
4. else if d>0
5. else if d<0
www.FirstRanker.com 11
www.FirstRanker.com www.FirstRanker.com
FLOWCHART
start
Read a,b,c
d=b*b-4.0*a*c
No
if(d=
=0)
Yes
if(d> No
print “roots are real and
equal” 0)
r1=-b/2*a
r2=r1 Yes
print
om
root1,root 2
r.c
stop
ke
Program:
an
#include<stdio.h>
tR
#include<conio.h>
irs
#include<math.h>
.F
void main()
{
w
float a,b,c,d,r1,r2,imp,rp;
w
clrscr();
w
printf(“Enter a,b,c:”);
scanf(“%f%f%f”,&a,&b,&c);
d=b*b-4.0*a*c;
if(d= =0)
{
Printf(“roots are real and equal”);
r1=-b/2*a;
r2=r1;
printf(“root1=%f”,r1);
printf(“root2=%f”,r2);
}
else if(d>0)
{
Printf(“roots are real and unequal”);
r1=(-b+sqrt(d))/2*a;
www.FirstRanker.com 12
www.FirstRanker.com www.FirstRanker.com
r2=(-b-sqrt(d))/2*a;
printf(“root1=%f”,r1);
printf(“root2=%f”,r2);
}
else if(d<0)
{
d=-d;
printf(“roots are complex”);
rp=-b/2*a;
imp=sqrt(d)/2*a;
printf(“root1=%f+i%f”,rp,imp);
printf(“root2=%f-i%f”,rp,imp);
}
getch();
}
Result:
www.FirstRanker.com 13
www.FirstRanker.com www.FirstRanker.com
WEEK-3
A) The total distance travelled by vehicle in 't' seconds is given by distance = ut+1/2at2
where 'u' and 'a' are the initial velocity (m/sec.) and acceleration (m/sec2). Write C
program to find the distance travelled at regular intervals of time given the values of 'u'
and 'a'. The program should provide the flexibility to the user to select his own time
intervals and repeat the calculations for different values of 'u' and 'a'.
Algorithm:
Step 1:Start
Step 3: Set i to 1
Step 4:Set k to dt
Step 7: Write s
Else
Begin
ke
Step 8.3.1:Set I to 0
irs
End
Else
.F
Begin
w
End
Step 9: Stop
www.FirstRanker.com 14
www.FirstRanker.com www.FirstRanker.com
Flowchart:
om
r.c
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 15
www.FirstRanker.com www.FirstRanker.com
Program:
#include<stdio.h>
main()
{
int a,u,t,t1,t2,i;
float s;
clrscr();
printf("ENTER THE VALUES OF a,u,t,t1,t2:");
scanf("%d%d%d%d%d",&a,&u,&t,&t1,&t2);
for(i=t1;i<=t2;i=i+t) // performing the looping operation for time intervals
{
s=(u*i)+(0.5*a*i*i); // calculate the total distance
printf("\n\nthe distance travelled in %d seconds is %f ",i,s);
}
getch();
}
Input/Output:
1
5
r.c
2
irs
3
4
.F
www.FirstRanker.com 16
www.FirstRanker.com www.FirstRanker.com
B) AIM: Two integer operands and one operator form user, performs the operation and
then prints the result.
(Consider the operators +,-,*, /, % and use Switch Statement)
Algorithm:
Step 1: Start
R=a%b
Go to step 8
ke
an
Step 8: write R
tR
Step 9:End
irs
.F
w
w
w
www.FirstRanker.com 17
www.FirstRanker.com www.FirstRanker.com
Flowchart:
om
r.c
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 18
www.FirstRanker.com www.FirstRanker.com
Program:
#include<stdio.h>
main()
{
char op;
float a,b,c;
clrscr();
printf("enter two operands:");
scanf("%d%d",&a,&b);
printf("enter an operator:");
scanf(" %c",&op);
switch(op) // used to select particular case from the user
{
case '+':printf("sum of two numbers %2d %2d is: %d",a,b,a+b);
break;
case '-':printf("subtraction of two numbers %2d %2d is:
%d",a,b,a-b);
break;
case '*':printf("product of two numbers %2d %2d is:
%d",a,b,a*b);
break;
case '/':printf("quotient of two numbers %2d %2d is:
%d",a,b,a/b);
break;
case '%':printf("reminder of two numbers %2d %2d is:
om
%d",a,b,c);
break;
r.c
}
an
getch();
}
tR
irs
Result:
.F
enter an operator:+
w
www.FirstRanker.com 19
www.FirstRanker.com www.FirstRanker.com
WEEK-4
A) AIM: Write a C program to find the factorial of a given integer by using recursive
and non-recursive functions.
i)Recursive Algorithm:
3. if n is equal to 0
Flow chart:
start
om
r.c
Read n
ke
an
tR
If No
irs
n=0
.F
Print “0 factorial is 1”
w
w
return n>=1 ? n *
factorial(n-1) : 1
stop
www.FirstRanker.com 20
www.FirstRanker.com www.FirstRanker.com
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
long int fact;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
if(n==0)
printf("Factorial of 0 is 1\n");
else
printf("Factorial of %d Using Recursive Function is %d\n",n,factorial(n));
getch();
}
/* Recursive Function*/
unsigned int factorial(int n)
{
return n>=1 ? n * factorial(n-1) : 1;
om
}
r.c
Result:
ke
an
Enter number: 5
Factorial of 5 using recursive function is: 120
tR
irs
Step 1: start
w
Step 2: read n
w
www.FirstRanker.com 21
www.FirstRanker.com www.FirstRanker.com
Factorial nonrecursive
start
Read i
Call subprogram
Fact(n)
Print output
Value of fact
Stop
Sub program
om
Fact ( )
r.c
ke
F = 1, i
an
tR
irs
If n == 0 ||
.F
n == 1
w
w
w
I=1 i++
I<=n
www.FirstRanker.com 22
www.FirstRanker.com www.FirstRanker.com
Program:
#include<stdio.h>
#include<conio.h>
int fact(int n) //starting of the sub program
{
int f=1,i;
if((n==0)||(n==1)) // check the condition for n value
return(1);
else
for(i=1;i<=n;i++) // perform the looping operation for calculating the factorial
f=f*i;
return(f);
}
void main()
{
int n;
clrscr();
printf("enter the number :");
scanf("%d",&n);
printf("factoria of number%d",fact(n));
getch();
}
Result:
om
1.Enter a number: 7
r.c
www.FirstRanker.com 23
www.FirstRanker.com www.FirstRanker.com
B) AIM: Write a C program to find the GCD(greatest common divisor) of two given
integers by using recursive and Non-recursive functions.
i)Recursive Algorithm:
1. Define the recursive function
2. Read the a,b values
a. if n>m
c. if n==0
d. then return m
Flow chart:
start
Read a, b
om
No
If n>m
ke
an
Yes
tR
irs
No
w
If n==0
w
w
Yes
return
Return m GCDRecursive(n,
m%n)
stop
www.FirstRanker.com 24
www.FirstRanker.com www.FirstRanker.com
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
getch();
}
/* Recursive Function*/
unsigned int GCDRecursive(unsigned m, unsigned n)
{
if(n>m)
return GCDRecursive(n,m);
if(n==0)
return m;
om
else
return GCDRecursive(n,m%n);
r.c
}
ke
an
Result:
tR
www.FirstRanker.com 25
www.FirstRanker.com www.FirstRanker.com
ii)Non-Recursive Algorithm:
Step 1: start
Step 2: read a,b
Step 3: call sub program g=GCD(a,b)
Step 4: print the g value
Step 5: stop
Flowchart:
start
Read a, b
Output g
Program:
om
#include<stdio.h> stop
#include<conio.h>
r.c
#include<math.h>
gcd (a,b )
int gcdnonrecursive(int m,int n)
ke
{
an
int remainder;
Remainder=p-(p/q*q)
remainder=m-(m/n*n);
tR
return n; If remainder==0
else
.F
gcdnonrecursive(n,remainder);
w
} Gcd(q,remainder) Return q
w
w
Result:
www.FirstRanker.com 26
www.FirstRanker.com www.FirstRanker.com
WEEK-5
A) AIM: - A C program to find both the largest and smallest number in list of integers
Algorithm:
1. Start
2. Read n
3. for i 0 to n
4. do read a[i]
5. small a[0]
6. for i 0 to n
7. do if small > a[i]
8. then small a[i]
9. write small
10. large 0
11. for i 0 to n
12. do if large <a[i]
13. then large a[i]
14. write large
15. Stop
Flowchart: Start
Read n
om
F
for i 0 to n step by
1
r.c
ke
Small
Read elements in array
a[0]
an
tR
F
irs
for i 0 to n step by
1
.F
T
w
w
Small F
> a[i]
w
Small a[i]
Write
small
large 0
www.FirstRanker.com 27
www.FirstRanker.com www.FirstRanker.com
F
for i 0 to n step by
1
large
T< a[i]
large a[i]
Write
large
PROGRAM: Stop
om
#include <stdio.h>
#include <conio.h>
r.c
Void main()
ke
{
an
int i,n,small=0,large=0;
tR
int a[30];
irs
clrscr();
.F
scanf("%d",&n);
w
w
www.FirstRanker.com 28
www.FirstRanker.com www.FirstRanker.com
large=0;
for(i=0;i<n;i++)
{
if(large < a[i])
large = a[i];
}
printf("\n The largest element in given array is %d",large);
RESULT:
Input :
Enter size of the array: 9
Enter values in array elements:
96 46 86 6 36 76 26 16 56
Output:
The smallest element in given array is 6
The largest element in given array is 96
om
r.c
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 29
www.FirstRanker.com www.FirstRanker.com
Algorithm
1. Start
2. read r1,r2,c1,c2
3. if r1 ≠ r2 and c1 ≠ c2
4. then “matrix addition is not possible”
5. else
6. do init_mat(a,r1,c1)
7. print_mat(a,r1,c1)
8. init_mat(b,r2,c2)
9. print_mat(b,r2,2)
10. add_mat(a,b,c,r1,c1)
11. print_mat(c,r1,c1)
12. Stop
init_mat(a4,r4,c4)
1. for i 0 to r4
2. do for j 0 to c4
3. read a4[i][j]
print_mat(a4,r4,c4)
1. for i 0 to r4
2. do for j 0 to c4
3. print a[i][j]
om
4. print next_line
r.c
add_mat(a4,b4,c24.r4,c4)
1. for i 0 to r4
ke
2. do for j to c4
an
www.FirstRanker.com 30
www.FirstRanker.com www.FirstRanker.com
Flowchart:-
Start
Read r1,r2,c1,c2
True
Matrix addition
is not possible
init_mat(a,r1,c1)
om
r.c
print_mat(a,r1,c1)
ke
an
init_mat(b,r2,c2)
tR
irs
.F
w
print_mat(b,r2,c2)
w
w
www.FirstRanker.com 31
www.FirstRanker.com www.FirstRanker.com
add_mat(a,b,c,r1,c1)
print_mat(c,r1,c1)
Stop
Start of init_mat
om
(a4,r4,c4)
r.c
F
return
for i 0 to r4 step by 1
ke
an
tR
irs
for i 0 to r4 step by 1 F
.F
w
w
T
w
read a[i][j]
www.FirstRanker.com 32
www.FirstRanker.com www.FirstRanker.com
Start of print_mat
(a4,r4,c4)
F
for i 0 to r4 step by
return
1
False
for j 0 to c4 step
by 1
print a[i][j]
Start of add_mat
om
(a4,b4,c4,r4,c24)
r.c
ke
F
return for i 0 to r4 step
an
by 1
tR
irs
False
.F
by 1
w
w
PROGRAM:
#include <conio.h>
#include <stdio.h>
www.FirstRanker.com 33
www.FirstRanker.com www.FirstRanker.com
main()
{
int r1,r2,c1,c2;
int a[10][10],b[10][10],c[10][10];
clrscr();
if(r1!=r2 || c1!=c2)
{
printf("\n Matrix Addition is not possible ");
getch();
exit(0);
}
else
{
/* Matrix - A */
printf("\n Enter the elements of Matrix – A:");
init_mat(a,r1,c1);
om
/* Matrix - B */
ke
init_mat(b,r2,c2);
printf("\n The elements of Matrix - B");
tR
print_mat(b,r2,c2);
irs
.F
add_mat(a,b,c,r1,c1);
w
w
www.FirstRanker.com 34
www.FirstRanker.com www.FirstRanker.com
}
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
om
{
c[i][j] = a[i][j]+b[i][j];
r.c
} } }
ke
RESULT:
an
Case - 1
Input :
tR
irs
1 2
w
3 4
Enter the elements of Matrix – B: 1 2 3 4
The elements of Matrix – B:
1 2
2 4
Output:
The elements of Matrix - C after addition of A & B:
2 4
4 8
Case – 2
Input :
Enter the order of Matrix – A: 2 3
Enter the order of Matrix – B: 2 2
Output :
Matrix Addition is not possible
www.FirstRanker.com 35
www.FirstRanker.com www.FirstRanker.com
Algorithm
1. Start
2. read r1,r2,c1,c2
3. if r1 ≠ c2
4. then “matrix multiplication is not possible”
5. else
6. do init_mat(a,r1,c1)
7. print_mat(a,r1,c1)
8. init_mat(b,r2,c2)
9. print_mat(b,r2,2)
10. mul_mat(a,b,c,r1,c1,c2)
11. print_mat(c,r1,c1)
12. Stop
init_mat(a4,r4,c4)
1. for i 0 to r4
2. do for j 0 to c4
3. read a4[i][j]
print_mat(a4,r4,c4)
1. for i 0 to r4
om
2. do for j 0 to c4
3. print a[i][j]
r.c
4. print next_line
ke
mul_mat(a4,b4,c24.r4,c4,c5)
an
1. for i 0 to r4
2. do for j to c5
tR
3. do c[i][j] 0
irs
4. for k 0 to c4
5. c[i][j] c[i][j] + a[i][k]*b[k][j]
.F
w
w
w
www.FirstRanker.com 36
www.FirstRanker.com www.FirstRanker.com
Flow Chart:
Start
Read r1,r2,c1,c2
Yes
Matrix addition
is not possible
init_mat(a,r1,c1)
om
r.c
print_mat(a,r1,c1)
ke
an
init_mat(b,r2,c2)
tR
irs
.F
print_mat(b,r2,c2)
w
w
w
www.FirstRanker.com 37
www.FirstRanker.com www.FirstRanker.com
mul_mat(a,b,c,r1,c1,c2)
Start of print_mat
(a4,r4,c4)
print_mat(c,r1,c1)
F
for i 0 to r4 step
return by 1
A
F
for j 0 to c4 step by
1
Stop
Print a[i][j]
Start of Init_mat
om
(a4,r4,c4)
Start of mul_mat
r.c
return (a4,b4,c,r4,c4,c5)
ke
F
for i 0 to r4 step by
F
an
return 1
for i 0 to r4 step
tR
by 1
irs
F
for j 0 to c4 step by 1
.F
F
w
for j 0 to c5 step
w
by 1
w
read a[i][j]
c[i][j] 0
F
for k 0 to c4 step
by 1
c[i][j] c[i][j] +
a[i][k] + b[k][j]
www.FirstRanker.com 38
www.FirstRanker.com www.FirstRanker.com
PROGRAM :
#include <stdio.h>
#include <conio.h>
if(r1!=c2)
om
{
printf("\n :: Matrix Multiplication is not possible :: ");
r.c
getch();
exit(0);
ke
}
an
else
{
tR
irs
/* Matrix - A */
printf("\n Enter the elements of Matrix – A:");
.F
init_mat(a,r1,c1);
w
print_mat(a,r1,c1);
w
/* Matrix - B */
printf("\n Enter the elements of Matrix – B:");
init_mat(b,r2,c2);
printf("\n The elements of Matrix – B:");
print_mat(b,r2,c2);
www.FirstRanker.com 39
www.FirstRanker.com www.FirstRanker.com
}
r.c
{
an
int i,j,k;
for(i=0;i<r1;i++)
tR
{
irs
for(j=0;j<c2;j++)
{
.F
c[i][j] = 0;
w
for(k=0;k<c1;k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
RESULT:
Case - 1
Input :
Enter the order of Matrix – A: 2 2
Enter the order of Matrix – B: 2 2
Enter the elements of Matrix – A: 1 2 3 4
www.FirstRanker.com 40
www.FirstRanker.com www.FirstRanker.com
Case – 2
Input :
Enter the order of Matrix – A: 2 3
Enter the order of Matrix – B: 1 2
Output :
Matrix Multiplication is not possible
om
r.c
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 41
www.FirstRanker.com www.FirstRanker.com
WEEK-6
A). AIM: - Write A C- Program That Uses Functions To Insert A Sub-String In To A Given
Main String From A Given Position.
Algorithm:
1. start
2. read str (string)
3. read n(position), substr (sub string)
4. ins_substr(str,substr,p,n)
5. stop
ins_substr(str,substr,p,n)
1. k Length[str]
2. m p-1
3. for i m and j n to k
4. substr[j] str[i]
5. substr[j] NULL
6. for j 0 and i m to Length[substr]
7. str[i] substr[j]
8. str[i] NULL
9. print str
Flowchart:
Start
position, n
r.c
ke
Ins_substr(str,sub-
str,n,p)
an
tR
irs
Stop
.F
w
w
w
Start of ins_substr(str,sub_str,p,n)
F For i q-1, j n to
Length[str[i]], step by 1
substr[j] str[i]
1
substr[j] NULL
www.FirstRanker.com 42
www.FirstRanker.com www.FirstRanker.com
F For j 0, i p-1 to
Length[substr[j]], step by
1
substr[j] str[i]
substr[j] NULL
Print str
om
return
r.c
ke
an
PROGRAM:
tR
/* Declaring C-Libraries */
irs
#include <stdio.h>
#include <conio.h>
.F
w
www.FirstRanker.com 43
www.FirstRanker.com www.FirstRanker.com
RESULT:
Case - 1
ke
Input :
an
Enter Sub-String: L
Output :
.F
Case - 2
Input :
Enter the String: HELLO
Enter the specific position : 5
Enter the Number of Characters: 5
Enter Sub-String: WORLD
Output :
The string after inserting substring : HELLO WORLD
:: End of the main program ::
www.FirstRanker.com 44
www.FirstRanker.com www.FirstRanker.com
AIM: - Write A C- Program That Uses Functions To Delete N – Charactres From A Given
Position In A Given String.
Algorithm:
1. start
2. read str(main string)
3. read p (position)
4. read n (number of characters to delete)
5. del_str(str,p,n)
6. stop
del_str(str,p,n)
1. for i 0 , j 0 to Length[str]
2. do if i = p-1
3. ii+n
4. str[j] str[i]
5. str[j] NULL
6. print str
del_str(str,p,n)
ke
an
tR
Stop
irs
.F
w
w
F
X for i 0, j 0 to H
Length[str] by step 1
T i = p-1 F
Z
www.FirstRanker.com 45
www.FirstRanker.com www.FirstRanker.com
i i+n
Z s[j] s[i] H
X S[j] NULL
Print str
return
PROGRAM:
//* Declaring C - Libraries */
om
#include <stdio.h>
#include <conio.h>
r.c
ke
{
.F
int n,p;
w
char str[30];
w
clrscr();
w
www.FirstRanker.com 46
www.FirstRanker.com www.FirstRanker.com
{
int i,j;
for(i=0,j=0;str[i]!='\0';i++,j++)
{
if(i==(p-1))
{
i=i+n;
}
str[j]=str[i];
}
str[j]='\0';
RESULT:
Case - 1
Input :
Enter the String: ABCD EFGH IJKL
Enter the position from where the characters are to be deleted: 5
Enter Number of characters to be deleted: 4
Output :
The string after deletion of characters:: ABCD IJKL
:: End of the main program ::
om
r.c
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 47
www.FirstRanker.com www.FirstRanker.com
Algorithm:
1. Start
2. read str (string)
3. len Length[str]
4. for i 0 (increment step), j len-1 (decrement step) to Length[str]
5. do str[i] ≠ str[j]
6. print “ not palindrome”
7. stop
8. print “palindrome”
9. stop
Start
Flowchart:-
Read str
Len Length[str]
T
ke
an
str[i] F
tR
≠
irs
str[j]
.F
T
w
w
Print “not
w
palindrome”
Print
“palindrome”
stop
www.FirstRanker.com 48
www.FirstRanker.com www.FirstRanker.com
PROGRAM:
/* Declaring C-library */
#include <stdio.h>
#include <conio.h>
}
printf("\n :the given string is palindrome:");
r.c
getch();
}
ke
an
RESULT:
Case - 1
tR
Input :
irs
Output :
w
Case - 2
Input :
Enter the String: ABC
The length of the string is 3
Output :
The given string is not a palindrome:
www.FirstRanker.com 49
www.FirstRanker.com www.FirstRanker.com
WEEK-7
A) AIM: - Write A C- Program That Displays The Positions Or Index Of In The String – S
Where The String – T Begins, Or -1 If String – S Doesnot Contain String – T.
Algorithm:
1. Start
2. read s, t
3. while Length[str]
4. do if s[i] == t[j] and s[i+1] == t[j+1]
5. then break
6. if i = Length[s]
7. then print “-1” start
8. goto end
9. j 0, n i
Read s,t
10. while Length[t[j]] (strings)
11. do if s[i] == t[j]
12. then i i+1 i0
13. j j+1 j0
14. else
15. print “-1”
16. goto end F
S[i] ≠ NULL
next Con
17. print “n+1”
18. end:
19. stop
om
T
r.c
Flowchart:
ke
T
.F
next
w
w
w
F
Compare i T 1
Inr with Length[s]
www.FirstRanker.com 50
www.FirstRanker.com www.FirstRanker.com
1
2
Print “ -1 ”
no
s[i] = t[j] End
Stop Con
yes
Inr i i +1
i i +1
jj+1 whi
next
j0
B in
whi
t[j] ≠ NULL B
Print “-1”
No
yes
stop 2
om
r.c
ke
End
an
tR
irs
Print “n+1”
.F
w
w
w
stop
PROGRAM:
/* declaring C-Libraries */
#include <stdio.h>
#include <conio.h>
#include <string.h>
www.FirstRanker.com 51
www.FirstRanker.com www.FirstRanker.com
main()
{
int i,j,n;
char s[40],t[40];
clrscr();
printf("\n Enter the string:");
gets(s);
fflush(stdin);
printf("\n Enter the sub string:");
gets(t);
n=i;
while(t[j] != '\0')
r.c
{
if(s[i] == t[j])
ke
{
an
i++;
j++;
tR
}
irs
else
{
.F
printf("-1");
w
getch();
w
exit(0);
w
}
}
printf("\n The string is found at %d",n+1);
getch(); }
RESULT:
Input :
Enter the String: HELLO WORLD
Enter substring : WORLD
Output :
The String is found at 6
www.FirstRanker.com 52
www.FirstRanker.com www.FirstRanker.com
AIM: - Write A C- Program To Count The Lines, Words, Characters In A Given Text
Algorithm:
1. start
2. read text
3. while text[i] != EOF
4. do i i+1
5. print i
6. for i 0 to Length[text]
7. do ch++
8. if text[i] = 32 and text[i+1] ≠ ‘ ‘
9. then w++
10. sp++
11. if text[i] = ‘\n’
12. then l++
13. w++
14. print ch
15. print w+1
16. print l
17. print sp
Flow Chart:
om
start
r.c
Read text
ke
an
tR
Test[i] != EOF
irs
.F
ii+1
w
w
www.FirstRanker.com 53
www.FirstRanker.com www.FirstRanker.com
ch ch + 1
no
Text[i] = 32 ne
and text [i+1]
= Space
yes
ww+1
sp sp +1
ne
yes no
ll+1 Text[i] =
ww+1 new line pri
om
r.c
ke
fo 2
an
tR
irs
.F
2
w
w
w
Print ch
Print w+1
Print l
Print sp
stop
www.FirstRanker.com 54
www.FirstRanker.com www.FirstRanker.com
PROGRAM:
/* Declaring C - Libraries */
#include <conio.h>
#include <stdio.h>
/* Main function is starting */
main()
{
char text[200];
int i,l,ch,w,sp;
clrscr();
ch++;
if(text[i]==32 && text[i+1] != ' ')
r.c
{
w++;
ke
sp++;
an
}
if(text[i] == '\n')
tR
{
irs
l++;
w++;
.F
}
w
}
w
Result:
Enter lines of text and press ^Z”” ABCD EFGH IJKL MNOP
www.FirstRanker.com 55
www.FirstRanker.com www.FirstRanker.com
WEEK 8
Algorithm:
Step 1: Start
Step 4: Stop
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 56
www.FirstRanker.com www.FirstRanker.com
START
Flowchart:
Read height n of
pascal triangle
Print “ “
Or i==j
tR
irs
a[i][j]=a[i-1][j-1]+a[i-1][j] a[i][j]=1
.F
w
w
w
Print a[i][j]
Print “\n”
www.FirstRanker.com 57
www.FirstRanker.com www.FirstRanker.com
Program:
#include <stdio.h>
#include <conio.h>
main()
{
int a[10][10],i,j,k,n;
clrscr();
printf("Enter the height of the pascal traingle");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(k=1;k<=n-i;k++)
printf(" ");
for(j=0;j<=i;j++)
{
if(j==0 || i==j)
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
printf("%d ",a[i][j]);
}
printf("\n");
}
om
}
r.c
Result:
ke
an
1 2 1
irs
1 3 3 1
.F
w
w
w
www.FirstRanker.com 58
www.FirstRanker.com www.FirstRanker.com
Algorithm:
Step 1: Start
Step 3: for j := 0 to n do
Step 4: Stop
START
om
Flowchart:
r.c
ke
Read height n of
an
pyramid
tR
STOP
irs
.F
w
Print “ “
Print abs(i)
www.FirstRanker.com 59
www.FirstRanker.com www.FirstRanker.com
Program:
#include <stdio.h>
#include <conio.h>
main()
{
int i,j,k,n;
clrscr();
printf("Enter the height of the pyramid");
scanf("%d",&n);
for(j=0;j<=n;j++)
{
for(k=1;k<=2*(n-j);k++)
printf(" ");
for(i=-j;i<=j;i++)
printf("%d ",abs(i));
printf("\n");
}
}
Result:
Enter the height of the pyramid: 2
1
2 2
om
r.c
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 60
www.FirstRanker.com www.FirstRanker.com
WEEK 9
1+x+x2+x3+x4+……….+xn
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.
Algorithm :
Step 1: Start
Step 2: rept:
Step 5: else
Step 5.1: print not a valid n value
ke
Step 6: End if
tR
irs
Step 7: Stop
.F
w
w
w
www.FirstRanker.com 61
www.FirstRanker.com www.FirstRanker.com
Flowchart:
START
Read values of
x and n
False
IS n>0 Print not a
valid n
True
sum += pow(x,i)
om
r.c
Print x, n, sum
ke
an
tR
STOP
irs
.F
w
Program:
w
w
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
int x,n,sum=0,i;
start:
clrscr();
printf("enter the values for x and n");
scanf("%d%d",&x,&n);
if(n>0)
{
for(i=0;i<=n;i++)
{
sum = sum+pow(x,i);
}
www.FirstRanker.com 62
www.FirstRanker.com www.FirstRanker.com
Result:
om
r.c
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 63
www.FirstRanker.com www.FirstRanker.com
WEEK 10
A) Aim: Write an algorithm, flowchart and a C program for finding the 2’s
complement of a binary number
Algorithm:
Step 1: Start
Step 6: mask := 1
Step 9: Stop
w
w
w
www.FirstRanker.com 64
www.FirstRanker.com www.FirstRanker.com
Flowchart:
START
len=strlen(str)
IS Str[i]==’1’
True False
om
Str[i]=’0’
r.c
Str[i]=’1’
ke
an
tR
irs
.F
w
w
Mask=1
w
www.FirstRanker.com 65
www.FirstRanker.com www.FirstRanker.com
For i=len-1 to 0
by step -1
IS
mask==1
True
IS
str[i]==’1’
True False
Str[i]=’0’ Str[i]=’1’
om
mask=1 mask=0
r.c
ke
an
tR
irs
.F
w
w
w
Print str
(2s complement)
STOP
Program:
#include <stdio.h>
www.FirstRanker.com 66
www.FirstRanker.com www.FirstRanker.com
#include <conio.h>
#include <string.h>
main()
{
char str[32],strdp[32];
int mask,i;
clrscr();
printf("Enter a binary number:");
scanf("%s",str);
strcpy(strdp,str); /* creating duplicate copy */
for(i=0;i<strlen(str);i++) /* computing 1's complement */
{
if(str[i]=='1')
str[i]='0';
else
str[i]='1';
}
printf("1\'s complement of %s is %s\n",strdp,str);
mask=1;
for(i=strlen(str)-1;i>=0;i--) /* computing 2's complement */
{
if(mask==1)
{
if(str[i]=='1')
{
str[i]='0';
mask=1;
om
}
else
r.c
{
str[i]='1';
ke
mask=0;
an
}
}
tR
}
irs
Result:
w
www.FirstRanker.com 67
www.FirstRanker.com www.FirstRanker.com
Algorithm:
Step 1: Start
Step 2: Read the roman numeral
Step 3: len := strlen(roman)
Step 4: for i := 0 to len-1 do
Step 4.1: switch(roman[i])
Step 4.1.1: case ‘m’:
Step 4.1.2: case ‘M’:
Step 4.1.2.1: d[i]:=1000
Step 4.1.3: case ‘d’:
Step 4.1.4: case ‘D’:
Step 4.1.4.1: d[i]:=500
Step 4.1.5: case ‘c’:
Step 4.1.6: case ‘C’:
Step 4.1.6.1: d[i]:=100
Step 4.1.7: case ‘l’:
Step 4.1.8: case ‘L’:
Step 4.1.8.1: d[i]:=50
Step 4.1.9: case ‘x’:
Step 4.1.10: case ‘X’:
Step 4.1.10.1: d[i]:=10
Step 4.1.11: case ‘v’:
om
Step 7: Stop
w
www.FirstRanker.com 68
www.FirstRanker.com www.FirstRanker.com
START
Flowchart:
len=strlen(roman)
roman[i]
’M’ or ’m’
d[i]=1000
’D’ or ’d’
d[i]=500
’C’ or ’c’
d[i]=100
’L’ or ’l’
d[i]=50
’X’ or ’x’
d[i]=10
’V’ or ’v’
d[i]=5
’I’ or ’i’
d[i]=1
om
2
r.c
ke
i== len-1
irs
or
d[i]>=d[i+1]
.F
True False
w
print decimal
number
STOP
Program:
#include <stdio.h>
#include <conio.h>
main()
{
char roman[30];
int deci=0;
int len,i,d[30];
clrscr();
www.FirstRanker.com 69
www.FirstRanker.com www.FirstRanker.com
case 'm':
case 'M': d[i]=1000; break;
case 'd':
case 'D': d[i]= 500; break;
case 'c':
case 'C': d[i]= 100; break;
case 'l':
case 'L': d[i]= 50; break;
case 'x':
case 'X': d[i]= 10; break;;
om
case 'v':
case 'V': d[i]= 5; break;
r.c
case 'i':
case 'I': d[i]= 1;
ke
}
an
}
for(i=0;i<len;i++)
tR
{
irs
if(i==len-1 || d[i]>=d[i+1])
deci += d[i];
.F
else
w
deci -= d[i];
w
}
w
Result:
Enter a Roman numeral: L
The Decimal equivalent of Roman numeral L is :50
www.FirstRanker.com 70
www.FirstRanker.com www.FirstRanker.com
WEEK 11
Algorithm:
Step 1: Start
Step 2: Read the first complex number by calling readcomplex()
Step 3: Read the second complex number by calling readcomplex()
Step 4: read the operator op
Step 5: switch(op)
Step 5.1: case ‘+’: c3 := add(c1,c2)
Step 5.2: case ‘-‘ : c3 := sub(c1,c2)
Step 5.3: case ‘*’: c3 := mul(c1,c2)
Step 5.4: case ‘e’: program end
Step 6: print c3 by calling printcomplex(c1,c2,c3,op)
Step 7: Stop
add (c1,c2)
step 1: c3.x := c1.x + c2.x
om
sub(c1,c2)
ke
mul(c1,c2)
step 1: c3.x :=(c1.x*c2.x+c1.y+c2.y)/(c2.x*c2.x+c2.y*c2.y)
.F
step 3: return c3
w
w
www.FirstRanker.com 71
www.FirstRanker.com www.FirstRanker.com
Flowchart:
START
C1=readcomplex()
C2=readcomplex()
Read operator op
OP
printcomplex(c1,c2,c3,op)
OP=’e’
STOP
add(complex c1,complex c2
)
c3.x=c1.x+c2.x
c3.y=c1.y+c2.y
return(c3)
om
mul(complex c1,complex
r.c
c2)
ke
c3.x=(c1.x*c2.x+c1.y+c2.y)/(c2.x*c2.x+c2.y*c2.y)
c3.y=(c2.x*c1.y-c1.x*c2.y)/(c2.x*c2.x+c2.y*c2.y)
an
return(c3)
tR
sub(complex c1,complex
c2)
irs
c3.x=c1.x-c2.x
.F
c3.y=c1.y-c2.y
return(c3)
w
readcomplex()
w
w
return c
Program:
printcomplex()
#include <stdio.h>
#include <stdlib.h> Print c3
struct compl
{
int x;
int y;
};
typedef struct compl complex;
main()
www.FirstRanker.com 72
www.FirstRanker.com www.FirstRanker.com
{
complex c1,c2,c3;
complex add(complex c1,complex c2);
complex sub(complex c1,complex c2);
complex mul(complex c1,complex c2);
complex readcomplex();
complex printcomplex(complex c1,complex c2,complex c3,char op);
char op;
clrscr();
printf("Reading the first complex number\n");
c1=readcomplex();
printf("Reading the second complex number\n");
c2=readcomplex();
printf("Enter + for addition \n"
"Enter * for multiplication\n"
"Enter - for subtraction\n"
"Enter e for exit:");
fflush(stdin);
op=getche();
switch(op)
{
case '+': c3=add(c1,c2);
break;
case '-': c3=sub(c1,c2);
break;
case '*': c3=mul(c1,c2);
break;
om
printcomplex(c1,c2,c3,op);
getch();
ke
}
an
tR
{
complex c3;
.F
c3.x=c1.x+c2.x;
w
c3.y=c1.y+c2.y;
w
return(c3);
w
}
complex sub(complex c1,complex c2)
{
complex c3;
c3.x=c1.x-c2.x;
c3.y=c1.y-c2.y;
return(c3);
}
complex mul(complex c1,complex c2)
{
complex c3;
c3.x=(c1.x*c2.x+c1.y+c2.y)/(c2.x*c2.x+c2.y*c2.y);
c3.y=(c2.x*c1.y-c1.x*c2.y)/(c2.x*c2.x+c2.y*c2.y);
return(c3);
}
complex readcomplex()
www.FirstRanker.com 73
www.FirstRanker.com www.FirstRanker.com
{
complex c;
printf("Enter the values of x and y of a complex number");
scanf("%d%d",&c.x,&c.y);
return(c);
}
complex printcomplex(complex c1,complex c2,complex c3,char op)
{
printf("\n(%d+i%d)%c(%d+i%d)=%d+i(%d)",c1.x,c1.y,op,c2.x,c2.y,c3.x,c3.y);
}
Result:
Reading the first complex number: 2 + 2i
Reading the second complex number: 2 - 2i
Enter + for addition
"Enter * for multiplication"
"Enter - for subtraction"
"Enter e for exit : *
Result is: 0 om
r.c
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 74
www.FirstRanker.com www.FirstRanker.com
WEEK-12
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main(int argc, char *argv[])
{
FILE *fs,*ft;
char ch;
clrscr();
if(argc!=3)
{
puts("Invalid number of arguments.");
exit(0);
}
fs = fopen(argv[1],"r");
if(fs==NULL)
{
puts("Source file cannot be opened.");
exit(0);
}
ft = fopen(argv[2],"w");
if (ft==NULL)
{
om
exit(0);
}
ke
while(1)
an
{
ch=fgetc(fs);
tR
if (ch==EOF)
irs
break;
else
.F
fputc(ch,ft);
w
}
w
fclose(fs);
w
fclose(ft);
getch();
}
Result:
File created is passed as parameter:
File is copied
www.FirstRanker.com 75
www.FirstRanker.com www.FirstRanker.com
ALGORITHM:
STEP6: Read the contents from existed file and reverse first n characters in the
string from file.
om
PROGRAM:
#include <stdio.h>
r.c
#include <conio.h>
#include <string.h>
ke
#include <process.h>
an
char a[15];
irs
char s[20];
char n;
.F
int k;
w
int j=0;
w
int i;
w
int len;
FILE *fp;
if(argc!=3)
{
puts("Improper number of arguments.");
exit(0);
}
fp = fopen(argv[1],"r");
if(fp == NULL)
{
puts("File cannot be opened.");
exit(0);
}
k=atoi(argv[2]);
n = fread(a,1,k,fp);
a[n]='\0';
www.FirstRanker.com 76
www.FirstRanker.com www.FirstRanker.com
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();
}
Result:
Abc.txt: He is a good boy
Output: yob doog a si eH
om
r.c
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 77
www.FirstRanker.com www.FirstRanker.com
WEEK 14
Program
i) Linear search:
/*SEQUENTIAL SEARCH*/
#include<stdio.h>
main()
{
int a[10],i,n,key,co=0;
clrscr();
printf("how many you want");
scanf("%d",&n);
printf("enter array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the searching elements");
scanf("%d",&key);
search(a,n);
}
om
int i;
for(i=0;i<n;i++)
ke
{
an
if(a[i]==key)
co++;
tR
}
irs
if(co>0)
printf("Element is found");
.F
else
w
printf("Not found");
w
getch();
w
Output:
www.FirstRanker.com 78
www.FirstRanker.com www.FirstRanker.com
low=0;
om
high=n-1;
printf("enter the searching elements");
r.c
scanf("%d",&key);
co=Rbinarysearch(a,low,high,key);
ke
if(co==-1)
an
printf("Not found");
else
tR
printf("Element is found");
irs
getch();
}
.F
w
{
w
int mid;
if(low>high)
return(-1);
mid=(low+high)/2;
if(key==a[mid])
return(mid);
if(key<a[mid])
return(Rbinarysearch(a,low,mid-1,key));
else
return(Rbinarysearch(a,mid+1,high,key));
}
Output:
how many you want5
enter array elements:32 1 45 67 98
enter the searching elements98
Element is found
www.FirstRanker.com 79
www.FirstRanker.com www.FirstRanker.com
WEEK 15
AIM: Write C programs that implement the following sorting methods to sort a given list
of integers in ascending order by using Bubble sort.
1. start
2. take list(array), num
3. readlist(list,num)
4. printlist(list,num)
5. bub_sort(list,num)
6. printlist(list,num)
7. stop
printlist(list,num)
1. for j 0 to num
2. write list[j].
bub_sort(list,num)
1. for i 0 to num
2. for j 0 to (num – i)
3. if( list[j] > list[j+1])
om
PROGRAM:
.F
#include <stdio.h>
w
#define MAX 10
w
w
www.FirstRanker.com 80
www.FirstRanker.com www.FirstRanker.com
void main()
{
int list[MAX], num;
clrscr();
printf("\n\n\n***** Enter the number of elements [Maximum 10] *****\n");
scanf("%d",&num);
readlist(list,num);
printf("\n\nElements in the list before sorting are:\n");
printlist(list,num);
om
bub_sort(list,num);
printf("\n\nElements in the list after sorting are:\n");
r.c
printlist(list,num);
getch();
ke
}
an
tR
irs
Output:
Enter the number of elements [Maximum 10]: 5
.F
www.FirstRanker.com 81
www.FirstRanker.com www.FirstRanker.com
WEEK-16
Program:
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node; /* node type defined */
main()
{
node *head;
int opt;
void create(node *p);
node *insert(node *head);
om
create(head);
printf("\n");
tR
print(head);
irs
printf("\n");
printf("\nNumber of items = %d \n", count(head));
.F
w
while(1)
w
{
w
printf("Enter 1. insertion\n"
" 2. deletion \n"
" 3. traversal \n"
" 4. exit:");
scanf("%d",&opt);
switch(opt)
{
case 1: head=insert(head);
break;
case 2: head= delete(head);
break;
case 3: print(head);
break;
case 4: exit(0);
www.FirstRanker.com 82
www.FirstRanker.com www.FirstRanker.com
}
}
void create(node *list)
{
printf("Input a number\n");
printf("(type -999 at end): ");
scanf("%d", &list -> number); /* create current node */
if(list->number == -999)
{
list->next = NULL;
}
else
{
list->next = (node *)malloc(sizeof(node));
create(list->next);
}
return;
}
if(list->next->next == NULL)
r.c
printf("%d", list->next->number);
ke
}
return;
tR
}
irs
{
w
if(list->next == NULL)
w
return (0);
w
else
return(1+ count(list->next));
}
node *delete(node *head)
{
node *find(node *p, int a);
int key; /* item to be deleted */
node *n1; /* pointer to node preceding key node */
node *p; /* temporary pointer */
printf("\n What is the item (number) to be deleted?");
scanf("%d", &key);
if(head->number == key) /* first node to be deleted) */
{
p = head->next; /* pointer to 2nd node in list */
free(head); /* release space of key node */
head = p; /* make head to point to 1st node */
www.FirstRanker.com 83
www.FirstRanker.com www.FirstRanker.com
}
else
{
n1 = find(head, key);
if(n1 == NULL)
printf("\n key not found \n");
else /* delete key node */
{
p = n1->next->next; /* pointer to the node
following the keynode */
if(head->number == key)
ke
{
an
new->next = head;
irs
head = new;
}
.F
else
w
{
w
n1 = find(head, key);
w
if(n1 == NULL)
printf("\n key is not found \n");
else
{
new = (node *)malloc(sizeof(node));
new->number = x;
new->next = n1->next;
n1->next = new;
}
}
return(head);
}
node *find(node *list, int key)
{
if(list->next->number == key) /* key found */
www.FirstRanker.com 84
www.FirstRanker.com www.FirstRanker.com
return(list);
else
Result:
1. Create
2. Insert
3. Delete
4. Display
5. Exit
Enter Choice : 1
Count = 5
The inserted element is : 3
ke
1. Create
an
2. Insert
3. Delete
tR
4. Display
irs
5. Exit
Enter Choice : 3
.F
Count = 3
w
Deleted element is : 3
w
1. Create
2. Insert
3. Delete
4. Display
5. Exit
Enter Choice : 4
Null ->25->36->45->69->NULL
1. Create
2. Insert
3. Delete
4. Display
5. Exit
Enter Choice : 5
www.FirstRanker.com 85
www.FirstRanker.com www.FirstRanker.com
WEEK-17
#include<stdio.h>
#include<conio.h>
int st_arr[20];
int t=-1;
void main()
{
char choice,num1=0,num2=0;
while(1)
{
clrscr();
printf("======================================");
printf("\n\t\t MENU ");
printf("\n======================================");
printf("\n[1] Using Push Function");
printf("\n[2] Using Pop Function");
om
scanf("%c",&choice);
an
switch(choice-'0')
tR
{
irs
case 1:
{
.F
scanf("%d",&num1);
w
push_ele(num1);
w
break;
}
case 2:
{
num2=pop_ele(1);
printf("\n\tElement to be popped: %d\n\t",num2);
getch();
break;
}
case 3:
{
display_ele();
getch();
break;
www.FirstRanker.com 86
www.FirstRanker.com www.FirstRanker.com
case 4:
exit(1);
break;
default:
printf("\nYour choice is invalid.\n");
break;
}
}
}
{
int ele1;
r.c
if(t==-1)
{
ke
printf("\n\tSTACK is Empty.\n");
an
getch();
exit(1);
tR
}
irs
return(st_arr[t--]);
}
.F
w
void display_ele()
w
{
int k;
printf("\n\tElements present in the stack are:\n\t");
for(k=0;k<=t;k++)
printf("%d\t",st_arr[k]);
}
Result:
Enter size of stack: 4
Enter option push,pop & display 1
Enter element to push: 3
Enter option push,pop & display 1
Enter element to push: 5
Enter option push,pop & display 2
Element 5 deleted
Enter option push,pop & display 3
Elements in stack are: 3
www.FirstRanker.com 87
www.FirstRanker.com www.FirstRanker.com
WEEK-18
Program:
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
#define size 10
#define true 1
#define false 0
struct q_arr
{
int f,r;
int num;
int a[size];
};
/*main function*/
r.c
void main()
{
ke
int ele,k;
an
int ch;
tR
init(queue);
.F
while(1)
w
{
w
clrscr();
w
switch(ch)
{
case 1:
{
printf("\nElement to be inserted:");
www.FirstRanker.com 88
www.FirstRanker.com www.FirstRanker.com
scanf("%d",&ele);
add_ele(queue,ele);
break;
}
case 2:
{
if(!e_que(queue))
{
k=rem_ele(queue);
printf("\n%d element is removed\n",k);
getch();
}
else
{
printf("\tQueue is Empty. No element can be removed.");
getch();
}
break;
}
case 3:
{
display_ele(queue);
getch();
break;
}
om
case 4:
r.c
exit(0);
ke
default:
an
printf("\tInvalid Choice.");
getch();
tR
break;
irs
}
}
.F
}
w
/*end main*/
w
w
www.FirstRanker.com 89
www.FirstRanker.com www.FirstRanker.com
if(queue->r == size - 1)
queue->r = -1;
queue->a[++queue->r] = j;
queue->num++;
return true;
}
if(queue->f == size)
queue->f = 0;
r.c
queue->num--;
return j;
ke
}
an
{
int j;
.F
if(e_que(queue))
w
{
w
return;
}
printf("\nElements present in the Queue are: ");
for(j=queue->f;j<=queue->r;j++)
printf("%d\t",queue->a[j]);
printf("\n");
}
Result:
Enter queue size: 3
Enter add,delete & display 1
Enter element to add: 3
Enter add,delete & display 1
Enter element to add: 5
Enter add,delete & display 3
Elements in queue are: 3 5
www.FirstRanker.com 90
www.FirstRanker.com www.FirstRanker.com
AIM: Write a C program that implements Queue (its operations) using Pointers.
Program:
#define true 1
#define false 0
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct q_point
{
int ele;
struct q_point* n;
};
int e_que(void);
void add_ele(int);
int rem_ele(void);
void show_ele();
/*main function*/
void main()
{
om
int ele,choice,j;
while(1)
r.c
{
clrscr();
ke
printf("==============================================");
printf("\n\t\t MENU\n");
tR
printf("==============================================");
irs
printf("\n\t[4] Exit");
w
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("\n\tElement to be inserted:");
scanf("%d",&ele);
add_ele(ele);
getch();
break;
}
case 2:
{
if(!e_que())
www.FirstRanker.com 91
www.FirstRanker.com www.FirstRanker.com
{
j=rem_ele();
printf("\n\t%d is removed from the queue",j);
getch();
}
else
{
printf("\n\tQueue is Empty.");
getch();
}
break;
}
case 3:
show_ele();
getch();
break;
case 4:
exit(1);
break;
default:
printf("\n\tInvalid choice.");
getch();
break;
}
om
}
r.c
}
ke
int e_que(void)
{
tR
if(f_ptr==NULL)
irs
return true;
return false;
.F
}
w
w
www.FirstRanker.com 92
www.FirstRanker.com www.FirstRanker.com
return;
}
r.c
else
{
ke
while(ptr!=NULL)
{
tR
printf("%d\t",ptr->ele);
irs
ptr=ptr->n;
}
.F
}
w
}
w
w
Result:
Enter queue size: 3
Enter add,delete & display 1
Enter element to add: 3
Enter add,delete & display 1
Enter element to add: 5
Enter add,delete & display 3
Elements in queue are: 3 5
www.FirstRanker.com 93
www.FirstRanker.com www.FirstRanker.com
WEEK 19
Aim: Write C programs to implement the linear regression algorithms.
Program:
#include<math.h>
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
float x,y,m,c,d;
float sumx=0,sumxsq=0,sumy=0,sumxy=0;
clrscr();
printf("enter the number of values for n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter values of x and y");
scanf("%f%f",&x,&y);
sumx=sumx+x;
sumxsq=sumxsq+(x*x);
sumy=sumy+y;
sumxy=sumxy+(x*y);
}
d=n*sumxsq-sumx*sumx;
m=(n*sumxy-sumx*sumy)/d;
om
c=(sumy*sumxsq-sumx*sumxy)/d;
printf("M=%f\tC=%f\n",m,c);
r.c
getch();
}
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 94
www.FirstRanker.com www.FirstRanker.com
WEEK 20
Aim: Write C programs to implement the polynomial regression algorithms.
Program:
#include<math.h>
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,m,n;
float x[20],y[20],u,a[10],c[20][20],power,r;
clrscr();
printf("enter m,n:");
scanf("%d%d",&m,&n);
for(i=1;i<=n;i++)
{
printf("enter values of x and y");
scanf("%f%f",&x[i],&y[i]);
}
for(j=1;j<=m+1;j++)
for(k=1;k<=m+1;k++)
{
c[j][k]=0;
for(i=1;i<=n;i++)
{
om
power=pow(x[i],j+k-2);
c[j][k]=c[j][k]+power;
r.c
}
}
ke
for(j=1;j<=m+1;j++)
an
{
c[j][m+2]=0;
tR
for(i=1;i<=n;i++)
irs
{
r=pow(x[i],j-1);
.F
c[j][m+2]=c[j][m+2]+y[i]*r;
w
}
w
}
w
for(i=1;i<=m+1;i++)
{
for(j=1;j<=m+2;j++)
{
printf("%.2f\t",c[i][j]);
}
printf("\n");
}
for(k=1;k<=m+1;k++)
for(i=1;i<=m+1;i++)
{
if(i!=k)
{
u=c[i][k]/c[k][k];
for(j=k;j<=m+2;j++)
www.FirstRanker.com 95
www.FirstRanker.com www.FirstRanker.com
{
c[i][j]=c[i][j]-u*c[k][j];
}
}
}
for(i=1;i<=m+1;i++)
{
a[i]=c[i][m+2]/c[i][i];
printf("a[%d]=%f\n",i,a[i]);
}
getch();
}
om
r.c
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 96
www.FirstRanker.com www.FirstRanker.com
WEEK 21
Program:
main()
{
int i,j,k,n;
float term,sum,x1,x[20],f[20];
textcolor(LIGHTCYAN);
clrscr();
printf("enter n value");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the values for x,f[x]");
scanf("%f%f",&x[i],&f[i]);
}
printf("enter the value");
scanf("%f",&x1);
sum=0;
for(k=1;k<=n;k++)
{
term=1;
for(j=1;j<=n;j++)
{
if(j!=k)
om
term=term*((x1-x[j])/(x[k]-x[j]));
}
r.c
sum=sum+term*f[k];
}
ke
printf("%f=%f",x1,sum);
an
getch();
}
tR
irs
.F
w
w
w
www.FirstRanker.com 97
www.FirstRanker.com www.FirstRanker.com
WEEK 22
Program:
#include<math.h>
#include<stdlib.h>
main()
{
int i,j,k,n;
float h,u,f,x[100],fx[100],term,xx;
clrscr();
printf("enter the no.of values");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the values x,fx");
scanf("%f%f",&x[i],&fx[i]);
}
printf("enter the values xx");
scanf("%f",&xx);
h=x[2]-x[1];
u=(xx-x[0])/h;
for(i=1;i<=n;i++)
{
for(j=n-1;j>=i;j--)
om
{
fx[j]=fx[j]-fx[j-1];
r.c
}
}
ke
k=1;f=0;term=1;
an
for(i=0;i<n;i++)
{
tR
f=f+term*fx[i];
irs
term=term*(u-k+1)/k;
k++;
.F
}
w
getch();
w
www.FirstRanker.com 98
www.FirstRanker.com www.FirstRanker.com
WEEK 23
Program:
#include<math.h>
#include<stdio.h>
main()
{
int i,n;
float sum,s1,s2,h,x0,xn,fn,f0;
clrscr();
printf("enter the values x0,xn,n:");
scanf("%f%f%d",&x0,&xn,&n);
s1=s2=0;
h=(xn-x0)/n;
f0=x0*x0;
fn=xn*xn;
s1=f0+fn;
for(i=1;i<=n-1;i++)
{
x0=x0+h;
f0=x0*x0;
s2=s2+f0;
printf("x[%d]=%f\tf[%d]=%f\n",i,x0,i,f0);
}
om
sum=(h*(s1+2*s2))/2;
printf("\tThe intergal value is:%f",sum);
r.c
getch();
}
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 99
www.FirstRanker.com www.FirstRanker.com
WEEK 24
Program:
else
s2=s2+f0;
r.c
printf("x[%d]=%f\t f[%d]=%f\n",i,x0,i,f0);
}
ke
sum=(h*(s0+4*s1+2*s2))/3;
an
}
irs
#include<math.h>
w
#include<stdio.h>
w
main()
w
{
int i,n;
float sum,s0,s1,s2,h,x0,xn,fn,f0;
clrscr();
printf("enter the values x0,xn,n:");
scanf("%f%f%d",&x0,&xn,&n);
s0=s1=s2=0;
h=(xn-x0)/n;
f0=x0*x0;
fn=xn*xn;
s0=f0+fn;
for(i=1;i<=n-1;i++)
{
x0=x0+h;
f0=x0*x0;
if(i%3==0)
www.FirstRanker.com 100
www.FirstRanker.com www.FirstRanker.com
s1=s1+2*f0;
else
s2=s2+3*f0;
printf("x[%d]=%f\t f[%d]=%f\n",i,x0,i,f0);
}
sum=(3*h*(s0+s1+s2))/8;
printf("\tThe intergal value is:%f",sum);
getch();
}
om
r.c
ke
an
tR
irs
.F
w
w
w
www.FirstRanker.com 101