Académique Documents
Professionnel Documents
Culture Documents
net
#include
void main()
{
int num1=0, num2=1,no,counter,fab;
clrscr();
printf(”\n\n\t\t\t<----FIBONACCI SERIES---->“);
printf(”\n\n\t\t%d %d”,num1,num2);
//LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS PRINTED IN ADVANCE
for(counter = 1; counter <= no-2; counter++)
{
fab=num1 + num2;
printf(" %d",fab);
num1=num2;
num2=fab;
}
getch();
}
2. /* Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied
by the user. */
#include
void main()
{
int no,counter,counter1,check;
clrscr();
printf(”<-----------------------PRIME NO. SERIES------------------------>“);
printf(”\n\n\n\t\t\tINPUT THE VALUE OF N: “);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
scanf(”%d”,&no);
printf(”\n\nTHE PRIME NO. SERIES B/W 1 TO %d : \n\n”,no);
#include
#include
void main()
{
int num, k=1, sum=0;
clrscr();
printf(”Enter the number whose digits are to be added:”);
scanf(”%d”,&num);
while(num!=0)
{
k=num%10;
sum=sum+k;
k=num/10;
num=k;
}
printf(”Sum of the digits:%d”,sum);
getch();
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
Week 2
#include
#include
void main()
{
int counter,f_coun;
float sum=0,x,power,fact;
clrscr();
printf(”SUM : %f”,sum);
getch();
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
#include
#include
#include
void main()
{
float a,b,c,root1,root2;
clrscr();
printf(”\n Enter values of a,b,c for finding roots of a quadratic eq:\n”);
scanf(”%f%f%f”,&a,&b,&c);
/*checking condition*/
if(b*b>4*a*c)
{
root1=-b+sqrt(b*b-4*a*c)/2*a;
root2=-b-sqrt(b*b-4*a*c)/2*a;
printf(”\n*****ROOTS ARE*****\n”);
printf(”\n root1=%f\n root2=%f”,root1,root2);
}
else
printf(”\n Imaginary Roots.”);
getch();
}
Week 3
1. /* Write C programs that use both recursive and non-recursive functions.To find the factorial of a
given integer.*/
#include
#include
void main()
{
int n,i;
long fact;
clrscr();
printf(”Enter the number: “);
scanf(”%d”,&n);
if(n==0)
printf(”Factorial of 0 is 1\n”);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
else
{
printf(”Factorial of %d Using Recursive Function is %d\n”,n,recr_factorial(n));
printf(”Factorial of %d Using Non-Recursive Function is %d\n”,n,iter_factorial(n));
}
getch();
}
/* Recursive Function*/
unsigned int recr_factorial(int n) {
return n>=1 ? n * recr_factorial(n-1) : 1;
}
/* Non-Recursive Function*/
unsigned int iter_factorial(int n) {
int accu = 1;
int i;
for(i = 1; i <= n; i++) {
accu *= i;
}
return accu;
}
2. /* Write C programs that use both recursive and non-recursive functions. To find the GCD (greatest
common divisor) of two given integers.*/
#include
#include
#include
int main(void)
{
int a,b,iGcd;
clrscr();
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
getch();
}
/* Recursive Function*/
unsigned int GcdRecursive(unsigned m, unsigned n)
{
if(n>m)
return GcdRecursive(n,m);
if(n==0)
return m;
else
return GcdRecursive(n,m%n);
}
/* Non-Recursive Function*/
unsigned int GcdNonRecursive(unsigned p,unsigned q)
{
unsigned remainder;
remainder = p-(p/q*q);
if(remainder==0)
return q;
else
GcdRecursive(q,remainder);
}
3. /* Write C programs that use both recursive and non-recursive functions. To solve Towers of Hanoi
problem.*/
#include
#include
/* Non-Recursive Function*/
void hanoiNonRecursion(int num,char sndl,char indl,char dndl)
{
char stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;
int top,add;
top=NULL;
one:
if(num==1)
{
printf(”\nMove top disk from needle %c to needle %c “,sndl,dndl);
goto four;
}
two:
top=top+1;
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=3;
num=num-1;
sndl=sndl;
temp=indl;
indl=dndl;
dndl=temp;
goto one;
three:
printf(”\nMove top disk from needle %c to needle %c “,sndl,dndl);
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=5;
num=num-1;
temp=sndl;
sndl=indl;
indl=temp;
dndl=dndl;
goto one;
four:
if(top==NULL)
return;
num=stkn[top];
sndl=stksndl[top];
indl=stkindl[top];
dndl=stkdndl[top];
add=stkadd[top];
top=top-1;
if(add==3)
goto three;
else if(add==5)
goto four;
}
/* Recursive Function*/
void hanoiRecursion( int num,char ndl1, char ndl2, char ndl3)
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
{
if ( num == 1 ) {
printf( “Move top disk from needle %c to needle %c.”, ndl1, ndl2 );
return;
}
void main()
{
int no;
clrscr();
printf(”Enter the no. of disks to be transferred: “);
scanf(”%d”,&no);
if(no<1)
printf(”\nThere’s nothing to move.”);
else
printf(”Non-Recursive”);
hanoiNonRecursion(no,’A',’B',’C');
printf(”\nRecursive”);
hanoiRecursion(no,’A',’B',’C');
getch();
}
Week 4
1. /* Write a C program, which takes two integer operands and one operator form the user, performs the
operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch Statement)
*/
#include
#include
void main()
{
int a,b,res,ch;
clrscr();
printf(”\t *********************”);
printf(”\n\tMENU\n”);
printf(”\t********************”);
printf(”\n\t(1)ADDITION”);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
printf(”\n\t(2)SUBTRACTION”);
printf(”\n\t(3)MULTIPLICATION”);
printf(”\n\t(4)DIVISION”);
printf(”\n\t(5)REMAINDER”);
printf(”\n\t(0)EXIT”);
printf(”\n\t********************”);
printf(”\n\n\tEnter your choice:”);
scanf(”%d”,&ch);
switch(ch)
{
case 1:
res=a+b;
printf(”\n Addition:%d”,res);
break;
case 2:
res=a-b;
printf(”\n Subtraction:%d”,res);
break;
case 3:
res=a*b;
printf(”\n Multiplication:%d”,res);
break;
case 4:
res=a/b;
printf(”\n Division:%d”,res);
break;
case 5:
res=a%b;
printf(”\n Remainder:%d”,res);
break;
case 0:
printf(”\n Choice Terminated”);
exit();
break;
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
default:
printf(”\n Invalid Choice”);
}
getch();
}
2./* 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’. */
#include
#include
void main()
{
int tim_intrval, counter,time;
float accl, distance=0, velos;
clrscr();
printf(”<===========PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A
VECHIAL===========>“);
printf(”\n\n\n\t\t\tNO OF TIME INTERVALS : “);
scanf(”%d”,&tim_intrval);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
#include
void main()
{
int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
clrscr();
printf(”************************************”);
printf(”\n\t\tMENU”);
printf(”\n**********************************”);
printf(”\n[1]ADDITION OF TWO MATRICES”);
printf(”\n[2]MULTIPLICATION OF TWO MATRICES”);
printf(”\n[0]EXIT”);
printf(”\n**********************************”);
printf(”\n\tEnter your choice:\n”);
scanf(”%d”,&ch);
switch(ch)
{
case 1:
printf(”Input rows and columns of A & B Matrix:”);
scanf(”%d%d”,&r1,&c1);
printf(”Enter elements of matrix A:\n”);
for(i=0;i
{
for(j=0;j
scanf(”%d”,&a[i][j]);
}
printf(”Enter elements of matrix B:\n”);
for(i=0;i
{
for(j=0;j
scanf(”%d”,&b[i][j]);
}
printf(”\n =====Matrix Addition=====\n”);
for(i=0;i
{
for(j=0;j
printf(”%5d”,a[i][j]+b[i][j]);
printf(”\n”);
}
break;
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
case 2:
printf(”Input rows and columns of A matrix:”);
scanf(”%d%d”,&m,&n);
printf(”Input rows and columns of B matrix:”);
scanf(”%d%d”,&p,&q);
if(n==p)
{
printf(”matrices can be multiplied\n”);
printf(”resultant matrix is %d*%d\n”,m,q);
printf(”Input A matrix\n”);
read_matrix(a,m,n);
printf(”Input B matrix\n”);
/*Function call to read the matrix*/
read_matrix(b,p,q);
/*Function for Multiplication of two matrices*/
printf(”\n =====Matrix Multiplication=====\n”);
for(i=0;i
for(j=0;j
{
c[i][j]=0;
for(k=0;k
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
case 0:
printf(”\n Choice Terminated”);
exit();
break;
default:
printf(”\n Invalid Choice”);
}
getch();
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
Week 5
1. /* Write a C program to find both the largest and smallest number in a list of integers*/
main( )
{
float largest(float a[ ], int n);
float value[4] = {2.5,-4.75,1.2,3.67};
printf(”%f\n”, largest(value,4));
}
float largest(float a[], int n)
{
int i;
float max;
max = a[0];
for(i = 1; i < n; i++)
if(max < a[i])
max = a[i];
return(max);
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
#include
void main()
{
int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
clrscr();
printf(”************************************”);
printf(”\n\t\tMENU”);
printf(”\n**********************************”);
printf(”\n[1]ADDITION OF TWO MATRICES”);
printf(”\n[2]MULTIPLICATION OF TWO MATRICES”);
printf(”\n[0]EXIT”);
printf(”\n**********************************”);
printf(”\n\tEnter your choice:\n”);
scanf(”%d”,&ch);
switch(ch)
{
case 1:
printf(”Input rows and columns of A & B Matrix:”);
scanf(”%d%d”,&r1,&c1);
printf(”Enter elements of matrix A:\n”);
for(i=0;i
{
for(j=0;j
scanf(”%d”,&a[i][j]);
}
printf(”Enter elements of matrix B:\n”);
for(i=0;i
{
for(j=0;j
scanf(”%d”,&b[i][j]);
}
printf(”\n =====Matrix Addition=====\n”);
for(i=0;i
{
for(j=0;j
printf(”%5d”,a[i][j]+b[i][j]);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
printf(”\n”);
}
break;
case 2:
printf(”Input rows and columns of A matrix:”);
scanf(”%d%d”,&m,&n);
printf(”Input rows and columns of B matrix:”);
scanf(”%d%d”,&p,&q);
if(n==p)
{
printf(”matrices can be multiplied\n”);
printf(”resultant matrix is %d*%d\n”,m,q);
printf(”Input A matrix\n”);
read_matrix(a,m,n);
printf(”Input B matrix\n”);
/*Function call to read the matrix*/
read_matrix(b,p,q);
/*Function for Multiplication of two matrices*/
printf(”\n =====Matrix Multiplication=====\n”);
for(i=0;i
for(j=0;j
{
c[i][j]=0;
for(k=0;k
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
case 0:
printf(”\n Choice Terminated”);
exit();
break;
default:
printf(”\n Invalid Choice”);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
}
getch();
}
Week 6
1 /* Write a C program that uses functions to perform the following operations: To insert a sub-string in
to given main string from a given position. */
#include
#include
#include
void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;
int t=0;
int x,g,s,n,o;
clrscr();
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
printf("%s", a);
getch();
}
#include
#include
enum Boolean{false,true};
enum Boolean IsPalindrome(char string[])
{
int left,right,len=strlen(string);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
/* Compare the first and last letter,second & second last & so on */
while(left<right&&matched)>
{
if(string[left]!=string[right])
matched=false;
else
{
left++;
right--;
}
}
return matched;
}</right&&matched)>
int main()
{
char string[40];
clrscr();
printf("****Program to test if the given string is a palindrome****\n");
printf("Enter a string:");
scanf("%s",string);
if(IsPalindrome(string))
printf("The given string %s is a palindrome\n",string);
else
printf("The given string %s is not a palindrome\n",string);
getch();
return 0;
}
3. /* Write a C program that uses functions to perform the following operations: To delete n Characters
from a given position in a given string. */
#include
#include
#include
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
void main()
{
char string[10];
int n,pos,p;
clrscr();
Week 7
1. /* Write a C program to count the lines, words and characters in a given text*/
Program
#include
main()
{
char line[81], ctr;
int i,c,
end = 0,
characters = 0,
words = 0,
lines = 0;
printf(”KEY IN THE TEXT.\n”);
printf(”GIVE ONE SPACE AFTER EACH WORD.\n”);
printf(”WHEN COMPLETED, PRESS ‘RETURN’.\n\n”);
while( end == 0)
{
/* Reading a line of text */
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
c = 0;
while((ctr=getchar()) != ‘\n’)
line[c++] = ctr;
line[c] = ‘\0′;
/* counting the words in a line */
if(line[0] == ‘\0′)
break ;
else
{
words++;
for(i=0; line[i] != ‘\0′;i++)
if(line[i] == ‘ ‘ || line[i] == ‘\t’)
words++;
}
/* counting lines and characters */
lines = lines +1;
characters = characters + strlen(line);
}
printf (”\n”);
printf(”Number of lines = %d\n”, lines);
printf(”Number of words = %d\n”, words);
printf(”Number of characters = %d\n”, characters);
}
Output
KEY IN THE TEXT.
GIVE ONE SPACE AFTER EACH WORD.
WHEN COMPLETED, PRESS ‘RETURN’.
Admiration is a very short-lived passion.
Admiration involves a glorious obliquity of vision.
Always we like those who admire us but we do not
like those whom we admire.
Fools admire, but men of sense approve.
Number of lines = 5
Number of words = 36
Number of characters = 205
2 /* Write a C program that displays the position or index in the string S where the string T begins, or – 1
if S doesn’t contain T. */
#include
#include
#include
void main()
{
char s[30], t[20];
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
char *found;
clrscr();
Week 8
#include
#include
void main()
{
int bin,p,q,r,x;
clrscr();
bin=1;
q=0;
printf(”\nPascal’s Triangle:\n”);
while(q
{
for(p=40-3*q;p>0;–p)
printf(” “);
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
bin=1;
else
bin=(bin*(q-x+1))/x;
printf("%6d",bin);
}
printf("\n");
++q;
}
getch();
}
#include
#include
void main()
{
int num,i,y,x=35;
clrscr();
printf(”\nEnter the number to generate the pyramid:\n”);
scanf(”%d”,&num);
for(y=0;y<=num;y++)
{
/*(x-coordinate,y-coordinate)*/
gotoxy(x,y+1);
printf(”%3d”,abs(i));
x=x-3;
}
getch();
}
Week 9
1. /*Write a C program to read in two numbers, x and n, and then compute the sum of this geometric
progression:
1+x+x2+x3+………….+xn For example: if n is 3 and x is 5, then the program computes 1+5+25+125.
Print x, n, the sum Perform error checking. For example, the formula does not make sense for negative
exponents – if n is less than 0. Have your program print an error message if n<0, then go back and read
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
in the next pair of numbers of without computing the sum. Are any values of x also illegal ? If so, test for
them too. */
#include
#include
#include
void main()
{
int s_sum,i,x,n;
clrscr();
printf(”Enter the values for x and n:”);
scanf(”%d %d”,&x,&n);
if(n<=0 || x<=0)
{
printf(”Value is not valid\n”);
}
else
{
printf(”Value is valid\n”);
s_sum=1;
for(i=1;i<=n;i++)
{
s_sum=s_sum+pow(x,i);
}
printf(”Sum of series=%d\n”,s_sum);
}
getch();
}
Week 10
1. /* 2’s complement of a number is obtained by scanning it from right to left and complementing all the
bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100. Write a C program to find
the 2’s complement of a binary number.*/
#include
#include
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
int i;
clrscr();
printf(”Enter the binary number”);
gets(a);
for(i=0;a[i]!=’\0′; i++)
{
if (a[i]!=’0′ && a[i]!=’1′)
{
printf(”The number entered is not a binary number. Enter the correct number”);
exit(0);
}
}
complement(a);
getch();
}
void complement (char *a)
{
int l, i, c=0;
char b[16];
l=strlen(a);
for (i=l-1; i>=0; i–)
{
if (a[i]==’0′)
b[i]=’1′;
else
b[i]=’0′;
}
for(i=l-1; i>=0; i–)
{
if(i==l-1)
{
if (b[i]==’0′)
b[i]=’1′;
else
{
b[i]=’0′;
c=1;
}
}
else
{
if(c==1 && b[i]==’0′)
{
b[i]=’1′;
c=0;
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
#include
#include
#include
#include
void main()
{
int *a,len,i,j,k;
char *rom;
clrscr();
len=strlen(rom);
for(i=0;i
{
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else if(rom[i]=='C')
a[i]=100;
else if(rom[i]=='D')
a[i]=500;
else if(rom[i]=='M')
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
a[i]=1000;
else
{
printf("\nInvalid Value");
getch();
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i–)
{
if(a[i]>a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i]
k=k+a[i-1];
}
printf(”\nIts Decimal Equivalent is:”);
printf(”%d”,k);
getch();
}
Week 11
#include
#include
struct comp
{
double realpart;
double imgpart;
};
void main()
{
int opern;
clrscr();
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
scanf(”%d”,&opern);
switch(opern)
{
case 0:
exit(0);
case 1:
case 2:
arithmetic(opern);
default:
main();
}
printf(”\n Enter two Complex Numbers (x+iy):\n Real Part of First Number:”);
scanf(”%lf”,&w1.realpart);
printf(”\n Imaginary Part of First Number:”);
scanf(”%lf”,&w1.imgpart);
printf(”\n Real Part of Second Number:”);
scanf(”%lf”,&w2.realpart);
printf(”\n Imaginary Part of Second Number:”);
scanf(”%lf”,&w2.imgpart);
switch(opern)
{
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
break;
}
if (w.imgpart>0)
printf(”\n Answer = %lf+%lfi”,w.realpart,w.imgpart);
else
printf(”\n Answer = %lf%lfi”,w.realpart,w.imgpart);
getch();
main();
}
Week 12
#include
#include
#include
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
break;
else
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
getch();
}
2. /* Write a C program to reverse the first n characters in a file. (Note: The file name and n are specified
on the command line.)*/
#include
#include
#include
#include
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=*argv[2]-48;
n = fread(a,1,k,fp);
a[n]=’\0′;
len=strlen(a);
for(i=len-1;i>=0;i–)
{
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
s[j]=a[i];
printf(”%c”,s[j]);
j=j+1;
}
s[j+1]=’\0′;
getch();
}
Week 13
1/* Write a program to create a linear linked list interactively and print out the list and the total number
of items in the list. */
#include
#include
#define NULL 0
struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node; /* node type defined */
main()
{
node *head;
void create(node *p);
int count(node *p);
void print(node *p);
head = (node *)malloc(sizeof(node));
create(head);
printf(”\n”);
printf(head);
printf(”\n”);
printf(”\nNumber of items = %d \n”, count(head));
}
void create(node *list)
{
printf(”Input a number\n”);
printf(”(type -999 at end): “);
scanf(”%d”, &list -> number); /* create current node */
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
if(list->number == -999)
{
list->next = NULL;
}
if(list->next->next == NULL)
printf(”%d”, list->next->number);
Output
Input a number
(type -999 to end); 60
Input a number
(type -999 to end); 20
Input a number
(type -999 to end); 10
Input a number
(type -999 to end); 40
Input a number
(type -999 to end); 30
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
Input a number
(type -999 to end); 50
Input a number
(type -999 to end); -999
Number of items = 6
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
if(n1 == NULL)
printf(”\n key is not found \n”);
else /* insert new node */
{
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) /* key found */
return(list);
else
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
find(list->next, key);
}
4 /* Write a program for creation of sorted list from a given list of numbers */
#include
#include
#define NULL 0
struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node;
main ()
{
int n;
node *head = NULL;
void print(node *p);
node *insert_Sort(node *p, int n);
while(n != -999)
{
if(head == NULL) /* create ‘base’ node */
{
head = (node *)malloc(sizeof(node));
head ->number = n;
head->next = NULL;
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
}
node *insert_sort(node *list, int x)
{
node *p1, *p2, *p;
p1 = NULL;
p2 = list; /* p2 points to first node */
if(p2->next == NULL)
{
p2 = p2->next; /* p2 set to NULL */
break; /* insert new node at end */
}
}
Output
Input the list of number.
At end, type -999.
80 70 50 40 60 -999
40–>50–>60–>70–>80 –>NULL
Input the list of number.
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
Week 14
/* Write a C program that uses functions to perform the following operations on doubly linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal in both ways */
#include “stdio.h”
#include “alloc.h”
DUBLL high,temp_node,low,last,pntr;
int flag=0;
DUBLL NodeAlloc();
DUBLL Search(int,int);
void CreateItem();
void AppendItem();
void PrintItem();
void DeleteItem();
DUBLL Search(int item,int flag);
DUBLL NodeAlloc();
void InsertItem();
void main(void)
{
int choice,Item;
high=NULL;
while(1)
{
clrscr();
printf(”\n \t\t\t***** M A I N M E N U *****\n\n”);
printf(”\n 1: Create Linked List \n 2: Append a Node to the List \n 3: Traverse the List \n 4: Delete a Node from
the List \n 5: Search a Node \n 6: Insert a Node to the List \n 7: Close \n\n\t\t Enter your Option [ ]\b\b”);
scanf(”%d”,&choice);
switch(choice)
{
case 1:
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
CreateItem();
puts(”\nPress any key to go back to main menu.”);
getch();
break;
case 2:
AppendItem();
break;
case 3:
PrintItem();
puts(”\nPress any key to go back to main menu.”);
getch();
break;
case 4:
DeleteItem();
break;
case 5:
printf(”Find an Item: “);
scanf(”%d”,&Item);
temp_node=Search(Item,0);
if(temp_node)
{
puts(”The item is available in the Linked List.”);
}
else
{
puts(”The item is not found in the Linked List.”);
}
getch();
break;
case 6:
InsertItem();
break;
case 7:
exit();
default:
puts(”Invalid choice.”);
puts(”\nPress any key to go back to main menu.”);
getch();
break;
}
}
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
if(high==NULL)
{
printf(”\n –Creating the list–”);
temp_node=NodeAlloc();
printf(”\n Enter starting data (as integer value) :”);
scanf(”%d”,&temp_node->data);
high=temp_node;
}
else{ printf(”\n List already created @ %d with %d as data.”,high,high->data);}
}
while(low->rightlink!=NULL)
low=low->rightlink;
low->rightlink=temp_node;
temp_node->leftlink=low;
last=low->rightlink;
}
}
/* Function to Traverse the list both ways and print the data*/
void PrintItem()
{
DUBLL temp_node;
if(high==NULL)
{
printf(”\n List is not available. Please create a list first.”);
getch();
CreateItem();
}
temp_node=high;
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
last=low->rightlink;
printf(”\n–Printing The List In Forward direction–\n”);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
getch();
CreateItem();
}
while(temp_node!=NULL)
{
if(temp_node->data==item )
{
if(flag==0)
{
return(1);
}
else
{
return(temp_node);
}
}
temp_node=temp_node->rightlink;
}
}
if(high==NULL)
{
printf(”\n List is not available. Please create a list first.”);
getch();
CreateItem();
}
temp_node=NodeAlloc();
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
printf(”Position At which node to be inserted: ___ & New Item Value: ___ “);
scanf(”%d”,&node);
scanf(”%d”,&temp_node->data);
pntr=Search(node,1);
pntr->rightlink->leftlink=temp_node;
pntr->rightlink=temp_node;
Week 15
#include
#include
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”);
printf(”\n[3] Elements present in Stack”);
printf(”\n[4] Exit\n”);
printf(”\n\tEnter your choice: “);
fflush(stdin);
scanf(”%c”,&choice);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
switch(choice-’0′)
{
case 1:
{
printf(”\n\tElement to be pushed: “);
scanf(”%d”,&num1);
push_ele(num1);
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;
}
case 4:
exit(1);
break;
default:
printf(”\nYour choice is invalid.\n”);
break;
}
}
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
st_arr[++t]=ele;
}
2 /* Write C programs that implement stack (its operations) using ii) Pointers */
#include
#include
struct st_point
{
int ele;
struct st_point *l;
}
*t;
int i;
void main()
{
char choice,num1=0,num2=0;
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
int i;
while(1)
{
clrscr();
printf(”======================================”);
printf(”\n\t\t MENU “);
printf(”\n======================================”);
printf(”\n[1] Using Push Function”);
printf(”\n[2] Using Pop Function”);
printf(”\n[3] Elements present in Stack”);
printf(”\n[4] Exit\n”);
printf(”\n\tEnter your choice: “);
fflush(stdin);
scanf(”%c”,&choice);
switch(choice-’0′)
{
case 1:
{
printf(”\n\tElement to be pushed:”);
scanf(”%d”,&num1);
push_ele(num1);
break;
}
case 2:
{
num2=pop_ele(1);
printf(”\n\tElement to be popped: %d\n\t”,num2);
getch();
break;
}
case 3:
{
printf(”\n\tElements present in the stack are:\n\t”);
display_ele();
getch();
break;
}
case 4:
exit(1);
break;
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
default:
printf(”\nYour choice is invalid.\n”);
break;
}
}
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
}
}
Week 16
#include
#include
#include
#define size 10
#define true 1
#define false 0
struct q_arr
{
int f,r;
int num;
int a[size];
};
/*main function*/
void main()
{
int ele,k;
int ch;
while(1)
{
clrscr();
printf(”\n\n****IMPLEMENTATION OF QUEUE USING ARRAYS****\n”);
printf(”============================================”);
printf(”\n\t\tMENU\n”);
printf(”============================================”);
printf(”\n\t[1] To insert an element”);
printf(”\n\t[2] To remove an element”);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
switch(ch)
{
case 1:
{
printf(”\nElement to be inserted:”);
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;
}
case 4:
exit(0);
default:
printf(”\tInvalid Choice.”);
getch();
break;
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
}
}
/*end main*/
if(queue->r == size – 1)
queue->r = -1;
queue->a[++queue->r] = j;
queue->num++;
return true;
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
j = queue->a[queue->f++];
if(queue->f == size)
queue->f = 0;
queue->num–;
return j;
}
2 /* Write C programs that implement Queue (its operations) using ii) Pointers */
#define true 1
#define false 0
#include
#include
#include
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()
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
{
int ele,choice,j;
while(1)
{
clrscr();
printf(”\n\n****IMPLEMENTATION OF QUEUE USING POINTERS****\n”);
printf(”==============================================”);
printf(”\n\t\t MENU\n”);
printf(”==============================================”);
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);
switch(choice)
{
case 1:
{
printf(”\n\tElement to be inserted:”);
scanf(”%d”,&ele);
add_ele(ele);
getch();
break;
}
case 2:
{
if(!e_que())
{
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();
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
getch();
break;
case 4:
exit(1);
break;
default:
printf(”\n\tInvalid choice.”);
getch();
break;
}
}
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
{
int j = f_ptr->ele;
queue=f_ptr;
f_ptr = f_ptr->n;
free (queue);
return j;
}
else
{
printf(”\n\tQueue is empty.”);
return -9999;
}
}
Week 17
1/* Write a C program that uses Stack operations to perform the following:
i) Converting infix expression into postfix expression
ii) Evaluating the postfix expression */
#include
#include
int st[100];
int st_top=-1;
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
/*main function*/
void main()
{
char in[100],post[100];
clrscr();
printf(”\n\tEnter the Infix Expression: “);
gets(in);
in_post(in);
getch();
}
/*end main*/
int pop_item()
{
int it;
if(st_top==-1)
{
getch();
}
return(st[st_top--]);
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
push_item(’\0′);
t=in[x];
while(t!=’\0′)
{
if(isalnum(t))
/*For checking whether the value in t is an alphabet or number. */
{
post[y]=t;
y++;
}
else if(t==’(')
{
push_item(’(');
}
else if(t==’)')
{
while(st[st_top]!=’(')
{
c=pop_item();
post[y]=c;
y++;
}
c=pop_item();
}
else
{
while(st_ISP(st[st_top])>=st_ICP(t))
{
c=pop_item();
post[y]=c;
y++;
}
push_item(t);
}
x++;
t=in[x];
}
while(st_top!=-1)
{
c=pop_item();
post[y]=c;
y++;
}
printf(”\n\tThe Postfix Expression is:”);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
for(z=0;z
printf("%c",post[z]);
printf("\n\nDo you want to evaluate the Result of Postfix Expression?(Y/N):");
scanf("%c",&a);
if(a=='y' || a=='Y')
{
result=cal(post);
printf("\n\n\tResult is: %d\n",result);
getch();
}
else if(a=='n' || a=='N')
{
exit(0);
}
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
switch(post[j])
{
case '+':x=n+m;
break;
case '-':x=n-m;
break;
case '*':x=n*m;
break;
case '/':x=n/m;
break;
}
push_item(x);
}
j++;
}
if(st_top>0)
{
printf(”Number of Operands are more than Operators.”);
exit(0);
}
else
{
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
y=pop_item();
return (y);
}
return 0;
}
Week 18
#include
#include
#include
struct treenode
{
int ele;
struct treenode *l_child, *r_child;
};
/*main function*/
void main()
{
struct treenode *root_node = NULL;
int num,value;
int choice;
clrscr();
printf(”—————————————————-\n”);
printf(”\t\t\tMENU\n”);
printf(”—————————————————–\n”);
printf(”[1] Create a Binary Tree and Use Inorder Traversal\n”);
printf(”[2] Create a Binary Tree and Use Preorder Traversal\n”);
printf(”[3] Create a Binary Tree and Use Postorder Traversal\n”);
printf(”—————————————————–\n”);
printf(”Enter your choice:”);
scanf(”%d”,&choice);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
while(num-- > 0)
{
printf(”\n\nEnter the data value:”);
scanf(”%d”,&value);
root_node = insert_node(root_node,value);
}
switch(choice)
{
case 1:
printf(”\n\nBinary tree using Inorder Traversal : “);
TraverseInorder(root_node);
getch();
break;
case 2:
printf(”\n\nBinary tree using Preorder Traversal : “);
TraversePreorder(root_node);
getch();
break;
case 3:
printf(”\n\nBinary tree using Postorder Traversal : “);
TraversePostorder(root_node);
getch();
break;
default:
printf(”Invalid Choice”);
break;
}
}
}
/*end main*/
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
while(temp_node1 != NULL)
{
temp_node2 = temp_node1;
if( temp_node1 ->ele > a)
temp_node1 = temp_node1->l_child;
else
temp_node1 = temp_node1->r_child;
}
if( temp_node2->ele > a)
{
temp_node2->l_child = (struct treenode*)malloc(sizeof(struct treenode));
temp_node2 = temp_node2->l_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 = NULL;
}
else
{
temp_node2->r_child = (struct treenode*)malloc(sizeof(struct treenode));
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 = NULL;
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
}
return(t);
}
Week 19
1. /* Write C programs that use both recursive and non recursive functions to perform
the following searching operations for a Key value in a given list of integers :
ii) Binary search*/
#include
#define MAX_LEN 10
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
/* Non-Recursive function*/
void b_search_nonrecursive(int l[],int num,int ele)
{
int l1,i,j, flag = 0;
l1 = 0;
i = num-1;
while(l1 <= i)
{
j = (l1+i)/2;
if( l[j] == ele)
{
printf("\nThe element %d is present at position %d in list\n",ele,j);
flag =1;
break;
}
else
if(l[j] < ele)
l1 = j+1;
else
i = j-1;
}
if( flag == 0)
printf("\nThe element %d is not present in the list\n",ele);
}
/* Recursive function*/
int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a)
{
int m,pos;
if (arrayStart<=arrayEnd)
{
m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a
return b_search_recursive(l,arrayStart,m-1,a);
else
return b_search_recursive(l,m+1,arrayEnd,a);
}
return -1;
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
for(i=0;i
scanf("%d",&l[i]);
}
/*main function*/
void main()
{
int l[MAX_LEN], num, ele,f,l1,a;
int ch,pos;
clrscr();
printf("======================================================");
printf("\n\t\t\tMENU");
printf("\n=====================================================");
printf("\n[1] Binary Search using Recursion method");
printf("\n[2] Binary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf(”\nRecursive method:\n”);
pos=b_search_recursive(l,0,num,ele);
if(pos==-1)
{
printf(”Element is not found”);
}
else
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
{
printf(”Element is found at %d position”,pos);
}
getch();
break;
2. /* Write C programs that use both recursive and non recursive functions
to perform the following searching operation for a Key value in a given list of integers :
i) Linear search */
#include
#define MAX_LEN 10
void main()
{
int l[MAX_LEN], num, ele;
int ch;
clrscr();
printf(”======================================================”);
printf(”\n\t\t\tMENU”);
printf(”\n=====================================================”);
printf(”\n[1] Linary Search using Recursion method”);
printf(”\n[2] Linary Search using Non-Recursion method”);
printf(”\n\nEnter your Choice:”);
scanf(”%d”,&ch);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
scanf(”%d”,&num);
read_list(l,num);
printf(”\nElements present in the list are:\n\n”);
print_list(l,num);
printf(”\n\nElement you want to search:\n\n”);
scanf(”%d”,&ele);
switch(ch)
{
case 1:printf(”\n**Recursion method**\n”);
l_search_recursive(l,num,ele);
getch();
break;
/* Non-Recursive method*/
void l_search_nonrecursive(int l[],int num,int ele)
{
int j, f=0;
for(j=0;j
if( l[j] == ele)
{
printf(”\nThe element %d is present at position %d in list\n”,ele,j);
f=1;
break;
}
if(f==0)
printf(”\nThe element is %d is not present in the list\n”,ele);
}
/* Recursive method*/
void l_search_recursive(int l[],int num,int ele)
{
int f = 0;
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
Week 20
#include
#define MAX 10
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
*n = temp;
}
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);
bub_sort(list,num);
printf("\n\nElements in the list after sorting are:\n");
printlist(list,num);
getch();
}
2. /* Write C program that implement the following sorting methods to sort a given list of integers in
ascending order: ii) Quick sort */
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
#include
#define MAX 10
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
for(j=0;j
scanf(”%d”,&list[j]);
}
void main()
{
int list[MAX], num;
clrscr();
printf(”\n***** Enter the number of elements Maximum [10] *****\n”);
scanf(”%d”,&num);
read_data(list,num);
printf(”\n\nElements in the list before sorting are:\n”);
print_data(list,num);
quicksort(list,0,num-1);
printf(”\n\nElements in the list after sorting are:\n”);
print_data(list,num);
getch();
}
Week 21
1. /* Write C program that implement the following sorting methods to sort a given list of integers in
ascending order: i) Insertion sort */
#include
#include
void main()
{
int num[5],count;
clrscr();
printf(”\nEnter the Five Elements to sort:\n”);
for (count=0;count<5;count++)
scanf("%d",&num[count]);
inst_sort(num);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
2. /* Write C program that implement the following sorting methods to sort a given list of integers in
ascending order:
ii) Merge sort */
#include
#include
#define MAX_ARY 10
int main(void) {
int ary[MAX_ARY];
int j = 0;
printf("\n");
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
printf("\n");
getch();
}
if(end == start)
return;
mrg1 = 0;
mrg2 = mid - end + 1;
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
Week 22
1. /* Write C program to implement the Lagrange interpolation.*/
#include
#include
#define MaxN 90
void main()
{
float arr_x[MaxN+1], arr_y[MaxN+1], numerator, denominator, x, y=0;
int i, j, n;
clrscr();
printf(”Enter the value of n: \n”);
scanf(”%d”, &n);
printf(”Enter the values of x and y: \n”);
for(i=0; i<=n; i++)
scanf("%f%f", &arr_x[i], &arr_y[i]);
printf("Enter the value of x at which value of y is to be calculated: ");
scanf("%f", &x);
for (i=0; i<=n; i++)
{
numerator=1;
denominator=1;
for (j=0; j<=n; j++)
if(j!=i)
{
numerator *= x-arr_x[j];
denominator *= arr_x[i]-arr_x[j];
}
y+=(numerator/denominator)*arr_y[i];
}
printf("When x=%4.1f y=%7.1f\n",x,y);
getch();
}
#include
#include
#define MaxN 100
#define Order_of_diff 4
void main ()
{
float arr_x[MaxN+1], arr_y[MaxN+1], numerator=1.0, denominator=1.0, x, y, p, h,
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
diff_table[MaxN+1][Order_of_diff+1];
int i,j,n,k;
clrscr();
while(!(arr_x[i]>x)) /* Finding x0 */
i++;
i–;
p=(x-arr_x[i])/h;
y=arr_y[i];
Week 23
#include
#include
#include
#include
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
void main()
{
float a[20],b[20],dx[20],dy[20];
float sy=0,sx=0,mean_x=0,mean_y=0,sum_xy=0;
float corr_coff=0,reg_coff_xy=0, reg_coff_yx=0;
char type_coff[7];
int n=0,i=0;
clrscr();
for(i=0;i
sum_xy=sum_xy+dx[i]*dy[i];
corr_coff=sum_xy/(n*sx*sy);
printf(”Enter the type of regression coefficient as ‘x on y’ or ‘y on x’: “);
fflush(stdin);
gets(type_coff);
if(strcmp(type_coff,”x on y”)==1)
{
reg_coff_xy=corr_coff*(sx/sy);
printf(”\nThe value of linear regression coefficient is %f”,reg_coff_xy);
}
else if(strcmp(type_coff,”y on x”)==1)
{
reg_coff_yx=corr_coff*(sy/sx);
printf(”\nThe value of linear regression coefficient is %f”,reg_coff_yx);
}
else
printf(”\nEnter the correct type of regression coefficient.”);
getch();
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
void deviation(float *a, float mean, int n, float *d, float *s)
{
float sum=0,t=0;
int i=0;
for(i=0;i
{
d[i]=a[i]-mean;
t=d[i]*d[i];
sum=sum+t;
}
sum=sum/n;
*s=sqrt(sum);
}
Week 24
1. /* Write C program to implement Simpson method. */
#include
#include
#include
char postfix[80];
float stack[80];
char stack1[80];
int top=-1,top1=-1;
float eval(char postfix[], float x1);
void infix_postfix(char infix[]);
main()
{
float x0, xn, h, s,e1,e2, e3;
char exp[80], arr[80];
int i,n,l=0;
clrscr();
printf(”\nEnter an expression: “);
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
gets(exp);
puts(”Enter x0, xn and number of sub-intervals: “);
scanf(”%f%f%d”, &x0, &xn, &n);
h=(xn-x0)/n;
if(exp[0]==’l'&& exp[1]==’o'&& exp[2]==’g')
{
l=strlen(exp);
for(i=0;i
arr[0]=exp[i+3];
arr[i]='\0';
infix_postfix(arr);
e1=eval(postfix,x0);
e2=eval(postfix,xn);
e3=4*eval(postfix, x0+h);
s=log(e1)+log(e2)+log(e3);
for (i=3;i<=n-1;i+=2)
s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);
}
else
{
infix_postfix(exp);
s=eval(postfix,x0)+eval(postfix,xn)+4*eval(postfix, x0+h);
for (i=3;i<=n-1;i+=2)
s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);
}
printf("The value of integral is %6.3f\n",(h/3)*s);
return(0);
}
/*Inserting the operands in a stack. */
void push(float item)
{
if(top==99)
{
printf("\n\tThe stack is full");
getch();
exit(0);
}
else
{
top++;
stack[top]=item;
}
return;
}
/*Removing the operands from a stack. */
float pop()
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
{
float item;
if(top==-1)
{
printf("\n\tThe stack is empty\n\t");
getch();
}
item=stack[top];
top--;
return (item);
}
void push1(char item)
{
if(top1==79)
{
printf("\n\tThe stack is full");
getch();
exit(0);
}
else
{
top1++;
stack1[top1]=item;
}
return;
}
/*Removing the operands from a stack. */
char pop1()
{
char item;
if(top1==-1)
{
printf("\n\tThe stack1 is empty\n\t");
getch();
}
item=stack1[top1];
top1--;
return (item);
}
/*Converting an infix expression to a postfix expression. */
void infix_postfix(char infix[])
{
int i=0,j=0,k;
char ch;
char token;
for(i=0;i<79;i++)
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
postfix[i]=' ';
push1('?');
i=0;
token=infix[i];
while(token!='\0')
{
if(isalnum(token))
{
postfix[j]=token;
j++;
}
else if(token=='(')
{
push1('(');
}
else if(token==')')
{
while(stack1[top1]!='(')
{
ch=pop1();
postfix[j]=ch;
j++;
}
ch=pop1();
}
else
{
while(ISPriority(stack1[top1])>=ICP(token))
{
ch=pop1();
postfix[j]=ch;
j++;
}
push1(token);
}
i++;
token=infix[i];
}
while(top1!=0)
{
ch=pop1();
postfix[j]=ch;
j++;
}
postfix[j]=’\0′;
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
/*Determining the priority of elements that are placed inside the stack. */
int ISPriority(char token)
{
switch(token)
{
case ‘(’:return (0);
case ‘)’:return (9);
case ‘+’:return (7);
case ‘-’:return (7);
case ‘*’:return (8);
case ‘/’:return (8);
case ‘?’:return (0);
default: printf(”Invalid expression”);
}
return 0;
}
/*Determining the priority of elements that are approaching towards the stack. */
int ICP(char token)
{
switch(token)
{
case ‘(’:return (10);
case ‘)’:return (9);
case ‘+’:return (7);
case ‘-’:return (7);
case ‘*’:return (8);
case ‘/’:return (8);
case ‘\0′:return (0);
default: printf(”Invalid expression”);
}
return 0;
}
/*Calculating the result of expression, which is converted in postfix notation. */
float eval(char p[], float x1)
{
float t1,t2,k,r;
int i=0,l;
l=strlen(p);
while(i
{
if(p[i]=='x')
push(x1);
else
if(isdigit(p[i]))
{
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
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;
}
#include
#include
#include
char postfix[80];
float stack[80];
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
char stack1[80];
int top=-1,top1=-1;
main()
{
float x0, xn, h, s,e1,e2;
char exp[80], arr[80];
int i,n,l=0;
clrscr();
printf(”\nEnter an expression: “);
gets(exp);
puts(”Enter x0, xn and number of subintervals”);
scanf(”%f%f%d”, &x0, &xn, &n);
h=(xn-x0)/n;
if(exp[0]==’l'&& exp[1]==’o'&& exp[2]==’g')
{
l=strlen(exp);
for(i=0;i
arr[0]=exp[i+3];
arr[i]='\0';
infix_postfix(arr);
e1=eval(postfix,x0);
e2=eval(postfix,xn);
s=log(e1)+log(e2);
for (i=1;i<=n-1;i++)
s+=2*log(eval(postfix,x0+i*h));
}
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);
}
printf("Value of the integral is %6.3f\n",(h/2)*s);
return(0);
}
/*Inserting the operands in a stack. */
void push(float item)
{
if(top==99)
{
printf("\n\tThe stack is full");
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
getch();
exit(0);
}
else
{
top++;
stack[top]=item;
}
return;
}
/*Removing the operands from a stack. */
float pop()
{
float item;
if(top==-1)
{
printf("\n\tThe stack is empty\n\t");
getch();
}
item=stack[top];
top--;
return (item);
}
void push1(char item)
{
if(top1==79)
{
printf("\n\tThe stack is full");
getch();
exit(0);
}
else
{
top1++;
stack1[top1]=item;
}
return;
}
/*Removing the operands from a stack. */
char pop1()
{
char item;
if(top1==-1)
{
printf("\n\tThe stack1 is empty\n\t");
getch();
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
}
item=stack1[top1];
top1--;
return (item);
}
while(ISPriority(stack1[top1])>=ICP(token))
{
ch=pop1();
/*Assigning the popped element into the postfix array. */
postfix[j]=ch;
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
j++;
}
push1(token);
}
i++;
token=infix[i];
}
while(top1!=0)
{
ch=pop1();
postfix[j]=ch;
j++;
}
postfix[j]=’\0′;
}
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
}
return 0;
}
/*Calculating the result of expression, which is converted in postfix notation. */
float eval(char p[], float x1)
{
float t1,t2,k,r;
int i=0,l;
l=strlen(p);
while(i
{
if(p[i]=='x')
push(x1);
else
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");
break;
}
push(k);
}
i++;
}
if(top>0)
{
printf(”You have entered the operands more than the operators”);
exit(0);
}
else
http:www.placementpapers.net
Downloaded from: http:www.placementpapers.net
{
r=pop();
return (r);
}
return 0;
}
http:www.placementpapers.net