You are on page 1of 47

( B ) Write a C program that uses functions to

perform the following:


LIST OF LAB PROGRAMS i) Addition of Two Matrices
ii) Multiplication of Two Matrices
6 .( A ) ( i ) Write a C program that uses functions to
1 . (A) Write a C program to find the sum of perform the following operations:
individual digits of a positive integer . To insert a sub-string in to given main string from
(B) A Fibonacci Sequence is defined as follows: a given position.
the first and second terms in the sequence are 0 and1 ( ii ) Write a C program that uses functions to
. Subsequent terms are found by adding the perform the following operations:
preceding two terms in the sequence. Write a C To delete n Characters from a given position in a
program to generate the first n terms of the given string.
sequence. ( B ) Write a C program to determine if the given
( C ) Write a C program to generate all the prime string is a palindrome or not
numbers between 1 and n, where n is a value 7 . ( A ) Write a C program that displays the
supplied by the user. position or index in the string S
where the string T begins, or - 1 if S doesn't
2 . ( A ) Write a C program to calculate the contain T.
following Sum: ( B ) Write a C program to count the lines, words
Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10! and characters in a given text*/
( B ) Write a C program toe find the roots of a 8 . ( A )Write a C program to generate Pascal's
quadratic equation. triangle.
( B ) Write a C program to construct a pyramid
3 . (A) Write C programs that use both recursive and of numbers.
non-recursive functions
To find the factorial of a given integer. 9 . Write a C program to read in two numbers, x and
( B ) Write C programs that use both recursive n, and then compute the sum of this geometric
and non-recursive functions progression:
To find the GCD (greatest common divisor) of 1+x+x2+x3+………….+xn For example: if n is 3
two given integers. and x is 5, then the program computes 1+5+25+125.
( C ) Write C programs that use both recursive and Print x, n, the sum Perform error checking. For
non-recursive functions example, the formula does not make sense
To solve Towers of Hanoi problem. for negative exponents - if n is less than 0. Have
4 . (A) The total distance travelled by vehicle in 't' your program print an error message if n<0,
seconds is given by distance = ut+1/2at2 then go back and read in the next pair of numbers of
where 'u' and 'a' are the initial velocity (m/sec.) without computing the sum.
and acceleration (m/sec2). Are any values of x also illegal ? If so, test for them
Write C program to find the distance travelled at too.
regular intervals of time given
the values of 'u' and 'a'. The program should 10 . ( A ) 2’s complement of a number is obtained
provide the flexibility to the user by scanning it from right to left and complementing
to select his own time intervals and repeat the all
calculations for different values of 'u' and 'a'. the bits after the first appearance of a 1. Thus 2’s
(B) Write a C program, which takes two integer complement of 11100 is 00100.
operands and one operator form the user, Write a C program to find the 2’s complement of a
performs the operation and then prints the result. binary number.
(Consider the operators +,-,*, /, % and use Switch ( B ) Write a C program to convert a Roman
Statement) numeral to its decimal equivalent.
5 . ( A ) Write a C program to find both the largest 11 . Write a C program that uses functions to
and smallest number in a list of integers perform the following operations:
i) Reading a complex number
ii) Writing a complex number ( ii ) Write C program that implement the following
iii) Addition of two complex numbers sorting methods
iv) Multiplication of two complex numbers to sort a given list of integers in ascending order: ii)
(Note: represent complex number using a structure.) Quick sort
21 . ( A ) Write C program that implement the
12 . ( A )Write a C program which copies one file to following sorting methods
another. to sort a given list of integers in ascending order: i)
B ) Write a C program to reverse the first n Insertion sort
characters in a file. ( B ) Write C program that implement the following
(Note: The file name and n are specified on the sorting methods to sort a given list of integers in
command line.) ascending order:
13 . ( i ) Write a program to create a linear linked ii) Merge sort
list interactively and print out the list and the total 22 . ( A ) Write C program to implement the
number of items in the list. Lagrange interpolation .
( ii ) Write a function for inserting an item into a ( B ) Write C program to implement the Newton-
linked list Gregory forward interpolation .
14 . Write a C program that uses functions to
perform the following operations on doubly linked 23 . ( A )Write C program to implement the linear
list.: regression algorithm.
i) Creation ii) Insertion iii) Deletion iv) 24 . Write C program to implement Trapezoidal
Traversal in both ways method .
15 . ( i ) Write C programs that implement stack (its Write C program to implement Simpson method.
operations) using i) Arrays
( ii ) Write C programs that implement stack
(its operations) using ii) Pointers
16 . ( i ) Write C programs that implement Queue
(its operations) using i) Arrays WEEK 1 :-
(i i ) Write C programs that implement Queue
(its operations) using ii) Pointers
17 . Write a C program that uses Stack operations to
/* (A) Write a C program to find the sum of
perform the following: individual digits of a positive integer.*/
i) Converting infix expression into postfix
expression #include<stdio.h>
ii) Evaluating the postfix expression #include<conio.h>
18 . Write a C program that uses functions to
perform the following: void main()
i) Creating a Binary Tree of integers {
ii) Traversing the above binary tree in preorder, int num, k=1, sum=0;
inorder and postorder. clrscr();
19 . Write C programs that use both recursive and
printf("Enter the number whose digits are to be
non recursive functions
to perform the following searching operation for a
added:");
Key value in a given list of integers : scanf("%d",&num);
i) Linear search while(num!=0)
Write C programs that use both recursive and non {
recursive functions to perform k=num%10;
the following searching operations for a Key value sum=sum+k;
in a given list of integers : k=num/10;
ii) BInary search. num=k;
20 . Write C programs that implement the following }
sorting methods to sort printf("Sum of the digits:%d",sum);
a given list of integers in ascending order: i)
getch();
Bubble sort

2
} <===========PROGRAM TO FIND THE
O/P:- FIBONACCI SERIES UP TO N NO. IN
Enter the number whose digits are to be SERIES=========>
added:123 tENTER LENGTH OF SERIES (N) 5
Sum of the digits:6 01235
/* ( C ) Write a C program to generate all
/* (B) A Fibonacci Sequence is defined as the prime numbers between 1 and n, where n
follows: the first and second terms in the is a value supplied by the user. */
sequence are 0 and 1. Subsequent
terms are found by adding the preceding two #include <stdio.h>
terms in the sequence. Write a C program to
generate void main()
the first n terms of the sequence. {
*/ int no,counter,counter1,check;
clrscr();
#include <stdio.h> printf("<-----------------------PRIME NO.
SERIES------------------------>");
void main() printf("\n\n\n\t\t\tINPUT THE VALUE OF N:
{ ");
int num1=0, num2=1,no,counter,fab; scanf("%d",&no);
clrscr(); printf("\n\nTHE PRIME NO. SERIES B/W 1
TO %d : \n\n",no);
printf("<===========PROGRAM TO FIND
THE FIBONACCI SERIES UP TO N NO. IN for(counter = 1; counter <= no; counter++)
SERIES=========>"); {
printf("\n\n\n\t\tENTER LENGTH OF SERIES check = 0;
(N) : "); //THIS LOOP WILL CHECK A NO TO BE
scanf("%d",&no); PRIME NO. OR NOT.

printf("\n\n\t\t\t<----FIBONACCI SERIES---- for(counter1 = counter-1; counter1 > 1 ;


>"); counter1--)
printf("\n\n\t\t%d %d",num1,num2); if(counter%counter1 == 0)
{
//LOOP WILL RUN FOR 2 TIME LESS IN check++; // INCREMENT CHECK IF
SERIES AS THESE WAS PRINTED IN NO. IS NOT A PRIME NO.
ADVANCE break;
for(counter = 1; counter <= no-2; counter++) }
{ if(check == 0)
fab=num1 + num2; printf("%d\t",counter);
printf(" %d",fab); }
num1=num2; getch();
num2=fab; }
} Output:
getch(); <-----------------------PRIME NO.
} SERIES------------------------>

O/p:

3
INPUT THE VALUE OF N:5 ENTER VALUE OF X :1
1 2 3 5 7 SUM :0.5403

/* ( B ) Write a C program toe find the roots


of a quadratic equation. */
WEEK 2 :-
#include<stdio.h>
/* ( A ) Write a C program to calculate the #include<conio.h>
following Sum: #include<math.h>
Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!
*/ void main()
#include <stdio.h> {
#include <math.h> float a,b,c,root1,root2;
void main() clrscr();
{ printf("\n Enter values of a,b,c for finding roots
int counter,f_coun; of a quadratic eq:\n");
float sum=0,x,power,fact; scanf("%f%f%f",&a,&b,&c);
clrscr();
/*checking condition*/
printf("<-----------------------PROGRAM FOR if(b*b>4*a*c)
SUM OF EQ. SERIES----------------------->"); {
printf("\n\n\tEQUATION SERIES : 1- X^2/2! + root1=-b+sqrt(b*b-4*a*c)/2*a;
X^4/4! - X^6/6! + X^8/8! - X^10/10!"); root2=-b-sqrt(b*b-4*a*c)/2*a;
printf("\n\n\n\tENTER VALUE OF X : "); printf("\n*****ROOTS ARE*****\n");
scanf("%f",&x); printf("\n root1=%f\n root2=%f",root1,root2);
for(counter=0, power=0; power<=10; counter+ }
+,power=power+2) else
{ printf("\n Imaginary Roots.");
fact=1; getch();
//CALC FACTORIAL OF POWER VALUE }
for(f_coun=power; f_coun>=1; f_coun--) O/p:
fact *= f_coun; Enter values of a,b,c for finding roots of a
//EQ. FOR SUM SERIES quadratic eq:
sum=sum+(pow(- 121
1,counter)*(pow(x,power)/fact)); root1=-1.0000000
} root2=-1.0000000
printf("SUM : %f",sum);
getch(); WEEK 3 :-
}
O/p: /* (A) Write C programs that use both
<---PROGRAM FOR SUM OF EQ. recursive and non-recursive functions
SERIES--> To find the factorial of a given integer.*/
EQUATION SERIES : 1- X^2/2! + X^4/4! -
X^6/6! + X^8/8! - X^10/10! #include<stdio.h>
#include<conio.h>

4
unsigned int recr_factorial(int n); #include<math.h>
unsigned int iter_factorial(int n); unsigned int GcdRecursive(unsigned m,
unsigned n);
void main() unsigned int GcdNonRecursive(unsigned
{ p,unsigned q);
int n,i; int main(void)
long fact; {
clrscr(); int a,b,iGcd;
printf("Enter the number: "); clrscr();
scanf("%d",&n); printf("Enter the two numbers whose GCD is
to be found: ");
if(n==0) scanf("%d%d",&a,&b);
printf("Factorial of 0 is 1\n"); printf("GCD of %d and %d Using Recursive
else Function is %d\n",a,b,GcdRecursive(a,b));
{ printf("GCD of %d and %d Using Non-
printf("Factorial of %d Using Recursive Recursive Function is
Function is %d\n",n,recr_factorial(n)); %d\n",a,b,GcdNonRecursive(a,b));
printf("Factorial of %d Using Non-Recursive getch();
Function is %d\n",n,iter_factorial(n)); }
} /* Recursive Function*/
getch(); unsigned int GcdRecursive(unsigned m,
} unsigned n)
/* Recursive Function*/ {
unsigned int recr_factorial(int n) { if(n>m)
return n>=1 ? n * recr_factorial(n-1) : 1; return GcdRecursive(n,m);
} if(n==0)
/* Non-Recursive Function*/ return m;
unsigned int iter_factorial(int n) { else
int accu = 1; return GcdRecursive(n,m%n);
int i; }
for(i = 1; i <= n; i++) { /* Non-Recursive Function*/
accu *= i; unsigned int GcdNonRecursive(unsigned
} p,unsigned q)
return accu; {
} unsigned remainder;
O/p: remainder = p-(p/q*q);
Enter the number:4
Factorial of 4 Using Recursive Function is 24 if(remainder==0)
Factorial of 4 Using Non-Recursive Function is return q;
24 else
GcdRecursive(q,remainder);
/*( B ) Write C programs that use both }
recursive and non-recursive functions O/p: Enter the two numbers whose GCD is to be
To find the GCD (greatest common divisor) found:3 18
of two given integers.*/ GCD of 3 and 18 Using Recursive Function is
3
#include<stdio.h> GCD of 3 and 18 Using Non-Recursive
#include<conio.h> Function is 3

5
stkdndl[top]=dndl;
stkadd[top]=5;
num=num-1;
temp=sndl;
sndl=indl;
/*( C ) Write C programs that use both recursive indl=temp;
and non-recursive functions dndl=dndl;
To solve Towers of Hanoi problem.*/ goto one;
four:
#include<conio.h> if(top==NULL)
#include<stdio.h> return;
/* Non-Recursive Function*/ num=stkn[top];
void hanoiNonRecursion(int num,char sndl,char sndl=stksndl[top];
indl,char dndl) indl=stkindl[top];
{ dndl=stkdndl[top];
char add=stkadd[top];
stkn[100],stksndl[100],stkindl[100],stkdndl[100 top=top-1;
],stkadd[100],temp; if(add==3)
int top,add; goto three;
top=NULL; else if(add==5)
goto four;
one: }
if(num==1) /* Recursive Function*/
{ void hanoiRecursion( int num,char ndl1, char
printf("\nMove top disk from needle ndl2, char ndl3)
%c to needle %c ",sndl,dndl); {
goto four; if ( num == 1 ) {
} printf( "Move top disk from needle %c
two: to needle %c.", ndl1, ndl2 );
top=top+1; return;
stkn[top]=num; }
stksndl[top]=sndl; hanoiRecursion( num - 1,ndl1, ndl3, ndl2 );
stkindl[top]=indl; printf( "Move top disk from needle %c to
stkdndl[top]=dndl; needle %c.", ndl1, ndl2 );
stkadd[top]=3; hanoiRecursion( num - 1,ndl3, ndl2, ndl1 );
num=num-1; }
sndl=sndl; void main()
temp=indl; {
indl=dndl; int no;
dndl=temp; clrscr();
goto one; printf("Enter the no. of disks to be transferred:
three: ");
printf("\nMove top disk from needle %c scanf("%d",&no);
to needle %c ",sndl,dndl);
top=top+1; if(no<1)
stkn[top]=num; printf("\nThere's nothing to move.");
stksndl[top]=sndl; else
stkindl[top]=indl; printf("Non-Recursive");

6
hanoiNonRecursion(no,'A','B','C'); distance += (velos*time +
printf("\nRecursive"); (accl*pow(time,2))/2);
hanoiRecursion(no,'A','B','C'); }
getch(); printf("\n\n\n\tTOTAL DISTANCE
} TRAVELLED BY VEHICLE IN %d
INTERVALS OF TIME :
WEEK 4 :- %f",tim_intrval,distance);
getch();
}
/* (A) The total distance travelled by vehicle in
't' seconds is given by distance = ut+1/2at2 Output : -
where 'u' and 'a' are the initial velocity ==PROGRAM FOR CALC TOTAL
(m/sec.) and acceleration (m/sec2). DISTANCE TRAVELED BY A
Write C program to find the distance VECHIAL====
travelled at regular intervals of time given NO OF TIME INTERVALS :
the values of 'u' and 'a'. The program should TIME(sec) :
provide the flexibility to the user ACCLERATION AT
to select his own time intervals and repeat TOTAL DISTANCE TRAVELLED BY
the calculations for different values of 'u' and 'a'. VEHICLE IN %d INTERVALS OF TIME :
*/
/* (B) Write a C program, which takes two
#include <stdio.h> integer operands and one operator form the
#include <math.h> user,
performs the operation and then prints the
void main() result.
{ (Consider the operators +,-,*, /, % and use
int tim_intrval, counter,time; Switch Statement)*/
float accl, distance=0, velos;
clrscr(); #include<stdio.h>
printf("<===========PROGRAM FOR #include<conio.h>
CALC TOTAL DISTANCE TRAVELED BY
A VECHIAL===========>"); void main()
printf("\n\n\n\t\t\tNO OF TIME INTERVALS : {
"); int a,b,res,ch;
scanf("%d",&tim_intrval); clrscr();
printf("\t *********************");
for(counter = 1; counter <= tim_intrval; printf("\n\tMENU\n");
counter++) printf("\t********************");
{ printf("\n\t(1)ADDITION");
printf("\n\t\t\tAT T%d TIME(sec) : printf("\n\t(2)SUBTRACTION");
",counter); printf("\n\t(3)MULTIPLICATION");
scanf("%d",&time); printf("\n\t(4)DIVISION");
printf("\t\t\tVELOCITY AT %d sec printf("\n\t(5)REMAINDER");
(m/sec) : ",time); printf("\n\t(0)EXIT");
scanf("%f",&velos); printf("\n\t********************");
printf("\t\t\tACCLERATION AT %d sec printf("\n\n\tEnter your choice:");
(m/sec^2): ",time); scanf("%d",&ch);
scanf("%f",&accl);

7
if(ch<=5 & ch>0) ********************
{ (1)ADDITION
printf("Enter two numbers:\n"); (2)SUBTRACTION
scanf("%d%d",&a,&b); (3)MULTIPLICATION
} (4)DIVISION
(5)REMAINDER
switch(ch) (0)EXIT
{ ********************
case 1:
res=a+b; Enter your choice: 1
printf("\n Addition:%d",res);
break; Enter two numbers: 1 2
Add : 3 .
case 2:
res=a-b; WEEK 5 :-
printf("\n Subtraction:%d",res);
break;
/* ( A ) Write a C program to find both the
case 3: largest and smallest number in a list of
res=a*b; integers*/
printf("\n Multiplication:%d",res);
break;
main( )
case 4: {
res=a/b; float largest(float a[ ], int n);
printf("\n Division:%d",res); float value[4] = {2.5,-
break; 4.75,1.2,3.67};
printf("%f\n", largest(value,4));
case 5: }
res=a%b; float largest(float a[], int n)
printf("\n Remainder:%d",res); {
break; int i;
float max;
case 0: max = a[0];
printf("\n Choice Terminated"); for(i = 1; i < n; i++)
exit(); if(max < a[i])
break; max = a[i];
return(max);
default: }
printf("\n Invalid Choice");
}
getch(); Output : max : 3.67 .
} Min : -4.75 .

Output : -
******************** /* ( B ) Write a C program that uses
MENU functions to perform the following:

8
i) Addition of Two Matrices }
ii) Multiplication of Two Matrices printf("\n =====Matrix Addition=====\n");
*/ for(i=0;i<r1;i++)
{
#include<stdio.h> for(j=0;j<c1;j++)
printf("%5d",a[i][j]+b[i][j]);
void main() printf("\n");
{ }
int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10] break;
[10],c[10][10];
clrscr(); case 2:
printf("********************************* printf("Input rows and columns of A matrix:");
***"); scanf("%d%d",&m,&n);
printf("\n\t\tMENU"); printf("Input rows and columns of B matrix:");
printf("\n******************************* scanf("%d%d",&p,&q);
***"); if(n==p)
printf("\n[1]ADDITION OF TWO {
MATRICES"); printf("matrices can be multiplied\n");
printf("\n[2]MULTIPLICATION OF TWO printf("resultant matrix is %d*%d\n",m,q);
MATRICES"); printf("Input A matrix\n");
printf("\n[0]EXIT"); read_matrix(a,m,n);
printf("\n******************************* printf("Input B matrix\n");
***"); /*Function call to read the matrix*/
printf("\n\tEnter your choice:\n"); read_matrix(b,p,q);
scanf("%d",&ch); /*Function for Multiplication of two matrices*/
printf("\n =====Matrix
if(ch<=2 & ch>0) Multiplication=====\n");
{ for(i=0;i<m;++i)
printf("Valid Choice\n"); for(j=0;j<q;++j)
} {
c[i][j]=0;
switch(ch) for(k=0;k<n;++k)
{ c[i][j]=c[i][j]+a[i][k]*b[k][j];
case 1: }
printf("Input rows and columns of A & B
Matrix:"); printf("Resultant of two matrices:\n");
scanf("%d%d",&r1,&c1); write_matrix(c,m,q);
printf("Enter elements of matrix A:\n"); }
for(i=0;i<r1;i++) /*end if*/
{ else
for(j=0;j<c1;j++) {
scanf("%d",&a[i][j]); printf("Matrices cannot be multiplied.");
} }
printf("Enter elements of matrix B:\n"); /*end else*/
for(i=0;i<r1;i++) break;
{
for(j=0;j<c1;j++) case 0:
scanf("%d",&b[i][j]); printf("\n Choice Terminated");

9
exit();
break; WEEK 6 :-

default:
printf("\n Invalid Choice"); /*( A ) ( i ) Write a C program that uses
} functions to perform the following
getch(); operations:
} To insert a sub-string in to given main
string from a given position.
/*Function read matrix*/ */
int read_matrix(int a[10][10],int m,int n)
{ #include <stdio.h>
int i,j; #include <conio.h>
for(i=0;i<m;i++) #include <string.h>
for(j=0;j<n;j++)
scanf("%d",&a[i][j]); void main()
return 0; {
} char a[10];
char b[10];
/*Function to write the matrix*/ char c[10];
int write_matrix(int a[10][10],int m,int n) int p=0,r=0,i=0;
{ int t=0;
int i,j; int x,g,s,n,o;
for(i=0;i<m;i++) clrscr();
{
for(j=0;j<n;j++) puts("Enter First String:");
printf("%5d",a[i][j]); gets(a);
printf("\n"); puts("Enter Second String:");
} gets(b);
return 0; printf("Enter the position where the item has to
} be inserted: ");
Output : - scanf("%d",&p);
r = strlen(a);
********************************** n = strlen(b);
MENU i=0;
**********************************
[1]ADDITION OF TWO MATRICES // Copying the input string into another array
[2]MULTIPLICATION OF TWO MATRICES while(i <= r)
[0]EXIT {
********************************** c[i]=a[i];
Enter your choice: 1 i++;
Enter the elementes of a matrix : 1 1 }
11 s = n+r;
Enter the elements of b matrix : 1 1 o = p+n;
11
// Adding the sub-string
Addition is : 2 2 for(i=p;i<s;i++)
22 {

10
x = c[i]; delchar(string, n,pos);
if(t<n) getch();
{ }
a[i] = b[t];
t=t+1; // Function to delete n characters
} void delchar(char *x,int a, int b)
a[o]=x; {
o=o+1; if ((a+b-1) <= strlen(x))
} {
strcpy(&x[b-1],&x[a+b-1]);
printf("%s", a); puts(x);
getch(); }
} }

Output : -
Enter First String: science output : -
Enter Second String:tist Enter the string : computer
Enter the position where the item has to be Enter the position from where to delete : 5
inserted: 5 Enter the number of characters to be deleted : 2
String is: scientist . String is : comput .

/*( ii ) Write a C program that uses functions /* ( B ) Write a C program to determine if


to perform the following operations: the given string is a palindrome or not */
To delete n Characters from a given
position in a given string. #include<stdio.h>
*/ #include<string.h>
enum Boolean{false,true};
#include <stdio.h> enum Boolean IsPalindrome(char string[])
#include <conio.h> {
#include <string.h> int left,right,len=strlen(string);
enum Boolean matched=true;
void delchar(char *x,int a, int b); if(len==0)
return 0;
void main() left=0;
{ right=len-1;
char string[10]; /* Compare the first and last letter,second &
int n,pos,p; second last & so on */
clrscr(); while(left<right&&matched)
{
puts("Enter the string"); if(string[left]!=string[right])
gets(string); matched=false;
printf("Enter the position from where to else
delete"); {
scanf("%d",&pos); left++;
printf("Enter the number of characters to be right--;
deleted"); }
scanf("%d",&n); }

11
return matched; /* Entering the string whose position or index to
} be displayed */
int main() puts("Enter the string to be searched: ");
{ gets(t);
char string[40];
clrscr(); /*Searching string t in string s */
printf("****Program to test if the given string found=strstr(s,t);
is a palindrome****\n"); if(found)
printf("Enter a string:"); printf("Second String is found in the First
scanf("%s",string); String at %d position.\n",found-s);
if(IsPalindrome(string)) else
printf("The given string %s is a printf("-1");
palindrome\n",string); getch();
else }
printf("The given string %s is not a Output : -
palindrome\n",string); Enter the first string: computer
getch(); Enter the second string: puter .
return 0; Second String is found in the First String at 3
} position.
Output :-
String : madam .
The given string madam is palindrome . /* ( B ) Write a C program to count the lines,
words and characters in a given text*/

WEEK 7 :- Program
#include <stdio.h>
main()
/*( A ) Write a C program that displays the {
position or index in the string S char line[81], ctr;
where the string T begins, or - 1 if S doesn't int i,c,
contain T. end = 0,
*/ characters = 0,
words = 0,
#include<stdio.h> lines = 0;
#include<string.h> printf("KEY IN THE TEXT.\n");
#include<conio.h> printf("GIVE ONE SPACE AFTER
EACH WORD.\n");
void main() printf("WHEN COMPLETED, PRESS
{ 'RETURN'.\n\n");
char s[30], t[20]; while( end == 0)
char *found; {
clrscr(); /* Reading a line of text */
c = 0;
/* Entering the main string */ while((ctr=getchar()) != '\n')
puts("Enter the first string: "); line[c++] = ctr;
gets(s); line[c] = '\0';
/* counting the words in a line */
if(line[0] == '\0')

12
break ; void main()
else {
{ int bin,p,q,r,x;
words++; clrscr();
for(i=0; line[i] != '\0';i++) bin=1;
if(line[i] q=0;
== ' ' || line[i] == '\t')
printf("Rows you want to input:");
words++; scanf("%d",&r);
}
/* counting lines and characters printf("\nPascal's Triangle:\n");
*/
lines = lines +1; while(q<r)
characters = characters + {
strlen(line); for(p=40-3*q;p>0;--p)
} printf(" ");
printf ("\n"); for(x=0;x<=q;++x)
printf("Number of lines = %d\n", lines); {
printf("Number of words = %d\n", if((x==0)||(q==0))
words); bin=1;
printf("Number of characters = %d\n", else
characters); bin=(bin*(q-x+1))/x;
} printf("%6d",bin);
Output }
KEY IN THE TEXT.
GIVE ONE SPACE AFTER EACH WORD. printf("\n");
WHEN COMPLETED, PRESS 'RETURN'. ++q;
Admiration is a very short-lived passion. }
Admiration involves a glorious obliquity of getch();
vision. }
Always we like those who admire us but we do
not Output : -
like those whom we admire.
Fools admire, but men of sense approve. Enter the length 2 .
Number of lines = 5
Number of words = 36 1
Number of characters = 205 121

WEEK 8 :-
/*( B ) Write a C program to construct a
/* ( A )Write a C program to generate pyramid of numbers. */
Pascal's triangle. */
#include<stdio.h>
#include<stdio.h> #include<conio.h>
#include<conio.h>
void main()

13
{
int num,i,y,x=35; #include<stdio.h>
clrscr(); #include<conio.h>
printf("\nEnter the number to generate the #include<math.h>
pyramid:\n");
scanf("%d",&num); void main()
{
for(y=0;y<=num;y++) int s_sum,i,x,n;
{
/*(x-coordinate,y-coordinate)*/ clrscr();
gotoxy(x,y+1); printf("Enter the values for x and n:");
scanf("%d %d",&x,&n);
/*for displaying digits towards the left and
right of zero*/ if(n<=0 || x<=0)
for(i=0-y;i<=y;i++) {
printf("Value is not valid\n");
printf("%3d",abs(i)); }
x=x-3; else
} {
getch(); printf("Value is valid\n");
} s_sum=1;
for(i=1;i<=n;i++)
Output :- {
Enter the number to generate the pyramid: 3 s_sum=s_sum+pow(x,i);
}
1 printf("Sum of series=%d\n",s_sum);
23 }
4567 getch();
}

WEEK 9 :- Output :-
Enter the values for x and n: 3 5
/*Write a C program to read in two numbers, Sum of series is :156 .
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 WEEK 10 :-
1+5+25+125.
Print x, n, the sum Perform error checking. /* ( A ) 2’s complement of a number is
For example, the formula does not make obtained by scanning it from right to left and
sense complementing all
for negative exponents - if n is less than 0. the bits after the first appearance of a 1. Thus
Have your program print an error message if 2’s complement of 11100 is 00100.
n<0, Write a C program to find the 2’s
then go back and read in the next pair of complement of a binary number.*/
numbers of without computing the sum.
Are any values of x also illegal ? If so, test for #include <stdio.h>
them too. */ #include<conio.h>

14
{
void complement (char *a); b[i]='1';
void main() c=0;
{ }
char a[16]; else if (c==1 && b[i]=='1')
int i; {
clrscr(); b[i]='0';
printf("Enter the binary number"); c=1;
gets(a); }
for(i=0;a[i]!='\0'; i++) }
{ }
if (a[i]!='0' && a[i]!='1') b[l]='\0';
{ printf("The 2's complement is %s", b);
printf("The number entered is not a binary }
number. Enter the correct number");
exit(0); Output :-
} Enter the binary number : 11100
} The 2’s comliment is : 00100 .
complement(a);
getch();
}
void complement (char *a)
{ /*( B ) Write a C program to convert a
int l, i, c=0; Roman numeral to its decimal equivalent. */
char b[16]; #include<string.h>
l=strlen(a); #include<stdlib.h>
for (i=l-1; i>=0; i--) void main()
{ {
if (a[i]=='0') int *a,len,i,j,k;
b[i]='1'; char *rom;
else clrscr();
b[i]='0'; printf("Enter the Roman Numeral:");
} scanf("%s",rom);
for(i=l-1; i>=0; i--) len=strlen(rom);
{ for(i=0;i<len;i++)
if(i==l-1) {
{ if(rom[i]=='I')
if (b[i]=='0') a[i]=1;
b[i]='1'; else if(rom[i]=='V')
else a[i]=5;
{ else if(rom[i]=='X')
b[i]='0'; a[i]=10;
c=1; else if(rom[i]=='L')
} a[i]=50;
} else if(rom[i]=='C')
else a[i]=100;
{ else if(rom[i]=='D')
if(c==1 && b[i]=='0') a[i]=500;

15
else if(rom[i]=='M') printf("\n\n \t\t\t***** MAIN MENU
a[i]=1000; *****");
else printf("\n\n Select your option: \n 1 : ADD\n
{ 2 : MULTIPLY\n 0 : EXIT \n\n\t\t Enter your
printf("\nInvalid Value"); Option [ ]\b\b");
getch();
exit(0); }} scanf("%d",&opern);
k=a[len-1];
for(i=len-1;i>0;i--) switch(opern)
{ {
if(a[i]>a[i-1]) case 0:
k=k-a[i-1]; exit(0);
else if(a[i]==a[i-1] || a[i]<a[i-1]) case 1:
k=k+a[i-1]; } case 2:
printf("\nIts Decimal Equivalent is:"); arithmetic(opern);
printf("%d",k); default:
getch(); main();
} }
Output :-
Enter the Roman Numeral:IV }
Its decimal number is : 4.
WEEK 11 :- void arithmetic(int opern)
{
/* Write a C program that uses functions to struct comp w1, w2, w;
perform the following operations:
i) Reading a complex number printf("\n Enter two Complex Numbers
ii) Writing a complex number (x+iy):\n Real Part of First Number:");
iii) Addition of two complex numbers scanf("%lf",&w1.realpart);
iv) Multiplication of two complex printf("\n Imaginary Part of First Number:");
numbers scanf("%lf",&w1.imgpart);
(Note: represent complex number using a printf("\n Real Part of Second Number:");
structure.) */ scanf("%lf",&w2.realpart);
printf("\n Imaginary Part of Second
#include<stdio.h> Number:");
#include<math.h> scanf("%lf",&w2.imgpart);
switch(opern)
void arithmetic(int opern); {
/*addition of complex number*/
struct comp case 1:
{ w.realpart = w1.realpart+w2.realpart;
double realpart; w.imgpart = w1.imgpart+w2.imgpart;
double imgpart; break;
}; /*multiplication of complex number*/
case 2:
void main() w.realpart=(w1.realpart*w2.realpart)-
{ (w1.imgpart*w2.imgpart);
int opern; w.imgpart=(w1.realpart*w2.imgpart)+
clrscr(); (w1.imgpart*w2.realpart);

16
break; exit(0);
} if (w.imgpart>0) }
printf("\n Answer = %lf+ ft = fopen(argv[2],"w");
%lfi",w.realpart,w.imgpart); if (ft==NULL)
else {
printf("\n Answer = %lf puts("Target file cannot be opened.");
%lfi",w.realpart,w.imgpart); fclose(fs);
getch(); exit(0);
main(); }
} while(1)
{
Output :- ch=fgetc(fs);
***** MAIN MENU *****"); if (ch==EOF)
Select your option: break;
1 : ADD else
2 : MULTIPLY fputc(ch,ft);
0 : EXIT }
Enter your Option [ ] : 1 fclose(fs);
fclose(ft);
Enter two Complex Numbers (x+iy): getch();
Real Part of First Number :1 }
Imaginary Part of First Number :1
Real Part of Second Number : 1
Imaginary Part of Second Number : 1
/*( B ) Write a C program to reverse the first
Addition is 2+i2 . n characters in a file.
(Note: The file name and n are specified on
the command line.)*/
WEEK 12 :-
/* ( A )Write a C program which copies one #include <stdio.h>
file to another.*/ #include <conio.h>
#include <stdio.h> #include <string.h>
#include <conio.h> #include <process.h>
#include <process.h>
void main(int argc, char *argv[]) void main(int argc, char *argv[])
{ {
FILE *fs,*ft; char a[15];
char ch; char s[20];
clrscr(); char n;
if(argc!=3) int k;
{ int j=0;
puts("Invalid number of arguments."); int i;
exit(0); int len;
} FILE *fp;
fs = fopen(argv[1],"r");
if(fs==NULL) if(argc!=3)
{ {
puts("Source file cannot be opened."); puts("Improper number of arguments.");

17
exit(0); printf(head);
} printf("\n");
fp = fopen(argv[1],"r"); printf("\nNumber of items = %d \n",
if(fp == NULL) count(head));
{ }
puts("File cannot be opened."); void create(node *list)
exit(0); {
} printf("Input a number\n");
printf("(type -999 at end): ");
k=*argv[2]-48; scanf("%d", &list -> number); /* create
n = fread(a,1,k,fp); current node */
a[n]='\0';
len=strlen(a); if(list->number == -999)
for(i=len-1;i>=0;i--) {
{ list->next = NULL;
s[j]=a[i]; }
printf("%c",s[j]);
j=j+1; else /*create next node */
} {
s[j+1]='\0'; list->next = (node
getch(); *)malloc(sizeof(node));
} create(list->next); */ Recursion
occurs */
}
WEEK 13 :- return;
/* ( i ) Write a program to create a linear }
linked list interactively and print out the list void print(node *list)
and the total number of items in the list. */ {
#include <stdio.h> if(list->next != NULL)
#include <stdlib.h> {
#define NULL 0 printf("%d-->",list
struct linked_list ->number); /* print current item */
{
int number; if(list->next->next == NULL)
struct linked_list *next; printf("%d", list->next-
}; >number);
typedef struct linked_list node; /* node type
defined */ print(list->next); /*
move to next item */
main() }
{ return;
node *head; }
void create(node *p);
int count(node *p); int count(node *list)
void print(node *p); {
head = (node *)malloc(sizeof(node)); if(list->next == NULL)
create(head); return (0);
printf("\n"); else

18
return(1+ count(list->next)); scanf("%d", &x);
} printf("Value of key item ? (type -999 if
last) ");
scanf("%d", &key);
Output
if(head->number == key) /*
Input a number new node is first */
(type -999 to end); 60 {
Input a number new = (node *)malloc(size of(node));
(type -999 to end); 20 new->number = x;
Input a number new->next = head;
(type -999 to end); 10 head = new;
Input a number }
(type -999 to end); 40 else /* find key node and
Input a number insert new node */
(type -999 to end); 30 { /* before the key
Input a number node */
(type -999 to end); 50 n1 = find(head, key); /* find
Input a number key node */
(type -999 to end); -999 if(n1 == NULL)
printf("\n key is not found \n");
60 -->20 -->10 -->40 -->30 -->50 --> -999 else /* insert new node
*/
Number of items = 6 {
new = (node *)malloc(sizeof(node));
new->number = x;
new->next = n1->next;
n1->next = new;
} }
return(head);
}
node *find(node *lists, int key)
{
if(list->next->number == key)
/*( ii ) Write a function for inserting an item /* key found */
into a linked list */ return(list);
node *insert(node *head) else
{ if(list->next->next == NULL)
node *find(node *p, int a); /* end */
node *new; /* pointer to new return(NULL);
node */ else
node *n1; /* pointer to node find(list->next, key);
preceding key node */ }
int key;
int x; /* new item
(number) to be inserted */ /*( i i i ) Write a function for deleting an item
from linked list */
printf("Value of new item?");

19
/* ( iv ) Write a program for creation of sorted
node *delete(node *head) list from a given list of numbers */
{
node *find(node *p, int a);
int key; /* item to be #include <stdio.h>
deleted */ #include <stdlib.h>
node *n1; /* pointer to node #define NULL 0
preceding key node */
node *p; /* temporary struct linked_list
pointer */ {
printf("\n What is the item (number) to int number;
be deleted?"); struct linked_list *next;
scanf("%d", &key); };
if(head->number == key) /* first typedef struct linked_list node;
node to be deleted) */
{ main ()
p = head->next; /* {
pointer to 2nd node in list */ int n;
free(head); /* release node *head = NULL;
space of key node */ void print(node *p);
head = p; /* make node *insert_Sort(node *p, int n);
head to point to 1st node */
} printf("Input the list of numbers.\n");
else printf("At end, type -999.\n");
{ scanf("%d",&n);
n1 = find(head, key);
if(n1 == NULL) while(n != -999)
printf("\n key not found \n"); {
else /* if(head == NULL) /* create
delete key node */ 'base' node */
{ {
p = n1->next->next; head = (node
/* pointer to the node *)malloc(sizeof(node));
head ->number = n;
following the keynode */ head->next = NULL;

free(n1->next); }
/* free key node */
n1->next = p; else /*
/* establish link */ insert next item */
} {
} head =
return(head); insert_sort(head,n);
} }
/* USE FUNCTION find() HERE */ scanf("%d", &n);
}
printf("\n");
print(head);

20
print("\n"); }
}
node *insert_sort(node *list, int x) Output
{ Input the list of number.
node *p1, *p2, *p; At end, type -999.
p1 = NULL; 80 70 50 40 60 -999
p2 = list; /* p2 points to first node */ 40-->50-->60-->70-->80 -->NULL
Input the list of number.
for( ; p2->number < x ; p2 = p2->next) At end, type -999.
{ 40 70 50 60 80 -999
p1 = p2; 40-->50-->60-->70-->80-->NULL

if(p2->next == NULL)
{
p2 = p2->next;
/* p2 set to NULL */ WEEK 14:-
break;
/* insert new node at end */
} /* Write a C program that uses functions to
} perform the following operations on doubly
linked list.:
/* key node found */ i) Creation ii) Insertion iii) Deletion
p = (node iv) Traversal in both ways
*)malloc(sizeof(node)); /* space for new node */
*/
p->number = x; /* place value #include "stdio.h"
in the new node */ #include "alloc.h"
p->next = p2; /* link new
node to key node */ typedef struct dubll
if (p1 == NULL) {
list int data;
= p; /* new node becomes the first node */ struct dubll *leftlink,*rightlink;
else }*DUBLL;
p1-
>next = p; /* new node inserted after 1st node DUBLL high,temp_node,low,last,pntr;
*/ int flag=0;
return (list);
} DUBLL NodeAlloc();
void print(node *list) DUBLL Search(int,int);
{
if (list == NULL) void CreateItem();
printf("NULL"); void AppendItem();
else void PrintItem();
{ void DeleteItem();
printf("%d-->",list->number); DUBLL Search(int item,int flag);
print(list->next); DUBLL NodeAlloc();
} void InsertItem();
return;

21
void main(void) }
{ getch();
int choice,Item; break;
high=NULL; case 6:
while(1) InsertItem();
{ break;
clrscr(); case 7:
printf("\n \t\t\t***** M A I N M E N U exit();
*****\n\n"); default:
printf("\n 1: Create Linked List \n 2: Append a puts("Invalid choice.");
Node to the List \n 3: Traverse the List \n 4: puts("\nPress any key to go back to main
Delete a Node from the List \n 5: Search a menu.");
Node \n 6: Insert a Node to the List \n 7: getch();
Close \n\n\t\t Enter your Option [ ]\b\b"); break;
scanf("%d",&choice); }
switch(choice) }
{ }
case 1:
CreateItem(); /* Function to Create the list*/
puts("\nPress any key to go back to main void CreateItem()
menu."); {
getch(); if(high==NULL)
break; {
case 2: printf("\n --Creating the list--");
AppendItem(); temp_node=NodeAlloc();
break; printf("\n Enter starting data (as integer
case 3: value) :");
PrintItem(); scanf("%d",&temp_node->data);
puts("\nPress any key to go back to main high=temp_node;
menu."); }
getch(); else{ printf("\n List already created @ %d
break; with %d as data.",high,high->data);}
case 4: }
DeleteItem();
break; /* Function to Append items to the list*/
case 5: void AppendItem()
printf("Find an Item: "); {
scanf("%d",&Item); low=high;
temp_node=Search(Item,0); if(high==NULL)
if(temp_node) {
{ CreateItem();
puts("The item is available in the }
Linked List."); else
} {
else temp_node=NodeAlloc();
{ printf("\n Enter Item (in integer) :");
puts("The item is not found in the Linked scanf("%d",&temp_node->data);
List."); temp_node->rightlink=NULL;

22
void DeleteItem()
while(low->rightlink!=NULL) {
low=low->rightlink; int value;
low->rightlink=temp_node; DUBLL temp_node;
temp_node->leftlink=low; if(high==NULL)
last=low->rightlink; {
printf("\n List is not available. Please create a
} list first.");
} getch();
CreateItem();
/* Function to Traverse the list both ways and }
print the data*/ printf("\n Item to delete :");
void PrintItem() scanf("%d",&value);
{ pntr=Search(value,1);
DUBLL temp_node; pntr->leftlink->rightlink=pntr->rightlink;
if(high==NULL) pntr->rightlink->leftlink=pntr->leftlink;
{ temp_node=pntr;
printf("\n List is not available. Please create a free(temp_node);
list first."); }
getch();
CreateItem(); /* Function to Search an item from the list*/
} DUBLL Search(int item,int flag)
temp_node=high; {
last=low->rightlink; temp_node = high;
printf("\n--Printing The List In Forward if(high==NULL)
direction--\n"); {
printf("\n List is not available. Please create a
while(temp_node!=NULL) //In list first.");
forward direction getch();
{ CreateItem();
printf("\t %d",temp_node->data); }
temp_node = temp_node->rightlink; while(temp_node!=NULL)
} {
printf("\n"); if(temp_node->data==item )
printf("\n--Printing The List In Backward {
direction--\n"); if(flag==0)
temp_node=high; {
if(temp_node->rightlink==NULL) return(1);
{printf("%d",temp_node->data);return; } }
while(last!=NULL) //In else
backward direction {
{ return(temp_node);
printf("\t %d",last->data); }
last = last->leftlink; }
} temp_node=temp_node->rightlink;
} }
}
/* Function to Delete items of the list*/

23
/* Function to Allocate nodes*/
DUBLL NodeAlloc()
{
DUBLL tmep_node;
tmep_node=malloc(sizeof(struct dubll));
if(tmep_node==NULL)
{ WEEK 15:-
printf("\n No memory available. Node
allocation cannot be done.");
} /* ( i ) Write C programs that implement stack
tmep_node->rightlink=tmep_node- (its operations) using i) Arrays */
>leftlink=NULL;
return(tmep_node); #include<stdio.h>
} #include<conio.h>

/* Function to Insert items in the middle of the int st_arr[20];


list*/ int t=-1;
void InsertItem()
{ void push_ele(int ele);
int node; int pop_ele();
DUBLL temp_node; void display_ele();

if(high==NULL) void main()


{ {
printf("\n List is not available. Please create a char choice,num1=0,num2=0;
list first."); while(1)
getch(); {
CreateItem(); clrscr();
} printf("===========================
temp_node=NodeAlloc(); ===========");
printf("Position At which node to be inserted: printf("\n\t\t MENU ");
___ & New Item Value: ___ "); printf("\n==========================
scanf("%d",&node); ============");
scanf("%d",&temp_node->data); printf("\n[1] Using Push Function");
pntr=Search(node,1); printf("\n[2] Using Pop Function");
printf("\n[3] Elements present in Stack");
if(pntr->rightlink==NULL){printf("\n The printf("\n[4] Exit\n");
operation is not possible."); getch();return;} printf("\n\tEnter your choice: ");
temp_node->leftlink=pntr; fflush(stdin);
//creating link to new node scanf("%c",&choice);
temp_node->rightlink=pntr->rightlink;
switch(choice-'0')
pntr->rightlink->leftlink=temp_node; {
pntr->rightlink=temp_node;
case 1:
printf("\n Item has been Inserted."); {
getch(); printf("\n\tElement to be pushed: ");
} scanf("%d",&num1);

24
push_ele(num1); {
break; printf("\n\tSTACK is Empty.\n");
} getch();
exit(1);
case 2: }
{ return(st_arr[t--]);
num2=pop_ele(1); }
printf("\n\tElement to be popped:
%d\n\t",num2); /*Implementing display() function. */
getch(); void display_ele()
break; {
} int k;
printf("\n\tElements present in the stack
case 3: are:\n\t");
{ for(k=0;k<=t;k++)
display_ele(); printf("%d\t",st_arr[k]);
getch(); }
break;
}

case 4: /* ( i ) Write C programs that implement


exit(1); stack (its operations) using ii) Pointers */
break;
#include<stdio.h>
default: #include<conio.h>
printf("\nYour choice is invalid.\n");
break; struct st_point
} {
} int ele;
} struct st_point *l;
}
/*Implementing the push() function. */
void push_ele(int ele) *t;
{ int i;
if(t==99)
{ void push_ele(int j);
printf("STACK is Full.\n"); int pop_ele();
getch(); void display_ele();
exit(1);
} void main()
st_arr[++t]=ele; {
} char choice,num1=0,num2=0;
int i;
/*Implementing the pop() function. */ while(1)
int pop_ele() {
{ clrscr();
int ele1; printf("===========================
if(t==-1) ===========");

25
printf("\n\t\t MENU "); }
printf("\n==========================
============"); /*Inserting the elements using push function*/
printf("\n[1] Using Push Function"); void push_ele(int j)
printf("\n[2] Using Pop Function"); {
printf("\n[3] Elements present in Stack"); struct st_point *m;
printf("\n[4] Exit\n"); m=(struct st_point*)malloc(sizeof(struct
printf("\n\tEnter your choice: "); st_point));
fflush(stdin); m->ele=j;
scanf("%c",&choice); m->l=t;
t=m;
switch(choice-'0') return;
{ }
case 1:
{ /*Removing the elements using pop function*/
printf("\n\tElement to be pushed:"); int pop_ele()
scanf("%d",&num1); {
push_ele(num1); if(t==NULL)
break; {
} printf("\n\STACK is Empty.");
getch();
case 2: exit(1);
{ }
num2=pop_ele(1); else
printf("\n\tElement to be popped: {
%d\n\t",num2); int i=t->ele;
getch(); t=t->l;
break; return (i);
} }
return 0;
case 3: }
{
printf("\n\tElements present in the stack /*Displaying the elements */
are:\n\t"); void display_ele()
display_ele(); {
getch(); struct st_point *pointer=NULL;
break; pointer=t;
} while(pointer!=NULL)
{
case 4: printf("%d\t",pointer->ele);
exit(1); pointer=pointer->l;
break; }
}
default:
printf("\nYour choice is invalid.\n");
break;
} WEEK 16:-
}

26
/*( i ) Write C programs that implement printf("\n\n\t Enter your choice: ");
Queue (its operations) usingi) Arrays */ scanf("%d",&ch);

#include<stdio.h> switch(ch)
#include<alloc.h> {
#include<conio.h> case 1:
#define size 10 {
#define true 1 printf("\nElement to be inserted:");
#define false 0 scanf("%d",&ele);
add_ele(queue,ele);
struct q_arr break;
{ }
int f,r;
int num; case 2:
int a[size]; {
}; if(!e_que(queue))
{
void init(struct q_arr* queue); k=rem_ele(queue);
int e_que(struct q_arr* queue); printf("\n%d element is removed\n",k);
int f_que(struct q_arr* queue); getch();
int add_ele(struct q_arr* queue,int); }
int rem_ele(struct q_arr* queue); else
void display_ele(struct q_arr* queue); {
printf("\tQueue is Empty. No element
/*main function*/ can be removed.");
void main() getch();
{ }
int ele,k; break;
int ch; }

struct q_arr *queue = (struct case 3:


q_arr*)malloc(sizeof(struct q_arr)); {
init(queue); display_ele(queue);
getch();
while(1) break;
{ }
clrscr();
printf("\n\n****IMPLEMENTATION OF case 4:
QUEUE USING ARRAYS****\n"); exit(0);
printf("===========================
================="); default:
printf("\n\t\tMENU\n"); printf("\tInvalid Choice.");
printf("=========================== getch();
================="); break;
printf("\n\t[1] To insert an element"); }
printf("\n\t[2] To remove an element"); }
printf("\n\t[3] To display all the elements"); }
printf("\n\t[4] Exit"); /*end main*/

27
}
void init(struct q_arr* queue)
{ /* Function to display the queue*/
queue->f = 0; void display_ele(struct q_arr* queue)
queue->r = -1; {
queue->num = 0; int j;
} if(e_que(queue))
{
/* Function to check is the queue is empty*/ printf("Queue is Empty. No records to
int e_que(struct q_arr* queue) display.");
{ return;
if(queue->num==0) }
return true; printf("\nElements present in the Queue are: ");
return false; for(j=queue->f;j<=queue->r;j++)
} printf("%d\t",queue->a[j]);
printf("\n");
/* Function to check if the queue is full*/ }
int f_que(struct q_arr* queue)
{
if(queue->num == size)
return true;
return false;
}

/* Function to add an element to the queue*/ /*( i ) Write C programs that implement
int add_ele(struct q_arr* queue,int j) Queue (its operations) usingii) Pointers */
{
if(f_que(queue)) #define true 1
return false; #define false 0

if(queue->r == size - 1) #include<stdio.h>


queue->r = -1; #include<conio.h>
queue->a[++queue->r] = j; #include<process.h>
queue->num++;
return true; struct q_point
} {
int ele;
/* Function to remove an element of the queue*/ struct q_point* n;
int rem_ele(struct q_arr* queue) };
{
int j; struct q_point *f_ptr = NULL;
if(e_que(queue))
return -9999; int e_que(void);
j = queue->a[queue->f++]; void add_ele(int);
if(queue->f == size) int rem_ele(void);
queue->f = 0; void show_ele();
queue->num--;
return j; /*main function*/

28
void main() case 3:
{ show_ele();
int ele,choice,j; getch();
while(1) break;
{
clrscr(); case 4:
printf("\n\n****IMPLEMENTATION OF exit(1);
QUEUE USING POINTERS****\n"); break;
printf("===========================
==================="); default:
printf("\n\t\t MENU\n"); printf("\n\tInvalid choice.");
printf("=========================== getch();
==================="); break;
printf("\n\t[1] To insert an element"); }
printf("\n\t[2] To remove an element");
printf("\n\t[3] To display all the elements"); }
printf("\n\t[4] Exit"); }
printf("\n\n\tEnter your choice:");
scanf("%d", &choice); /* Function to check if the queue is empty*/
int e_que(void)
switch(choice) {
{ if(f_ptr==NULL)
case 1: return true;
{ return false;
printf("\n\tElement to be inserted:"); }
scanf("%d",&ele);
add_ele(ele); /* Function to add an element to the queue*/
getch(); void add_ele(int ele)
break; {
} struct q_point *queue = (struct
q_point*)malloc(sizeof(struct q_point));
case 2: queue->ele = ele;
{ queue->n = NULL;
if(!e_que()) if(f_ptr==NULL)
{ f_ptr = queue;
j=rem_ele(); else
printf("\n\t%d is removed from the {
queue",j); struct q_point* ptr;
getch(); ptr = f_ptr;
} for(ptr=f_ptr ;ptr->n!=NULL; ptr=ptr->n);
else ptr->n = queue;
{ }
printf("\n\tQueue is Empty."); }
getch();
} /* Function to remove an element from the
break; queue*/
} int rem_ele()
{

29
struct q_point* queue=NULL; /* Write a C program that uses Stack
if(e_que()==false) operations to perform the following:
{ i) Converting infix expression into
int j = f_ptr->ele; postfix expression
queue=f_ptr; ii) Evaluating the postfix expression */
f_ptr = f_ptr->n;
free (queue); #include<stdio.h>
return j; #include<conio.h>
}
else int st[100];
{ int st_top=-1;
printf("\n\tQueue is empty.");
return -9999; int cal(char post[]);
} void in_post(char in[]);
} void push_item(int it);
int pop_item();
/* Function to display the queue*/ int st_ISP(char t);
void show_ele() int st_ICP(char t);
{
struct q_point *ptr=NULL; /*main function*/
ptr=f_ptr; void main()
if(e_que()) {
{ char in[100],post[100];
printf("\n\tQUEUE is Empty."); clrscr();
return; printf("\n\tEnter the Infix Expression: ");
} gets(in);
else in_post(in);
{ getch();
printf("\n\tElements present in Queue }
are:\n\t"); /*end main*/
while(ptr!=NULL)
{ void push_item(int it)
printf("%d\t",ptr->ele); {
ptr=ptr->n; if(st_top==99)
} {
} printf("\n\n\t*STACK is Full*");
} getch();
exit(1);
}
st[++st_top]=it;
}

int pop_item()
{
WEEK 17:- int it;
if(st_top==-1)
{
getch();

30
} }
return(st[st_top--]);
} while(st_top!=-1)
{
/*Function for converting an infix expression to c=pop_item();
a postfix expression. */ post[y]=c;
void in_post(char in[]) y++;
{ }
int x=0,y=0,z,result=0; printf("\n\tThe Postfix Expression is:");
char a,c, post[100];
char t; for(z=0;z<y;z++)
push_item('\0'); printf("%c",post[z]);
t=in[x]; printf("\n\nDo you want to evaluate the Result
while(t!='\0') of Postfix Expression?(Y/N):");
{ scanf("%c",&a);
if(isalnum(t)) if(a=='y' || a=='Y')
/*For checking whether the value in t is an {
alphabet or number. */ result=cal(post);
{ printf("\n\n\tResult is: %d\n",result);
post[y]=t; getch();
y++; }
} else if(a=='n' || a=='N')
else if(t=='(') {
{ exit(0);
push_item('('); }
} }
else if(t==')')
{ /*Determining priority of inside elements*/
while(st[st_top]!='(') int st_ISP(char t)
{ {
c=pop_item(); switch(t)
post[y]=c; {
y++; case '(':return (10);
} case ')':return (9);
c=pop_item(); case '+':return (7);
} case '-':return (7);
else case '*':return (8);
{ case '/':return (8);
while(st_ISP(st[st_top])>=st_ICP(t)) case '\0':return (0);
{ default: printf("Expression is invalid.");
c=pop_item(); break;
post[y]=c; }
y++; return 0;
} }
push_item(t);
} /*Determining priority of approaching
x++; elements*/
t=in[x]; int st_ICP(char t)

31
{ }
switch(t) if(st_top>0)
{ {
printf("Number of Operands are more than
case '(':return (10); Operators.");
case ')':return (9); exit(0);
case '+':return (7); }
case '-':return (7); else
case '*':return (8); {
case '/':return (8); y=pop_item();
case '\0':return (0); return (y);
default: printf("Expression is invalid."); }
break; return 0;
} }
return 0;
}

/*Evaluating the result of postfix expression*/


int cal(char post[])
{
int m,n,x,y,j=0,len;
len=strlen(post); WEEK 18:-
while(j<len)
{
if(isdigit(post[j])) /* Write a C program that uses functions to
{ perform the following:
x=post[j]-'0'; i) Creating a Binary Tree of integers
push_item(x); ii) Traversing the above binary tree in
} preorder, inorder and postorder. */
else
{ #include<stdio.h>
m=pop_item(); #include <stdlib.h>
n=pop_item(); #include<conio.h>

switch(post[j]) struct treenode


{ {
case '+':x=n+m; int ele;
break; struct treenode *l_child, *r_child;
case '-':x=n-m; };
break;
case '*':x=n*m; struct treenode *insert_node(struct treenode
break; *t,int a);
case '/':x=n/m; void TraverseInorder(struct treenode *t);
break; void TraversePreorder(struct treenode *t);
} void TraversePostorder(struct treenode *t);
push_item(x);
} /*main function*/
j++; void main()

32
{ TraversePreorder(root_node);
struct treenode *root_node = NULL; getch();
int num,value; break;
int choice;
case 3:
clrscr(); printf("\n\nBinary tree using Postorder
Traversal : ");
printf("------------------------------------------------ TraversePostorder(root_node);
----\n"); getch();
printf("\t\t\tMENU\n"); break;
printf("------------------------------------------------
-----\n"); default:
printf("[1] Create a Binary Tree and Use printf("Invalid Choice");
Inorder Traversal\n"); break;
printf("[2] Create a Binary Tree and Use }
Preorder Traversal\n"); }
printf("[3] Create a Binary Tree and Use }
Postorder Traversal\n"); /*end main*/
printf("------------------------------------------------
-----\n"); /* Function to create a Binary Tree of integers
printf("Enter your choice:"); data */
scanf("%d",&choice); struct treenode *insert_node(struct treenode
*t,int a)
if(choice>0 & choice<=3) {
{ struct treenode *temp_node1,*temp_node2;
printf("\nEnter the number of nodes:"); if(t == NULL)
scanf("%d",&num); {
t = (struct treenode *) malloc(sizeof(struct
while(num-- > 0) treenode));
{ if(t == NULL)
printf("\n\nEnter the data value:"); {
scanf("%d",&value); printf("Value cannot be allocated.\n");
root_node = exit(0);
insert_node(root_node,value); }
} t->ele = a;
t->l_child=t->r_child=NULL;
switch(choice) }
{ else
case 1: {
printf("\n\nBinary tree using Inorder temp_node1 = t;
Traversal : ");
TraverseInorder(root_node); while(temp_node1 != NULL)
getch(); {
break; temp_node2 = temp_node1;
if( temp_node1 ->ele > a)
case 2: temp_node1 = temp_node1->l_child;
printf("\n\nBinary tree using Preorder else
Traversal : "); temp_node1 = temp_node1->r_child;

33
} void TraversePreorder(struct treenode *t)
if( temp_node2->ele > a) {
{ if(t != NULL)
temp_node2->l_child = (struct {
treenode*)malloc(sizeof(struct treenode)); printf("%d\t",t->ele);
temp_node2 = temp_node2->l_child; TraversePreorder(t->l_child);
if(temp_node2 == NULL) TraversePreorder(t->r_child);
{ }
printf("Value cannot be allocated.\n"); }
exit(0);
} /* Function for Traversing the binary tree in
temp_node2->ele = a; postorder. */
temp_node2->l_child=temp_node2->r_child void TraversePostorder(struct treenode *t)
= NULL; {
} if(t != NULL)
else {
{ TraversePostorder(t->l_child);
temp_node2->r_child = (struct TraversePostorder(t->r_child);
treenode*)malloc(sizeof(struct treenode)); printf("%d\t",t->ele);
}
temp_node2 = temp_node2->r_child; }
if(temp_node2 == NULL)
{
printf("Value cannot be allocated.\n");
exit(0);
}
temp_node2->ele = a;
temp_node2->l_child=temp_node2->r_child WEEK 19 :-
= NULL;
} /* Write C programs that use both recursive
} and non recursive functions
return(t); to perform the following searching operation
} for a Key value in a given list of integers :
i) Linear search */
/* Function for Traversing the binary tree in
inorder. */ #include <stdio.h>
void TraverseInorder(struct treenode *t) #define MAX_LEN 10
{
if(t != NULL) void l_search_recursive(int l[],int num,int ele);
{ void l_search(int l[],int num,int ele);
TraverseInorder(t->l_child); void read_list(int l[],int num);
printf("%d\t",t->ele); void print_list(int l[],int num);
in_order(t->r_child);
} void main()
} {
int l[MAX_LEN], num, ele;
/* Function for Traversing the binary tree in int ch;
preorder. */

34
clrscr(); int j, f=0;
for(j=0;j<num;j++)
printf("=========================== if( l[j] == ele)
==========================="); {
printf("\n\t\t\tMENU"); printf("\nThe element %d is present at
printf("\n========================== position %d in list\n",ele,j);
==========================="); f=1;
printf("\n[1] Linary Search using Recursion break;
method"); }
printf("\n[2] Linary Search using Non- if(f==0)
Recursion method"); printf("\nThe element is %d is not present
printf("\n\nEnter your Choice:"); in the list\n",ele);
scanf("%d",&ch); }

if(ch<=2 & ch>0) /* Recursive method*/


{ void l_search_recursive(int l[],int num,int ele)
printf("Enter the number of elements :"); {
scanf("%d",&num); int f = 0;
read_list(l,num);
printf("\nElements present in the list if( l[num] == ele)
are:\n\n"); {
print_list(l,num); printf("\nThe element %d is present at
printf("\n\nElement you want to position %d in list\n",ele,num);
search:\n\n"); f=1;
scanf("%d",&ele); }
else
switch(ch) {
{ if((num==0) && (f==0))
case 1:printf("\n**Recursion method**\n"); {
l_search_recursive(l,num,ele); printf("The element %d is not found.",ele);
getch(); }
break; else
{
case 2:printf("\n**Non-Recursion l_search(l,num-1,ele);
method**\n"); }
l_search_nonrecursive(l,num,ele); }
getch(); getch();
break; }
}
} void read_list(int l[],int num)
getch(); {
} int j;
/*end main*/ printf("\nEnter the elements:\n");
for(j=0;j<num;j++)
/* Non-Recursive method*/ scanf("%d",&l[j]);
void l_search_nonrecursive(int l[],int num,int }
ele)
{ void print_list(int l[],int num)

35
{ {
int j; int m,pos;
for(j=0;j<num;j++) if (arrayStart<=arrayEnd)
printf("%d\t",l[j]); {
} m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a<l[m])
/* Write C programs that use both recursive return b_search_recursive(l,arrayStart,m-
and non recursive functions to perform 1,a);
the following searching operations for a Key else
value in a given list of integers : return
ii) Binary search*/ b_search_recursive(l,m+1,arrayEnd,a);
}
#include <stdio.h> return -1;
#define MAX_LEN 10 }

/* Non-Recursive function*/ void read_list(int l[],int n)


void b_search_nonrecursive(int l[],int num,int {
ele) int i;
{ printf("\nEnter the elements:\n");
int l1,i,j, flag = 0; for(i=0;i<n;i++)
l1 = 0; scanf("%d",&l[i]);
i = num-1; }
while(l1 <= i)
{ void print_list(int l[],int n)
j = (l1+i)/2; {
if( l[j] == ele) int i;
{ for(i=0;i<n;i++)
printf("\nThe element %d is present at printf("%d\t",l[i]);
position %d in list\n",ele,j); }
flag =1;
break; /*main function*/
} void main()
else {
if(l[j] < ele) int l[MAX_LEN], num, ele,f,l1,a;
l1 = j+1; int ch,pos;
else
i = j-1; clrscr();
}
if( flag == 0) printf("===========================
printf("\nThe element %d is not present in the ===========================");
list\n",ele); printf("\n\t\t\tMENU");
} printf("\n==========================
===========================");
/* Recursive function*/ printf("\n[1] Binary Search using Recursion
int b_search_recursive(int l[],int arrayStart,int method");
arrayEnd,int a)

36
printf("\n[2] Binary Search using Non- a given list of integers in ascending order: i)
Recursion method"); Bubble sort */
printf("\n\nEnter your Choice:");
scanf("%d",&ch); #include <stdio.h>
#define MAX 10
if(ch<=2 & ch>0)
{ void swapList(int *m,int *n)
printf("\nEnter the number of elements : "); {
scanf("%d",&num); int temp;
read_list(l,num); temp = *m;
printf("\nElements present in the list *m = *n;
are:\n\n"); *n = temp;
print_list(l,num); }
printf("\n\nEnter the element you want to
search:\n\n"); // Function for Bubble Sort
scanf("%d",&ele); void bub_sort(int list[], int n)
{
int i,j;
switch(ch) for(i=0;i<(n-1);i++)
{ for(j=0;j<(n-(i+1));j++)
case 1:printf("\nRecursive method:\n"); if(list[j] > list[j+1])
pos=b_search_recursive(l,0,num,ele); swapList(&list[j],&list[j+1]);
if(pos==-1) }
{
printf("Element is not found"); void readlist(int list[],int n)
} {
else int j;
{ printf("\nEnter the elements: \n");
printf("Element is found at %d for(j=0;j<n;j++)
position",pos); scanf("%d",&list[j]);
} }
getch();
break; // Showing the contents of the list
void printlist(int list[],int n)
case 2:printf("\nNon-Recursive method:\n"); {
b_search_nonrecursive(l,num,ele); int j;
getch(); for(j=0;j<n;j++)
break; printf("%d\t",list[j]);
} }
}
getch(); void main()
} {
int list[MAX], num;
WEEK 20 : - clrscr();
printf("\n\n\n***** Enter the number of
elements [Maximum 10] *****\n");
/* Write C programs that implement the scanf("%d",&num);
following sorting methods to sort readlist(list,num);

37
printf("\n\nElements in the list before sorting if( i < j)
are:\n"); swap(&list[i],&list[j]);
printlist(list,num); }
bub_sort(list,num); swap(&list[m],&list[j]);
printf("\n\nElements in the list after sorting quicksort(list,m,j-1);
are:\n"); quicksort(list,j+1,n);
printlist(list,num); }
getch(); }
}
// Function to read the data
void read_data(int list[],int n)
/*( ii ) Write C program that implement the {
following sorting methods int j;
to sort a given list of integers in ascending printf("\n\nEnter the elements:\n");
order: ii) Quick sort */ for(j=0;j<n;j++)
scanf("%d",&list[j]);
#include <stdio.h> }
#define MAX 10
// Function to print the data
void swap(int *m,int *n) void print_data(int list[],int n)
{ {
int temp; int j;
temp = *m; for(j=0;j<n;j++)
*m = *n; printf("%d\t",list[j]);
*n = temp; }
}
int get_key_position(int x,int y ) void main()
{ {
return((x+y) /2); int list[MAX], num;
} clrscr();
printf("\n***** Enter the number of elements
// Function for Quick Sort Maximum [10] *****\n");
void quicksort(int list[],int m,int n) scanf("%d",&num);
{ read_data(list,num);
int key,i,j,k; printf("\n\nElements in the list before sorting
if( m < n) are:\n");
{ print_data(list,num);
k = get_key_position(m,n); quicksort(list,0,num-1);
swap(&list[m],&list[k]); printf("\n\nElements in the list after sorting
key = list[m]; are:\n");
i = m+1; print_data(list,num);
j = n; getch();
while(i <= j) }
{
while((i <= n) && (list[i] <= key))
i++; WEEK 21:-
while((j >= m) && (list[j] > key))
j--;

38
/*( A ) Write C program that implement the void merge_sort(int x[], int end, int start);
following sorting methods
to sort a given list of integers in ascending int main(void) {
order: i) Insertion sort */ int ary[MAX_ARY];
int j = 0;
#include<stdio.h>
#include<conio.h> printf("\n\nEnter the elements to be sorted: \n");
for(j=0;j<MAX_ARY;j++)
void inst_sort(int []); scanf("%d",&ary[j]);

void main() /* array before mergesort */


{ printf("Before :");
int num[5],count; for(j = 0; j < MAX_ARY; j++)
clrscr(); printf(" %d", ary[j]);
printf("\nEnter the Five Elements to sort:\n");
printf("\n");
for (count=0;count<5;count++)
scanf("%d",&num[count]); merge_sort(ary, 0, MAX_ARY - 1);
inst_sort(num);
/* array after mergesort */
printf("\n\nElements after sorting: \n"); printf("After Merge Sort :");
for(count=0;count<5;count++) for(j = 0; j < MAX_ARY; j++)
printf("%d\n",num[count]); printf(" %d", ary[j]);
getch();
} printf("\n");
getch();
// Function for Insertion Sorting }
void inst_sort(int num[])
{ /* Method to implement Merge Sort*/
int i,j,k; void merge_sort(int x[], int end, int start) {
for(j=1;j<5;j++) int j = 0;
{ const int size = start - end + 1;
k=num[j]; int mid = 0;
for(i=j-1;i>=0 && k<num[i];i--) int mrg1 = 0;
num[i+1]=num[i]; int mrg2 = 0;
num[i+1]=k; int executing[MAX_ARY];
}
} if(end == start)
/* ( B ) Write C program that implement the return;
following sorting methods to sort a given list
of integers in ascending order: mid = (end + start) / 2;
ii) Merge sort */
merge_sort(x, end, mid);
#include <stdio.h> merge_sort(x, mid + 1, start);
#include <stdlib.h>
for(j = 0; j < size; j++)
#define MAX_ARY 10 executing[j] = x[end + j];

39
mrg1 = 0; for (j=0; j<=n; j++)
mrg2 = mid - end + 1; if(j!=i)
{
for(j = 0; j < size; j++) { numerator *= x-arr_x[j];
if(mrg2 <= start - end) denominator *= arr_x[i]-arr_x[j];
if(mrg1 <= mid - end) }
if(executing[mrg1] > executing[mrg2]) y+=(numerator/denominator)*arr_y[i];
x[j + end] = executing[mrg2++]; }
else printf("When x=%4.1f y=%7.1f\n",x,y);
x[j + end] = executing[mrg1++]; getch();
else }
x[j + end] = executing[mrg2++]; /*( B ) Write C program to implement the
else Newton- Gregory forward interpolation.*/
x[j + end] = executing[mrg1++];
} #include<stdio.h>
} #include<conio.h>
#define MaxN 100
#define Order_of_diff 4

void main ()
WEEK 22 : - {
float arr_x[MaxN+1], arr_y[MaxN+1],
numerator=1.0, denominator=1.0, x, y, p, h,
/*( A ) Write C program to implement the diff_table[MaxN+1][Order_of_diff+1];
Lagrange interpolation.*/ int i,j,n,k;
clrscr();
#include<stdio.h>
#include<conio.h> printf("Enter the value of n \n");
#define MaxN 90 scanf("%d",&n);
printf("Enter the values of x and y");
void main()
{ for(i=0; i<=n; i++)
float arr_x[MaxN+1], arr_y[MaxN+1], scanf("%f%f", &arr_x[i], &arr_y[i]);
numerator, denominator, x, y=0; printf("Enter the value of x at which value of y
int i, j, n; is to be calculated");
clrscr(); scanf("%f", &x);
printf("Enter the value of n: \n"); h=arr_x[1]-arr_x[0];
scanf("%d", &n);
printf("Enter the values of x and y: \n"); for(i=0; i<=n-1; i++)
for(i=0; i<=n; i++) diff_table[i][1]=arr_y[i+1]-arr_y[i];/*Creating
scanf("%f%f", &arr_x[i], &arr_y[i]); the difference table and calculating first order
printf("Enter the value of x at which value of y differences*/
is to be calculated: "); for(j=2; j<=Order_of_diff; j++)/*Calculating
scanf("%f", &x); higher order differences*/
for (i=0; i<=n; i++) for(i=0; i<=n-j; i++)
{ diff_table[i][j]=diff_table[i+1][j-1] -
numerator=1; diff_table[i][j-1];
denominator=1; i=0;

40
while(!(arr_x[i]>x)) /* Finding x0 */ scanf("%f%f",&a[i],&b[i]);
i++; mean_x=mean(a,n);
i--; mean_y=mean(b,n);
p=(x-arr_x[i])/h; deviation(a,mean_x,n,dx,&sx);
y=arr_y[i]; deviation(b,mean_y,n,dy,&sy);
for (k=1; k<=Order_of_diff; k++)
{ for(i=0;i<n;i++)
numerator *=p-k+1; sum_xy=sum_xy+dx[i]*dy[i];
denominator *=k; corr_coff=sum_xy/(n*sx*sy);
y +=(numerator/denominator)*diff_table[i][k]; printf("Enter the type of regression coefficient
} as 'x on y' or 'y on x': ");
printf("When x=%6.1f, y=%6.2f\n",x, y); fflush(stdin);
getch(); gets(type_coff);
}
if(strcmp(type_coff,"x on y")==1)
{
reg_coff_xy=corr_coff*(sx/sy);
WEEK 23:- printf("\nThe value of linear regression
coefficient is %f",reg_coff_xy);
}
/* ( A )Write C program to implement the else if(strcmp(type_coff,"y on x")==1)
linear regression algorithm. */ {
reg_coff_yx=corr_coff*(sy/sx);
#include<stdio.h> printf("\nThe value of linear regression
#include<conio.h> coefficient is %f",reg_coff_yx);
#include<math.h> }
#include<string.h> else
printf("\nEnter the correct type of regression
float mean(float *a, int n); coefficient.");
void deviation(float *a, float mean, int n, float getch();
*d, float *S); }

void main()
{ float mean(float *a, int n)
float a[20],b[20],dx[20],dy[20]; {
float float sum=0, i=0;
sy=0,sx=0,mean_x=0,mean_y=0,sum_xy=0; for(i=0;i<n;i++)
float corr_coff=0,reg_coff_xy=0, sum=sum+a[i];
reg_coff_yx=0; sum=sum/n;
char type_coff[7]; return (sum);
int n=0,i=0; }

clrscr(); void deviation(float *a, float mean, int n, float


*d, float *s)
printf("Enter the value of n: "); {
scanf("%d",&n); float sum=0,t=0;
printf("Enter the values of x and y:\n"); int i=0;
for(i=0;i<n;i++) for(i=0;i<n;i++)

41
{ e1=eval(postfix,x0);
d[i]=a[i]-mean; e2=eval(postfix,xn);
t=d[i]*d[i]; s=log(e1)+log(e2);
sum=sum+t; for (i=1;i<=n-1;i++)
} s+=2*log(eval(postfix,x0+i*h));
sum=sum/n; }
*s=sqrt(sum); else
} {
infix_postfix(exp);
s=eval(postfix,x0)+eval(postfix,xn);
for (i=1;i<=n-1;i++)
s+=2*eval(postfix,x0+i*h);
WEEK 24 :- }
printf("Value of the integral is %6.3f\n",
/* Write C program to implement Trapezoidal (h/2)*s);
method.*/ return(0);
}
#include<stdio.h> /*Inserting the operands in a stack. */
#include<conio.h> void push(float item)
#include<math.h> {
if(top==99)
char postfix[80]; {
float stack[80]; printf("\n\tThe stack is full");
char stack1[80]; getch();
int top=-1,top1=-1; exit(0);
}
float eval(char postfix[], float x1); else
void infix_postfix(char infix[]); {
top++;
main() stack[top]=item;
{ }
float x0, xn, h, s,e1,e2; return;
char exp[80], arr[80]; }
int i,n,l=0; /*Removing the operands from a stack. */
clrscr(); float pop()
printf("\nEnter an expression: "); {
gets(exp); float item;
puts("Enter x0, xn and number of if(top==-1)
subintervals"); {
scanf("%f%f%d", &x0, &xn, &n); printf("\n\tThe stack is empty\n\t");
h=(xn-x0)/n; getch();
if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g') }
{ item=stack[top];
l=strlen(exp); top--;
for(i=0;i<l-3; i++) return (item);
arr[0]=exp[i+3]; }
arr[i]='\0'; void push1(char item)
infix_postfix(arr); {

42
if(top1==79) push1('(');
{ }
printf("\n\tThe stack is full"); else if(token==')')
getch(); {
exit(0); while(stack1[top1]!='(')
} {
else ch=pop1();
{ postfix[j]=ch;
top1++; j++;
stack1[top1]=item; }
} ch=pop1();
return; }
} else
/*Removing the operands from a stack. */ {
char pop1()
{ while(ISPriority(stack1[top1])>=ICP(token))
char item; {
if(top1==-1) ch=pop1();
{ /*Assigning the popped element into the postfix
printf("\n\tThe stack1 is empty\n\t"); array. */
getch(); postfix[j]=ch;
} j++;
item=stack1[top1]; }
top1--; push1(token);
return (item); }
} i++;
token=infix[i];
/*Converting an infix expression to a postfix }
expression. */ while(top1!=0)
void infix_postfix(char infix[]) {
{ ch=pop1();
int i=0,j=0,k; postfix[j]=ch;
char ch; j++;
char token; }
for(i=0;i<79;i++) postfix[j]='\0';
postfix[i]=' '; }
push1('?');
i=0; int ISPriority(char token)
token=infix[i]; {
while(token!='\0') switch(token)
{ {
if(isalnum(token)) case '(':return (0);
{ case ')':return (9);
postfix[j]=token; case '+':return (7);
j++; case '-':return (7);
} case '*':return (8);
else if(token=='(') case '/':return (8);
{ case '?':return (0);

43
default: printf("Invalid expression"); case '-':k=t2-t1;
break; break;
} case '*':k=t2*t1;
return 0; break;
} case '/':k=t2/t1;
/*Determining the priority of elements that are break;
approaching towards the stack. */ default: printf("\n\tInvalid expression");
int ICP(char token) break;
{ }
switch(token) push(k);
{ }
case '(':return (10); i++;
case ')':return (9); }
case '+':return (7); if(top>0)
case '-':return (7); {
case '*':return (8); printf("You have entered the operands more
case '/':return (8); than the operators");
case '\0':return (0); exit(0);
default: printf("Invalid expression"); }
break; else
} {
return 0; r=pop();
} return (r);
/*Calculating the result of expression, which is }
converted in postfix notation. */ return 0;
float eval(char p[], float x1) }
{
float t1,t2,k,r; /* Write C program to implement Simpson
int i=0,l; method. */
l=strlen(p);
while(i<l) #include<stdio.h>
{ #include<conio.h>
if(p[i]=='x') #include<math.h>
push(x1);
else char postfix[80];
if(isdigit(p[i])) float stack[80];
{ char stack1[80];
k=p[i]-'0'; int top=-1,top1=-1;
push(k); float eval(char postfix[], float x1);
} void infix_postfix(char infix[]);
else
{ main()
t1=pop(); {
t2=pop(); float x0, xn, h, s,e1,e2, e3;
switch(p[i]) char exp[80], arr[80];
{ int i,n,l=0;
case '+':k=t2+t1; clrscr();
break; printf("\nEnter an expression: ");

44
gets(exp); }
puts("Enter x0, xn and number of sub-intervals: /*Removing the operands from a stack. */
"); float pop()
scanf("%f%f%d", &x0, &xn, &n); {
h=(xn-x0)/n; float item;
if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g') if(top==-1)
{ {
l=strlen(exp); printf("\n\tThe stack is empty\n\t");
for(i=0;i<l-3; i++) getch();
arr[0]=exp[i+3]; }
arr[i]='\0'; item=stack[top];
infix_postfix(arr); top--;
e1=eval(postfix,x0); return (item);
e2=eval(postfix,xn); }
e3=4*eval(postfix, x0+h); void push1(char item)
s=log(e1)+log(e2)+log(e3); {
for (i=3;i<=n-1;i+=2) if(top1==79)
s+=4*eval(postfix,x0+i*h)+2*eval(postfix, {
x0+(i-1)*h); printf("\n\tThe stack is full");
} getch();
else exit(0);
{ }
infix_postfix(exp); else
s=eval(postfix,x0)+eval(postfix,xn) {
+4*eval(postfix, x0+h); top1++;
for (i=3;i<=n-1;i+=2) stack1[top1]=item;
s+=4*eval(postfix,x0+i*h)+2*eval(postfix, }
x0+(i-1)*h); return;
} }
printf("The value of integral is %6.3f\n", /*Removing the operands from a stack. */
(h/3)*s); char pop1()
return(0); {
} char item;
/*Inserting the operands in a stack. */ if(top1==-1)
void push(float item) {
{ printf("\n\tThe stack1 is empty\n\t");
if(top==99) getch();
{ }
printf("\n\tThe stack is full"); item=stack1[top1];
getch(); top1--;
exit(0); return (item);
} }
else /*Converting an infix expression to a postfix
{ expression. */
top++; void infix_postfix(char infix[])
stack[top]=item; {
} int i=0,j=0,k;
return; char ch;

45
char token;
for(i=0;i<79;i++) /*Determining the priority of elements that are
postfix[i]=' '; placed inside the stack. */
push1('?'); int ISPriority(char token)
i=0; {
token=infix[i]; switch(token)
while(token!='\0') {
{ case '(':return (0);
if(isalnum(token)) case ')':return (9);
{ case '+':return (7);
postfix[j]=token; case '-':return (7);
j++; case '*':return (8);
} case '/':return (8);
else if(token=='(') case '?':return (0);
{ default: printf("Invalid expression");
push1('('); }
} return 0;
else if(token==')') }
{
while(stack1[top1]!='(') /*Determining the priority of elements that are
{ approaching towards the stack. */
ch=pop1(); int ICP(char token)
postfix[j]=ch; {
j++; switch(token)
} {
ch=pop1(); case '(':return (10);
} case ')':return (9);
else case '+':return (7);
{ case '-':return (7);
while(ISPriority(stack1[top1])>=ICP(token)) case '*':return (8);
{ case '/':return (8);
ch=pop1(); case '\0':return (0);
postfix[j]=ch; default: printf("Invalid expression");
j++; }
} return 0;
push1(token); }
} /*Calculating the result of expression, which is
i++; converted in postfix notation. */
token=infix[i]; float eval(char p[], float x1)
} {
while(top1!=0) float t1,t2,k,r;
{ int i=0,l;
ch=pop1(); l=strlen(p);
postfix[j]=ch; while(i<l)
j++; {
} if(p[i]=='x')
postfix[j]='\0'; push(x1);
} else

46
if(isdigit(p[i]))
{
k=p[i]-'0';
push(k);
}
else
{
t1=pop();
t2=pop();
switch(p[i])
{
case '+':k=t2+t1;
break;
case '-':k=t2-t1;
break;
case '*':k=t2*t1;
break;
case '/':k=t2/t1;
break;
default: printf("\n\tInvalid expression");
}
push(k);
}
i++;
}
if(top>0)
{
printf("You have entered the operands more
than the operators");
exit(0);
}
else
{
r=pop();
return (r);
}
return 0;
}

47

You might also like