wipro technologies pet evaluation tool

Post on 25-Nov-2014

121 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1.Write a program that computes an expression.

//evaluate an expression#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(int argc,char* argv[]){

int a,b,c,d,v;

a=atoi(argv[1]);b=atoi(argv[2]);c=atoi(argv[3]);d=atoi(argv[4]);

v=((2*a)*(5*b))+c-((6*d)*(5*c));

printf("%d\n",v);}--------------------------------------------------------------------------------------

2.Write a program that prints out the first 'n' numbers in the Fibonacci sequence.

//fib series#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(int argc,char* argv[]){

int n=atoi(argv[1]);int f1,f2,f3,i=1;

f1=1;f2=0;f3=f1+f2;printf("%d\n",f3);while(i<n){

f1=f2;f2=f3;f3=f1+f2;printf("%d\n",f3);i++;

}}---------------------------------------------------------------------------------------

3.Write a program that reverses an integer.

//rev a number

#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(int argc,char* argv[]){

int n=atoi(argv[1]);int r,s=0;;while(n>0){

r=n%10;s=s*10+r;n/=10;

}printf("%d\n",s);

}---------------------------------------------------------------------------------------

4.Write a program to print a field spanning from 5th to 14th bit positions.

//hex digit extract#include<stdlib.h>#include<stdio.h>#include<conio.h>void main(int argc,char *argv[]){

int hex,b;sscanf(argv[1],"%x",&hex);b=0X3FF0;hex=hex&b;hex=hex>>4;printf("%X",hex);

}----------------------------------------------------------------------------------------5.Write a program to find the sum of series 1+x+x2+x3+x4+....+xn.

//sum of series#include<stdio.h>#include<stdlib.h>#include<math.h>void main(int argc,char* argv[])

{int n,i,x;long int s=0;x=atoi(argv[1]);n=atoi(argv[2]);for(i=0;i<=n;i++){

s=s+(long int)pow(x,i);}

printf("%ld\n",s);}------------------------------------------------------------------------------------------6.Write a program that prints the transpose of a matrix

//trans of a matrix#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<math.h>void main(int argc,char* argv[]){

FILE *fp;int mat[10][10];int i,j,n,c=0,c1;fp=fopen(argv[1],"r");if(fp==NULL){

printf("File doesnot Exist");exit(0);

}

while(!feof(fp)){

fscanf(fp,"%d",&n);c++;

}rewind(fp);

c1=(int)sqrt(c);

while(!feof(fp)){

for(i=0;i<c1;i++){

for(j=0;j<c1;j++){

mat[i][j]=0;fscanf(fp,"%d",&mat[i][j]);

}

}

}

for(i=0;i<c1;i++){

for(j=0;j<c1;j++){

printf("%d ",mat[j][i]);}printf("\n");

}

fclose(fp);

}-----------------------------------------------------------------------------------------------7.Write a program to compute the sum of natural numbers.

//sum of first n natural nos#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(int argc,char* argv[]){

long int n;long int i,s=0;n=atoi(argv[1]);for(i=1;i<=n;i++){

s=s+i;}printf("%ld\n",s);

}

-------------------------------------------------------------------------------------------------

8.Write a program to print the even numbers.

//Print the Even numbers#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(int argc,char* argv[]){

int n,i;n=atoi(argv[1]);for(i=2;i<n;i++){

if(i%2==0)printf("%d ",i);

}printf("\n");

}----------------------------------------------------------------------------------------------------

9.Write a program that prints out the sum of the integers in the file.

//sum of integers from a file

#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(int argc,char* argv[]){

FILE *fp;int s=0,n;fp=fopen(argv[1],"r");if(fp==NULL){

printf("File doesn't Exist");exit(0);

}while(!feof(fp)){

fscanf(fp,"%d",&n);s+=n;

}printf("%d\n",s);fclose(fp);

}

------------------------------------------------------------------------------------------------------10.Write a program to display the given file contents in rank order.

//rank-order#include<stdio.h>#include<string.h>#include<conio.h>#include<stdlib.h>void main(int argc,char* argv[]){

FILE *fp;char nam[20][20];char ntemp[20],name[20];int age[20];int mark[20],i,j=0;int t;int l=0,ag,mrk;

fp=fopen(argv[1],"r");if(fp==NULL){

printf("File not Exist");exit(0);

}while(!feof(fp)){

fscanf(fp,"%s %d %d\n",name,&ag,&mrk);strcpy(nam[l],name);age[l]=ag;mark[l]=mrk;l++;

}for(i=0;i<l;i++){

for(j=i+1;j<l;j++){

if(mark[i]<mark[j]){

t=mark[j];mark[j]=mark[i];mark[i]=t;

strcpy(ntemp,nam[j]);strcpy(nam[j],nam[i]);strcpy(nam[i],ntemp);

t=age[j];age[j]=age[i];age[i]=t;

}}

}for(i=0;i<l;i++)

{printf("%s %d %d\n",nam[i],age[i],mark[i]);

}fclose(fp);

}----------------------------------------------------------------------------------------------

11.Write a program that concatenates two strings

//concatenate 2 strings into 3rd string#include<stdio.h>#include<conio.h>#include<string.h>void main(int argc,char* argv[]){

char s1[20],s2[20],s3[40];int i,j,k;int l1,l2;strcpy(s1,argv[1]);strcpy(s2,argv[2]);l1=strlen(s1);l2=strlen(s2);

for(i=0,k=0;i<l1;i++,k++)s3[k]=s1[i];

for(j=0;j<l2;j++,k++)s3[k]=s2[j];

s3[k]='\0';printf("%s\n",s3);

}-----------------------------------------------------------------------------------------------------------

12.Write a program to print the name of all the students who have passed the PRP exam from a unix batch.

//to find the pass students in unix#include<stdio.h>

#include<conio.h>#include<stdlib.h>#include<string.h>void main(int argc,char* argv[]){

FILE *fp1,*fp2;char n[20][20],nam[30],d[30][30],dept[30],j;int mark[20],mrk,i=0,temp;

fp1=fopen(argv[1],"r");fp2=fopen(argv[2],"r");if(fp1==NULL || fp2==NULL){

printf("no file");exit(0);

}while(!feof(fp1)){

fscanf(fp1,"%s %d",nam,&mrk);strcpy(n[i],nam);mark[i]=mrk;i++;

}temp=i;i=0;while(!feof(fp2)){

fscanf(fp2,"%s %s",nam,dept);strcpy(n[i],nam);strcpy(d[i],dept);i++;

}

j=0;for(i=0;i<temp;i++){

if((strcmp(d[i],"unix"))==0){

//ind[j++]=i;if(mark[i]>=70)

printf("%s\n",n[i]);

}

}}---------------------------------------------------------------------------------------------------------13.Write a program that will print all possible combination of letters in a word lexicographically. ---------------------------------------------------------------------------------------------------------14.Write a program that reverses the content of each line in a file

/*read text file and print in reverse order*/

#include<stdio.h>#include<conio.h>#include<string.h>

void main(int argc,char* argv[]){

FILE *fp;char ar[100];fp=fopen(argv[1],"r");while(!feof(fp)){

fgets(ar,1000,fp);strrev(ar);printf("%s\n",ar);

}

}---------------------------------------------------------------------------------------------------------15.Write a program that sorts out the words in a file lexicographically

//lexicographical order#include<stdio.h>#include<conio.h>#include<string.h>void main(int argc,char *argv[]){

FILE *fp;

char n[40],str[50][150],tmp[40],tm[50][150];

int i=0,j,t,x=0;int k;

fp=fopen(argv[1],"r");while(!feof(fp))

{fscanf(fp,"%s",n);strcpy(str[i],n);

i++;}t=i;

t=t-1;

for(i=0;i<t;i++){

for(j=i;j<t;j++){

if((strcmpi(str[i],str[j]))>0){

strcpy(tmp,str[i]);strcpy(str[i],str[j]);strcpy(str[j],tmp);

}

}}

for(i=0;i<t;i++){

strcpy(tm[i],str[i]);

}

for(k=0;k<t;k++){

printf("%s\n",tm[k]);

}fclose(fp);

}

-----------------------------------------------------------------------------------------------------------------------------------------

16.Write a program that accepts two numbers as command-line arguments, compute their L.C.M(Least Common Multiple) and print it.

#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(int argc,char* argv[]){

int n1,n2,a,b,gcd,lcm;int r;

n1=a=atoi(argv[1]);n2=b=atoi(argv[2]);r=a%b;while(r!=0){

a=b;b=r;r=a%b;

}gcd=b;lcm=n1*n2/gcd;

printf("%d\n",lcm);}------------------------------------------------------------------------------------------------------------------------------------------17.Expression

#include<stdio.h>#include<stdlib.h>void main(int argc,char* argv[]){

int a[5],i=0,v;FILE *fp;fp=fopen(argv[1],"r");if(fp==NULL){

printf("File does not Exist");exit(0);

}while(!feof(fp)){

fscanf(fp,"%d",&a[i]);i++;

}

v=a[0]+a[1]+a[2]+a[3];printf("%d\n",v);

}//c:\\inp.txt

---------------------------------------------------------------------------------------

18.GCD

#include<stdio.h>#include<math.h>#include<stdlib.h>void main(int argc,char* argv[]){

int m,n;FILE *fp;fp=fopen(argv[1],"r");if(fp==NULL){

printf("File not exist");exit(0);}while(!feof(fp)){

fscanf(fp,"%d %d",&m,&n);}int rem,gcd;rem=m%n;while(rem!=0){

m=n;n=rem;rem=m%n;

}gcd=n;printf("%d",gcd);

}

------------------------------------------------------------------19.Palindrome

#include<stdio.h>#include<conio.h>#include<string.h>

void main(int argc,char* argv[]){

char b[100],str[100];int tmp,i,j,l;FILE *fp;fp=fopen(argv[1],"r");fscanf(fp,"%s",str);l=strlen(str);tmp=l;b[l]='\0';for(i=0,j=l-1;i<tmp;i++,j--){

b[j]=str[i];}if((strcmp(str,b)==0))

printf("palindorme");else

printf("Not palindorme");}

-----------------------------------------------------------------------

20.Matrix addition input from two files

#include<stdio.h>#include<string.h>#include<stdlib.h>

void readmat(char* fa,int mat[][10],int*r,int*c){

char s [100];FILE *fp=fopen(fa,"r");int i=0,j=0;char *p;while(fgets(s,100,fp)!=NULL){

p=strtok(s," ");mat[i][j]=atoi(p);while((p=strtok(NULL," "))!=NULL){

++j;mat[i][j]=atoi(p);

}++i;*c=j+1;j=0;

}*r=i;

}

int main(int argc,char* argv[]){

int a[10][10],b[10][10],r,c,i,j;//FILE *fp1=fopen(argv[3],"w");readmat(argv[1],a,&r,&c);readmat(argv[2],b,&r,&c);

printf("First Matrix read from file1\n");for(i=0;i<c;i++){

for(j=0;j<r;j++){

printf("%d\t",a[i][j]);}printf("\n");

}printf("Second Matrix read from file2 \n");for(i=0;i<c;i++){

for(j=0;j<r;j++){

printf("%d\t",b[i][j]);}printf("\n");

}printf("Resultant Matrix printing to another file ");printf("Sum is \n");for(i=0;i<c;i++){

for(j=0;j<r;j++){

printf("%d\t",(a[i][j]+b[i][j]));}printf("\n");

}

return 0;}

-----------------------------------------------------------------------------------------------

21.Alternative fib series 1 2 5 13

#include <stdio.h>#include<stdlib.h>int main(int argc,char *argv[]){

int p=atoi(argv[1]);int m=0,n=1,i,s;//printf("%d\t",m);printf("%d\t",n);

for(i=0;i<p;i++){

s=m+n;if(i%2==1)

printf("%d\t",s);m=n;n=s;

}

return 0;printf("\n");

}

-----------------------------------------------------------------------22.Transpose of a matrix*/

#include<stdio.h>#include<string.h>#include<stdlib.h>

void readmat(char *filename,int mat[][10],int *r,int *c){

char s[100];FILE *fp=fopen(filename,"r");int i=0,j=0;char *p;while(fgets(s,100,fp)!=NULL){

p=strtok(s," ");mat[i][j]=atoi(p);while((p=strtok(NULL," " ))!=NULL){

++j;

mat[i][j]=atoi(p);}++i;*c=j+1;j=0;

}*r=i;

}

int main(int argc, char *argv[]){ int a[10][10],r,c,i,j;

readmat(argv[1],a,&r,&c);printf("Rows = %d\n", r);printf("Cols = %d\n", c);for(i=0;i<c;i++){ for(j=0;j<r;j++)

printf("%d ",a[j][i]);printf("\n");

}

return 0;}-----------------------------------------------------------------------------------------------------------------------

23.adding rows value and creating new column*/#include<stdio.h>#include<string.h>#include<stdlib.h>

void readmat(char* fa,int mat[][10],int*r,int*c){

char s [100];int k;FILE *fp=fopen(fa,"r");int i=0,j=0;char *p;int sum[10];for(k=0;k<10;k++){ sum[k]=0;}while(fgets(s,100,fp)!=NULL){

p=strtok(s," ");mat[i][j]=atoi(p);

while((p=strtok(NULL," "))!=NULL){

++j;mat[i][j]=atoi(p);

}++i;*c=j+1;j=0;

}*r=i;for(int u=0;u<*r;u++)

for(int v=0;v<*c;v++)sum[u]=sum[u]+ mat[u][v];

for(i=0;i<*r;i++){

for(j=0;j<*c;j++){

printf("%d\t",mat[i][j]);}printf("%d\t",sum[i]);printf("\n");

}

}

int main(int argc,char* argv[]){

int a[10][10],b[10][10],r,c,i,j;printf("First Matrix read from file1\n");readmat(argv[1],a,&r,&c);

printf("Second Matrix read from file2 \n");readmat(argv[2],b,&r,&c);printf("Resultant Matrix printing to another file ");printf("Sum is \n");for(i=0;i<c;i++){

for(j=0;j<r;j++){

printf("%d\t",(a[i][j]+b[i][j]));}printf("\n");

}return 0;

}--------------------------------------------------------------------------------------------------24.90 degree matrix*/

#include<stdio.h>#include<conio.h>int main(){

int a[3][3],b[3][3];int m=2,n=2,i,j;int row=m, col=m-n;for(i=0;i<3;i++)

for(j=0;j<3;j++)scanf("%d",&a[i][j]);

printf("The given marix is : \n"); for(i=0;i<3;i++)

{ for(j=0;j<3;j++){ printf("%d\t",a[i][j]);

}printf("\n");

}for(i=0;i<3;i++)

for(j=0;j<3;j++)b[i][j]=0;

for(i=0;i<3;i++){ for(j=0;j<3;j++)

{ b[i][j]=a[row][col];row--;

}row=m;col=col+1;

}printf("\n"); for(i=0;i<3;i++){ for(j=0;j<3;j++)

{ printf("%d\t",b[i][j]); }

printf("\n");}return 0;

}------------------------------------------------------------------------------------------------------

25.Printing even number 2 4 6 8 */

#include<stdio.h>

#include<stdlib.h>int main(int argc,char *argv[]){

int number,i;number=atoi(argv[1]);for(i=2;i<number;i=i+2){

if((i%2)==0)printf("%d ",i);

}return 0;

}

/*reversing a line*/

#include<stdio.h>#include<string.h>int main(int argc, char *argv[]){ char s[100];

int i;FILE *fp = fopen(argv[1],"r");while(fgets(s,100,fp)!= NULL){ for(i=strlen(s)-1;i>=0;i--)

if(s[i]!=NULL)printf("%c",s[i]);

printf("\n");}return 0;

}----same ------------------#include<stdio.h>#include<string.h>#include<stdlib.h>int main(int argc,char* agv[]){

char s[100];int i,n=0;FILE *fp=fopen(agv[1],"r");while(fgets(s,100,fp)!=NULL){

for(i=strlen(s)-1;i>=0;i--){

if(s[i]!='\n')

printf("%c",s[i]);}printf("\n");

}//printf("\n");

return 0; }

}------------------------------------------------------------------------------------------------------------------ 26.Lexical sorting of names*/

#include<stdio.h>#include<string.h>#include<stdlib.h>

int main(int argc, char *argv[]){ char *s[100];

char t[100],*temp;int len=0,i,j;FILE *fp = fopen(argv[1],"r");

while(fscanf(fp,"%s",t)!=EOF){

s[len++]=strdup(t);}for(i=0;i<len-1;++i)

for(j=0;j<len-i-1;++j)if(strcmpi(s[j],s[j+1])>0){

temp=s[j];s[j]=s[j+1];s[j+1]=temp;

}else if(strcmpi(s[j],s[j+1])==0){

if(strcmp(s[j],s[j+1])<0){

temp=s[j];s[j]=s[j+1];s[j+1]=temp;

}}

for(i=0;i<len;++i)printf("%s\n",s[i]);

return 0;}-----------------------------------------------------------------------27.1+x^2+x^3+x^4+...+x^n */

#include<stdio.h>#include<stdlib.h>#include<math.h>int main(int argc,char *argv[]){

int n1,n2,sum=0,i,power;n1=atoi(argv[1]);n2=atoi(argv[2]);power=1;for(i=1;i<=n2;i++){ power=power*n1;

sum=sum+power;}printf("%d",sum+1);return 0;

}----------------------------------------------------------------------28.gettibg the values from the file...and thn sorting s done...

#include<stdio.h>void main(int argc,char* argv[]){

int count=0,i=0,b[20],a[20],sum=0,temp,j,k=0;int avg;

FILE *fp;fp=fopen(argv[1],"r");while(!feof(fp)){

fscanf(fp,"%d",&a[i]);i++;count++;

}fclose(fp);for(i=0;i<count;i++){

sum=sum+a[i];}

printf("%d%d\n",sum,count);avg=(sum/count);

printf("%d\n",avg);for(i=0;i<count;i++){if(a[i]<avg){b[k++]=a[i];

}}for(i=0;i<k;i++){

printf("%d\t",b[i]);}

for(i=0;i<k-1;i++){

for(j=i+1;j<k;j++){

if(b[i]>b[j]){

temp=b[i];b[i]=b[j];b[j]=temp;

}}

}

for(i=0;i<k;i++){

printf("%d\t",b[i]);}}

---------------------------------------------------------------------------------------------------------------------------------------------Data Structures and algorithm---------------------------------------------------------------------------------------------------------------------------------------------1.Single Linked List

#include<stdio.h>#include<stdlib.h>struct Slist{

int iData;struct Slist* psNext;

};typedef struct Slist theNode;

theNode* theHead=NULL,*prev=NULL; void fnCreateList(FILE*);void fnDisplay(theNode*);

int main(int iArgc, char *pcArgv[]){

FILE* fp;fp=fopen(pcArgv[1],"r");fnCreateList(fp);printf("Created linked list is \n");fnDisplay(theHead);return (0);

}

void fnCreateList(FILE* fp){

theNode *new1; int m;

while(!feof(fp))

{fscanf(fp,"%d",&m);new1=(theNode*)malloc(sizeof(theNode));

new1->iData=m; new1->psNext=NULL;

if(theHead==NULL){

theHead=new1;theHead->psNext=NULL;

}else{

prev=theHead;while(prev->psNext!=NULL){

prev=prev->psNext;}prev->psNext=new1;new1->psNext=NULL;

}

}}

void fnDisplay(theNode* psTraverse){

if(theHead==NULL)printf("Data doesnot exists\n");

else{

for(prev=theHead;prev;prev=prev->psNext)printf("%d ",prev->iData);

}}

-------------------------------------------------------------------------------------------------------------------------2.Binary Search Tree

#include<stdio.h>#include<stdlib.h>struct SBst{

int iData;

struct SBst* psLeft;struct SBst* psRight;

};

typedef struct SBst theNode;theNode* theRoot=NULL;void fnCreateTree(FILE*);void fnInorderTraversal(theNode*);

int main(int iArgc, char* pcArgv[]){

FILE *fpFilePointer; fpFilePointer=fopen(pcArgv[1],"r");fnCreateTree(fpFilePointer);printf("Inorder traversal of the Binary search tree is\n");

fnInorderTraversal(theRoot); return (0);

} theNode* btnew=NULL;theNode* btmp=NULL;theNode* prev; void fnCreateTree(FILE* fpFilePointer){

int n=0;while(!feof(fpFilePointer)){

btnew=(theNode*)malloc(sizeof(theNode));fscanf(fpFilePointer,"%d",&n);btnew->psLeft=NULL;btnew->iData=n;btnew->psRight=NULL;

if(theRoot==NULL){

theRoot=btnew;}else{

btmp=theRoot;prev=NULL;while(btmp!=NULL){prev=btmp;if(n<btmp->iData)btmp=btmp->psLeft;else

btmp=btmp->psRight;}

if(n<prev->iData)prev->psLeft=btnew;

elseprev->psRight=btnew;

}

}

} void fnInorderTraversal(theNode* psNode){

if(psNode!=NULL){

fnInorderTraversal(psNode->psLeft);printf("%d ",psNode->iData);fnInorderTraversal(psNode->psRight);

}} --------------------------------------------------------------------------------------------------------------------3. Double Linked List

#include<stdio.h>#include<stdlib.h>

struct SDll{

int iData;struct SDll* psLeft;struct SDll* psRight;

};

typedef struct SDll theNode;/* theHead will point to the start of the doubly linked list */ theNode* theHead=NULL;

/*Function Prototype */ void fnCreateList(FILE*);void fnDisplay(theNode*);

int main(int iArgc,char* pcArgv[])

{FILE* fpFile;fpFile=fopen(pcArgv[1],"r");fnCreateList(fpFile);printf("Created doubly linked list is\n");fnDisplay(theHead);return (0);

}void fnCreateList(FILE* fp)

{int m;

theNode* new1,*prev;/* Paste your code to create the doubly linked list here*/while(!feof(fp)){

fscanf(fp,"%d",&m);new1=(theNode*)malloc(sizeof(theNode));

new1->iData=m; new1->psLeft=NULL;

new1->psRight=NULL;if(theHead==NULL){

theHead=new1; theHead->psLeft=NULL;

new1->psRight=NULL;

}else{prev=theHead;while(prev->psRight!=NULL)prev=prev->psRight;prev->psRight=new1;new1->psLeft=prev;new1->psRight=NULL;}

}}/* fnDisplay will display the doubly linked listed from start to end */ void fnDisplay(theNode* psTraverse){theNode* prev;

/*Paste your code to display the doubly linked list here*/if(theHead==NULL)

printf("Data doesnot exists\n");

else{

for(prev=theHead;prev;prev=prev->psRight)printf("%d ",prev->iData);

}}---------------------------------------------------------------------------------------------------------------------

4. Stack

#include<stdio.h>#include<stdlib.h>

struct SStack{

int iStackData;struct SStack* psLink;

};typedef struct SStack theStack;

/*Top will point to the Top position of the Stack */theStack* theTop=NULL;

/* Function Prototypes */ void fnCreateStack(FILE*);void fnDisplayStack(theStack*);

/* Main function */ int main(int iArgc, char *pcArgv[]){

/*fpFile will hold the pointer to the file where the contents to be put on the stack are saved */

FILE* fpFile;fpFile=fopen(pcArgv[1],"r");fnCreateStack(fpFile);printf("Stack contents are\n");fnDisplayStack(theTop);return (0);

}

void fnCreateStack(FILE* fp){theStack* new1; int m;

while(!feof(fp)){

fscanf(fp,"%d",&m);

new1=(theStack*)malloc(sizeof(theStack));new1->iStackData=m;new1->psLink=NULL;if(theTop==NULL)

theTop=new1;

else{

new1->psLink=theTop;theTop=new1;

}

}

}

/* fnDisplayStack will display the contents of the stack from top to bottom */ void fnDisplayStack(theStack* Traverse){theStack* ptr;

/*Paste your code to display the stack here */if(theTop==NULL)

printf("Stack is empty\n");else{for(ptr=theTop;ptr;ptr=ptr->psLink)

printf("%d ",ptr->iStackData);

}

}

----------------------------------------------------------------------------------------------------------------------5.Queue

#include<stdio.h>#include<stdlib.h>struct SQueue

{int iData;struct SQueue* psLink;

};typedef struct SQueue theQueue;

/* theRear is a pointer at the rear end of the queue */theQueue* theRear=NULL;/* theFront is a pointer at the front end of the queue */theQueue* theFront=NULL;

/* Function Prototypes */ void fnCreateQueue(FILE*);void fnDisplay(theQueue*);

int main(int iArgc, char* pcArgv[]){

FILE* fpFile;fpFile=fopen(pcArgv[1],"r");fnCreateQueue(fpFile);printf("Created queue is\n");fnDisplay(theFront);return (0);

}void fnCreateQueue(FILE* fp){

int m;theQueue* new1,*prev;/*Paste your code to create the queue here */

while(!feof(fp)){

fscanf(fp,"%d",&m);new1=(theQueue*)malloc(sizeof(theQueue));

new1->iData=m; new1->psLink=NULL;if(theRear==NULL && theFront==NULL){

theRear=new1;theFront=new1;

theRear->psLink=NULL;

}else{

prev=theRear;while(prev->psLink!=NULL)

{prev=prev->psLink;} prev->psLink=new1;theRear=new1;new1->psLink=NULL;}

}}

/* fnDisplay will display the content of the queue from front to rear */ void fnDisplay(theQueue* psTraverse){

theQueue* prev;/*Paste your code to display the queue from front here */if(theFront==NULL)

printf("Data doesnot exists\n");else{

for(prev=theFront;prev;prev=prev->psLink)printf("%d ",prev->iData);

}}----------------------------------------------------------------------------------------------------------------6.Dll for deleting last but one node

#include<stdio.h> #include<stdlib.h> typedef struct SDll theNode;

struct SDll {int iData; struct SDll* psLeft; struct SDll* psRight; };/* theHead will point to the start of the doubly linked list */

theNode *theHead=NULL;

/*Function Prototype */void fnCreateList(FILE*); void fnDisplay(theNode*); void fnDelNode(theNode*);

int main(int iArgc,char* pcArgv[]) {FILE* fpFile;fpFile=fopen(pcArgv[1],"r"); fnCreateList(fpFile);printf("\nDoubly linked list before delete\n");

fnDisplay(theHead);

fnDelNode(theHead);

printf("\nDoubly linked list after delete\n");

fnDisplay(theHead);return (0); }/* fnDisplay will display the doubly linked listed from start to end */void fnDisplay(theNode* psTraverse) {while(psTraverse!=NULL) {printf("%d ",psTraverse->iData); psTraverse=psTraverse->psRight;}}/* fnCreateList will read the data from the file and create a doubly linked list */void fnCreateList(FILE* fp) {theNode *ref,*temp;int m; while(!feof(fp)) {

ref=(theNode*)malloc(sizeof(theNode));

fscanf(fp,"%d",&m);

ref->iData=m;ref->psLeft=NULL;ref->psRight=NULL;if(theHead==NULL) {theHead=ref;}

else

{

temp=theHead;

while(temp->psRight!=NULL) {temp=temp->psRight;}temp->psRight=ref;ref->psLeft=temp;

//ref=temp;}

}}/* fnSwapNode will swap only the first and last node in the doubly linked list */void fnDelNode(theNode* psStart) {

theNode *d,*t;d=psStart;

while(d->psRight!=NULL) {

t=d; d=d->psRight;

}

t->psLeft->psRight=t->psRight;d->psLeft=t->psLeft;t->psRight=NULL;

}

----------------------------------------------------------------------------------------------------------7.Dll for deleting last but one node

#include<stdio.h> #include<stdlib.h> typedef struct SDll theNode;

struct SDll {int iData; struct SDll* psLeft; struct SDll* psRight; };/* theHead will point to the start of the doubly linked list */

theNode *theHead=NULL;

/*Function Prototype */void fnCreateList(FILE*); void fnDisplay(theNode*); void fnDelNode(theNode*); int main(int iArgc,char* pcArgv[]) {FILE* fpFile;fpFile=fopen(pcArgv[1],"r"); fnCreateList(fpFile);printf("\nDoubly linked list before delete\n");

fnDisplay(theHead);

fnDelNode(theHead);

printf("\nDoubly linked list after delete\n");

fnDisplay(theHead);return (0); }/* fnDisplay will display the doubly linked listed from start to end */void fnDisplay(theNode* psTraverse) {while(psTraverse!=NULL) {printf("%d ",psTraverse->iData); psTraverse=psTraverse->psRight;}}/* fnCreateList will read the data from the file and create a doubly linked list */void fnCreateList(FILE* fp) {theNode *ref,*temp;int m; while(!feof(fp))

{

ref=(theNode*)malloc(sizeof(theNode));

fscanf(fp,"%d",&m);

ref->iData=m;ref->psLeft=NULL;ref->psRight=NULL;if(theHead==NULL) {theHead=ref;}

else {

temp=theHead;

while(temp->psRight!=NULL) {temp=temp->psRight;}temp->psRight=ref;ref->psLeft=temp;

//ref=temp;}

}}/* fnSwapNode will swap only the first and last node in the doubly linked list */void fnDelNode(theNode* psStart) {

theNode *d,*t;d=psStart;

while(d->psRight!=NULL) {

t=d; d=d->psRight;

}

t->psLeft->psRight=t->psRight;

d->psLeft=t->psLeft;t->psRight=NULL;

}------------------------------------------------------------------------------------------------

8.Write a program to swap only the first and last node in the Doubly Linked List

#include<stdio.h> #include<stdlib.h> typedef struct SDll theNode;

struct SDll {int iData; struct SDll* psLeft; struct SDll* psRight; };/* theHead will point to the start of the doubly linked list */

theNode *theHead=NULL;

/*Function Prototype */void fnCreateList(FILE*); void fnDisplay(theNode*); void fnSwapNode(theNode*); int main(int iArgc,char* pcArgv[]) {FILE* fpFile;fpFile=fopen(pcArgv[1],"r"); fnCreateList(fpFile);printf("Doubly linked list before Swaping the node\n"); fnDisplay(theHead);fnSwapNode(theHead);printf("\nDoubly linked list after Swap\n"); fnDisplay(theHead);return (0); }/* fnDisplay will display the doubly linked listed from start to end */void fnDisplay(theNode* psTraverse) {while(psTraverse!=NULL) {printf("%d ",psTraverse->iData);

psTraverse=psTraverse->psRight;}}/* fnCreateList will read the data from the file and create a doubly linked list */void fnCreateList(FILE* fp) {theNode *ref,*temp;int m; while(!feof(fp)) {

ref=(theNode*)malloc(sizeof(theNode));

fscanf(fp,"%d",&m);

ref->iData=m;ref->psLeft=NULL;ref->psRight=NULL;if(theHead==NULL) {theHead=ref;}

else {

temp=theHead;

while(temp->psRight!=NULL) {temp=temp->psRight;}temp->psRight=ref;ref->psLeft=temp;

//ref=temp;}

}}/* fnSwapNode will swap only the first and last node in the doubly linked list */void fnSwapNode(theNode* psStart) {

theNode *d;int m,n; d=psStart;theHead=d;

while(d->psRight!=NULL) {d=d->psRight;}m=d->iData;n=theHead->iData;theHead->iData=m;d->iData=n;}

-----------------------------------------------------------------------------------------------------------------

9.singly linked list*/

#include <stdio.h>#include <string.h>#include <stdlib.h>

struct node {int info;struct node *next;

};

typedef struct node ND;

ND* makenode (int info) {ND *p = (ND *) malloc (sizeof (ND));p -> info = info;p -> next = NULL;return p;

}

ND* addatfront1 (ND *p, ND *head) {p -> next = head;head = p;return head;

}

void addatfront2 (ND *p, ND **head) {p -> next = *head;*head = p;

}

ND* deletefromfront1 (ND *head) {//ND *p;

if (head == NULL)return NULL;

//p = head;head = head -> next;//free (p);return head;

}

ND* insertAtEnd(ND *p,ND *head){

ND *q;q=head;while(q->next!=NULL){ q=q->next;}q->next=p;p->next=NULL;return head;

}void delAtEnd(ND *head){ ND *temp;

for(temp=head;((temp->next)->next)!=NULL;temp=temp->next);temp->next=NULL;//return head;

}

void display (ND *head) {ND *p;

for (p = head; p != NULL; p = p -> next)printf ("%d -> ", p -> info);

printf ("NULL\n");}ND* insertAtPos(ND *p,ND *head,int posn){

ND *temp,*t;int i;if(posn==1 || posn==0)

return addatfront1(p,head);else{ temp=head;

for(i=1;temp->next!=NULL;temp=temp->next,i++);

if(posn>i+1){ printf("specified posn not available\n");

}else{ temp=head;

for(i=2;i<posn;i++){ temp=temp->next;}t=temp;temp=temp->next;p->next=temp;t->next=p;

}}return head;

}ND* delAtPos(ND *head,int posn){ ND *temp,*t;

int i;if(posn==1)

return deletefromfront1(head);else{ temp=head;

for(i=1;temp->next!=NULL;temp=temp->next,i++);if(posn>i){ printf("specified posn not available\n");}else{ temp=head;

for(i=1;i<posn+1;i++){ temp=temp->next;

if(i==posn-2)t=temp;

}t->next=temp;

}}return head;

}

int main () {int c, n,m;ND *head = NULL;ND *p;do{

printf ("1. Insert at front.\n");

printf ("2. Delete from front.\n");printf ("3. Display.\n");printf ("4. Insert at end\n");printf ("5. Delete at end\n");printf ("6. Insert at posn.\n");printf ("7. Delete from posn.\n");printf ("8. Exit.\n");printf ("Enter your option: ");scanf ("%d", &c);fflush (stdin);

switch (c) {case 1:

printf ("Enter an integer: ");scanf ("%d", &n);fflush (stdin);p = makenode (n);head = addatfront1 (p, head);break;

case 2:head = deletefromfront1 (head);break;

case 3:display (head);break;

case 4:printf ("Enter an integer: ");scanf ("%d", &n);fflush (stdin);p = makenode (n);head = insertAtEnd(p, head);break;

case 5:delAtEnd(head);break;

case 6:printf ("Enter an integer: ");scanf ("%d", &n);printf ("Enter the posn: ");scanf ("%d", &m);fflush (stdin);p = makenode (n);head = insertAtPos(p, head,m);break;

case 7:printf ("Enter the posn: ");scanf ("%d", &n);

fflush (stdin);head = delAtPos(head,n);break;

case 8:exit(0);

}}while(c!=9);return 0;

}-----------------------------------------------------------------------------------------------------------------------------------10.queue*/#include <stdio.h>#include <string.h>#include <stdlib.h>struct SQueue{ int iData;

struct SQueue* psLink;};typedef struct SQueue theQueue;theQueue* theRear=NULL; /* theRear is a pointer at the rear end of the queue */theQueue* theFront=NULL; /* theFront is a pointer at the front end of the queue */void fnCreateQueue(FILE* fp) // fnCreateQueue will read the data from the file and put it in the queue{ int num,i;

theQueue *p;for(i=0;fscanf(fp,"%d",&num)!=EOF;i++){ p= (theQueue *) malloc (sizeof (theQueue));

p -> iData = num;p -> psLink = NULL;if(i==0){ theRear=p;

theFront=p;}else{ theRear->psLink=p;

theRear=theRear->psLink;}

}}void fnDeleteQueue(FILE* fp){ theQueue *p;

p = theFront;theFront = theFront->psLink;

p->psLink = NULL;}void fnDisplay(theQueue* psTraverse){ theQueue *p;

for (p = psTraverse; p != NULL; p = p -> psLink)printf ("%d ", p -> iData);

printf ("\n");}int main(int iArgc, char* pcArgv[]){ FILE* fpFile = fopen(pcArgv[1],"r");

fnCreateQueue(fpFile);printf("Created queue is\n");fnDisplay(theFront);fnDeleteQueue(fpFile);fnDisplay(theFront);return (0);

}---------------------------------------------------------------------------------------------------------------------------------11.doubly linked list*/

#include <stdio.h>#include <stdlib.h>

struct node {int info;struct node *next, *prev;

};

typedef struct node ND;

ND* makenode (int info) {ND* p = (ND*)malloc (sizeof (ND));p -> info = info;p -> next = p -> prev = NULL;return p;

}

void addathead (ND* p, ND** head, ND** tail) {if (*head == NULL) {

*head = *tail= p;return;

}

p -> next = *head;(*head) -> prev = p;

*head = p;}

void addattail (ND* p, ND** head, ND** tail) {if (*tail == NULL) {

*head = *tail= p;return;

}

p -> prev = *tail;(*tail) -> next = p;*tail = p;

}

void displayfromhead (ND* head) {ND* p;for (p = head; p != NULL; p = p -> next)

printf ("%d -> ", p -> info);printf ("NULL.\n");

}

void displayfromtail (ND* tail) {ND* p;for (p = tail; p != NULL; p = p -> prev)

printf ("%d -> ", p -> info);printf ("NULL.\n");

}

int main () {int c, n;

ND *head = NULL, *tail = NULL;ND *p;while (1) {

printf ("1. Insert at head.\n");printf ("2. Insert at tail.\n");printf ("3. Display from head.\n");printf ("4. Display from tail.\n");printf ("5. Exit.\n");printf ("Enter your option: ");scanf ("%d", &c);fflush (stdin);

switch (c) {case 1:

printf ("Enter an integer: ");scanf ("%d", &n);

fflush (stdin);p = makenode (n);addathead (p, &head, &tail);break;

case 2:printf ("Enter an integer: ");scanf ("%d", &n);fflush (stdin);p = makenode (n);addattail (p, &head, &tail);break;

case 3:displayfromhead (head);break;

case 4:displayfromtail (tail);break;

case 5:return 0;

}}return 0;

}-----------------------------------------------------------------------------------------------------------------12.doubly must

#include <stdio.h>#include<stdio.h>#include<stdlib.h>

struct SDll{ int iData;

struct SDll* psLeft;struct SDll* psRight;

};

typedef struct SDll theNode;theNode* theHead=NULL; /* theHead will point to the start of the doubly linked list */ theNode *theTail = NULL;

void fnCreateList(FILE *fp){ int num,i;

theNode *p;

for(i=0;fscanf(fp,"%d",&num)!=EOF;i++)

{ p= (theNode *) malloc (sizeof (theNode));p -> iData = num;p -> psLeft = NULL;p -> psRight = NULL;if(theHead==NULL){ theHead=p;

theTail=p;}else{ theTail->psRight=p;

p->psLeft=theTail;theTail=theTail->psRight;theTail->psRight=NULL;

}}

}void addLIst(int data, int posn){ int i=0;

theNode *p, *temp1, *temp2;temp1 = theHead;p= (theNode *) malloc (sizeof (theNode));p -> iData = data;p -> psLeft = NULL;p -> psRight = NULL;if(posn == 1){ theHead->psLeft = p;

p->psRight = theHead;theHead = theHead->psLeft;

}else{ for( i=2; i< posn; i++)

{ temp1 = temp1->psRight;}temp2 = temp1->psRight;temp1->psRight = p;p->psLeft = temp1;p->psRight = temp2;temp2->psLeft=p;

}temp1 = theHead;printf("\n");while(temp1!=NULL){ printf("%d ",theHead->iData);

theHead=theHead->psRight;}

}

void fnDisplay(theNode* theHead){ while(theHead!=NULL)

{ printf("%d ",theHead->iData);theHead=theHead->psRight;

}}

void fnSearchLIst(int data){ int i;

theNode *temp = theHead;

for(i=1;temp->psRight!=NULL && temp->iData!=data;i++,temp = temp->psRight)

;if(temp->iData==data)

printf("yes\n");else

printf("no\n");}void fnDeleteNode(int iPosition){ int i;

theNode *p = theHead;theNode *q = NULL;for(i=1;i<=iPosition;i++){ p=p->psRight;

if(i==iPosition-2)q=p;

}q->psRight=p;p->psLeft=q;

}

int main(int iArgc,char* pcArgv[]){ FILE* fpFile;

fpFile=fopen(pcArgv[1],"r");fnCreateList(fpFile);printf("Doubly linked list before deleting the node\n");fnDisplay(theHead);fnSearchLIst(atoi(pcArgv[2]));fnDeleteNode(atoi(pcArgv[3]));printf("\nDoubly linked list after deletion\n");fnDisplay(theHead);addLIst(atoi(pcArgv[4]),atoi(pcArgv[5]));

printf("\nDoubly linked list after insertion\n");//fnDisplay(theHead);return (0);

}-----------------------------------------------------------------------------------------------------------------------------------------------13.Binary tree*/

#include <stdio.h>#include <stdlib.h>

struct tnode {int info;struct tnode *l, *r;

};

typedef struct tnode T;

T *makenode (int n) {T *p = (T *) malloc (sizeof (T));p -> info = n;p -> r = p -> l = NULL;return p;

}

T *addnode (T *nd, T *root) {T *q, *p;if (root == NULL) {

root = nd;return root;

}for (q = NULL, p = root; p != NULL;) {

if (nd -> info >= p -> info) {q = p;p = p -> r;

}else {

q = p;p = p -> l;

}}if (nd -> info >= q -> info)

q -> r = nd;else

q -> l = nd;return root;

}

void intrav (T *root) {if (root != NULL) {

intrav (root -> l);printf ("%d ", root -> info);intrav (root -> r);

}}

void posttrav (T *root) {if (root != NULL) {

posttrav (root -> l);posttrav (root -> r);printf ("%d ", root -> info);

}}

void pretrav (T *root) {if (root != NULL) {

printf ("%d ", root -> info);pretrav (root -> l);pretrav (root -> r);

}}

int main () {int i;T *root = NULL;T *p = NULL;int a[] = {50, 60, 40, 45, 46, 47, 49, 43, 44, 42, 65, 61, 63, 55, 59};for (i = 0; i < 15; ++i) {

p = makenode (a [i]);root = addnode (p, root);

}intrav (root);printf ("\n");posttrav (root);printf ("\n");pretrav (root);printf ("\n");return 0;

}---------------------------------------------------------------------------------------------------------------------

14.single linked list swapping first and last

#include<stdio.h>#include<stdlib.h>struct Slist{

int iData;struct Slist* psNext;

};typedef struct Slist theNode;

theNode* theHead=NULL,*prev=NULL; void fnCreateList(FILE*);void fnDisplay(theNode*);void fnSwap(theNode*);

int main(int iArgc, char *pcArgv[]){

FILE* fp;fp=fopen(pcArgv[1],"r");fnCreateList(fp);printf("Created linked list is \n");fnDisplay(theHead);fnSwap(theHead);printf("Swapped linked list is \n");fnDisplay(theHead);return (0);

}

void fnCreateList(FILE* fp){

theNode *new1; int m;

while(!feof(fp)){

fscanf(fp,"%d",&m);new1=(theNode*)malloc(sizeof(theNode));

new1->iData=m; new1->psNext=NULL;

if(theHead==NULL){

theHead=new1;theHead->psNext=NULL;

}else{

prev=theHead;while(prev->psNext!=NULL){

prev=prev->psNext;}prev->psNext=new1;new1->psNext=NULL;

}

}}

void fnSwap(theNode* psStart){

theNode *d;int m,n; d=psStart;

theHead=d;

while(d->psNext!=NULL) {d=d->psNext;}m=d->iData;n=theHead->iData;theHead->iData=m;d->iData=n;

}

void fnDisplay(theNode* psTraverse){

if(theHead==NULL)printf("Data doesnot exists\n");

else{

for(prev=theHead;prev;prev=prev->psNext)printf("%d ",prev->iData);

}}---------------------------------------------------------------------------------------------------------/*Write a program to swap only the first and last node in the Doubly Linked List */#include<stdio.h> #include<stdlib.h> typedef struct SDll theNode;

struct SDll {int iData; struct SDll* psLeft; struct SDll* psRight; };/* theHead will point to the start of the doubly linked list */

theNode *theHead=NULL;

/*Function Prototype */void fnCreateList(FILE*); void fnDisplay(theNode*); void fnSwapNode(theNode*); int main(int iArgc,char* pcArgv[]) {FILE* fpFile;fpFile=fopen(pcArgv[1],"r"); fnCreateList(fpFile);printf("Doubly linked list before Swaping the node\n"); fnDisplay(theHead);fnSwapNode(theHead);printf("\nDoubly linked list after Swap\n"); fnDisplay(theHead);return (0); }/* fnDisplay will display the doubly linked listed from start to end */void fnDisplay(theNode* psTraverse) {while(psTraverse!=NULL) {printf("%d ",psTraverse->iData); psTraverse=psTraverse->psRight;}}/* fnCreateList will read the data from the file and create a doubly linked list */void fnCreateList(FILE* fp) {

theNode *ref,*temp;int m; while(!feof(fp)) {

ref=(theNode*)malloc(sizeof(theNode));

fscanf(fp,"%d",&m);

ref->iData=m;ref->psLeft=NULL;ref->psRight=NULL;if(theHead==NULL) {theHead=ref;}

else {

temp=theHead;

while(temp->psRight!=NULL) {temp=temp->psRight;}temp->psRight=ref;ref->psLeft=temp;

//ref=temp;}

}}/* fnSwapNode will swap only the first and last node in the doubly linked list */void fnSwapNode(theNode* psStart) {

theNode *d;int m,n; d=psStart;theHead=d;while(d->psRight!=NULL) {d=d->psRight;}m=d->iData;n=theHead->iData;

theHead->iData=m;d->iData=n;}

---------------------------------------------------------------------------------------

Binary Search Tree-----------------------

#include<stdio.h>#include<stdlib.h>struct SBst{

int iData;struct SBst* psLeft;struct SBst* psRight;

};

typedef struct SBst theNode;theNode* theRoot=NULL;void fnCreateTree(FILE*);void fnInorderTraversal(theNode*);void fnPreorderTraversal(theNode*);void fnPostorderTraversal(theNode*);int count(theNode*);

int main(int iArgc, char* pcArgv[]){

FILE *fpFilePointer; fpFilePointer=fopen(pcArgv[1],"r");fnCreateTree(fpFilePointer);printf("Inorder traversal of the Binary search tree is\n");

fnInorderTraversal(theRoot); printf("\n");fnPreorderTraversal(theRoot);printf("\n");fnPostorderTraversal(theRoot); printf("\n");int n=count(theRoot);printf("The total number of nodes:%d",n);return (0);

} theNode* btnew=NULL;theNode* btmp=NULL;theNode* prev;

--------------------------------------CREATE TREE---------------------------------------

void fnCreateTree(FILE* fpFilePointer){

int n=0;while(!feof(fpFilePointer)){

btnew=(theNode*)malloc(sizeof(theNode));fscanf(fpFilePointer,"%d",&n);btnew->psLeft=NULL;btnew->iData=n;btnew->psRight=NULL;

if(theRoot==NULL){

theRoot=btnew;}else{

btmp=theRoot;prev=NULL;while(btmp!=NULL){prev=btmp;if(n<btmp->iData)btmp=btmp->psLeft;else

btmp=btmp->psRight;}

if(n<prev->iData)prev->psLeft=btnew;

elseprev->psRight=btnew;

}

}

}

------------------------------------------------INORDER-----------------------------------------------void fnInorderTraversal(theNode* psNode)

{ if(psNode!=NULL){

fnInorderTraversal(psNode->psLeft);printf("%d ",psNode->iData);fnInorderTraversal(psNode->psRight);

}} ------------------------------------------------PREORDER----------------------------------------------- void fnPreorderTraversal(theNode* psNode){

if(psNode!=NULL){

printf("%d ",psNode->iData);fnPreorderTraversal(psNode->psLeft);fnPreorderTraversal(psNode->psRight);

}} --------------------------------------------------POSTOREDER--------------------------------------------------- void fnPostorderTraversal(theNode* psNode){

if(psNode!=NULL){

fnPostorderTraversal(psNode->psLeft);fnPostorderTraversal(psNode->psRight);printf("%d ",psNode->iData);

}}

------------------------------------------------------COUNT NUMBER OF NODES IN TREE----------------------------------------------------- int count(theNode* psNode){

unsigned int con=0;if(psNode!=NULL){

con=1+count(psNode->psLeft)+count(psNode->psRight);}return con;

}------------------------------------------------------------------------------------------------------

--------------------------------------------------------------DOUBLY LINKED LIST--------------------------------------------------------------

#include<stdio.h>#include<stdlib.h>

struct SDll{

int iData;struct SDll* psLeft;struct SDll* psRight;

};

typedef struct SDll theNode;/* theHead will point to the start of the doubly linked list */ theNode* theHead=NULL;

/*Function Prototype */ void fnCreateList(FILE*);void fnDisplay(theNode*);void insert_begin(int);void insert_end(int);void insert_middle(int,int);void delete_begin(theNode *);void delete_end();void delete_middle(theNode *,int);void search(theNode*,int);

int main(int iArgc,char* pcArgv[]){

FILE* fpFile;int dat,pos;

fpFile=fopen(pcArgv[1],"r");fnCreateList(fpFile);

printf("Created doubly linked list is\n");fnDisplay(theHead);

dat=atoi(pcArgv[2]);pos=atoi(pcArgv[3]);insert_begin(dat);insert_end(dat);insert_middle(dat,pos);

delete_begin(theHead);delete_end();delete_middle(theHead,pos);

search(theHead,dat);

printf("Result doubly linked list is\n");fnDisplay(theHead);

return (0);}

--------------------------------------CREATION--------------------------------------

void fnCreateList(FILE* fp)

{int m;

theNode* new1,*prev;/* Paste your code to create the doubly linked list here*/while(!feof(fp)){

fscanf(fp,"%d",&m);new1=(theNode*)malloc(sizeof(theNode));

new1->iData=m; new1->psLeft=NULL;

new1->psRight=NULL;if(theHead==NULL){

theHead=new1; theHead->psLeft=NULL;

new1->psRight=NULL;

}else

{prev=theHead;while(prev->psRight!=NULL)prev=prev->psRight;prev->psRight=new1;new1->psLeft=prev;new1->psRight=NULL;}

}}

------------------------------------DISPLAY------------------------------------

/* fnDisplay will display the doubly linked listed from start to end */ void fnDisplay(theNode* psTraverse){

theNode* prev;/*Paste your code to display the doubly linked list here*/if(theHead==NULL)

printf("Data doesnot exists\n");else{

for(prev=theHead;prev;prev=prev->psRight)printf("%d ",prev->iData);

}}

----------------------------------INSERT BEGIN----------------------------------

void insert_begin(int data){

theNode*p;p=(struct SDll*)malloc(sizeof(struct SDll*));if(theHead==NULL){

p->iData=data;p->psLeft=NULL;p->psRight=NULL;theHead=p;

}

else{

p->iData=data;p->psRight=theHead;p->psLeft=NULL;theHead->psLeft=p;theHead=p;

}}

-------------------------------------INSERT END--------------------------------------

void insert_end(int data){

theNode*p;while(theHead->psRight!=NULL)

theHead=theHead->psRight;p=(struct SDll*)malloc(sizeof(struct SDll*));p->iData=data;p->psRight=NULL;p->psLeft=theHead;theHead->psRight=p;while(theHead->psLeft!=NULL)

theHead=theHead->psLeft;}

--------------------------------------INSERT MIDDLE--------------------------------------

void insert_middle(int data,int position){

int count=1;theNode*p;while(position!=count){

theHead=theHead->psRight;count++;

}p=(struct SDll*)malloc(sizeof(struct SDll*));p->iData=data;p->psRight=theHead->psRight;p->psLeft=theHead;theHead->psRight=p;

p->psRight->psLeft=p;while(theHead->psLeft!=NULL)

theHead=theHead->psLeft;}

----------------------------------------DELETE BEGIN----------------------------------------

void delete_begin(theNode *psStart){

int n;theNode *temp = psStart;n=temp->iData;theHead=theHead->psRight;

free(temp);

}

---------------------------------------DELETE END----------------------------------------

void delete_end(){

int n;while(theHead->psRight!=NULL)

theHead=theHead->psRight;n=theHead->iData;theHead=theHead->psLeft;theHead->psRight->psLeft=NULL;theHead->psRight=NULL;while(theHead->psLeft!=NULL)

theHead=theHead->psLeft;

}

------------------------------------------DELETE MIDDLE------------------------------------------

void delete_middle(theNode *psStart,int position){

int n;

theNode*temp=psStart;int count=1;while(position!=count){

//theHead=temp;temp=temp->psRight;count++;

}temp->psLeft->psRight=temp->psRight;temp->psRight->psLeft=theHead;n=temp->iData;temp->psLeft=temp->psRight=NULL;while(theHead->psLeft!=NULL)

theHead=theHead->psRight;

}

-------------------------------------------SEARCHING A NODE--------------------------------------------

void search(theNode*psStart,int data){

theNode*temp=psStart;while(temp->psRight!=NULL && temp->iData!=data){

temp=temp->psRight;}if(temp->iData==data)

printf("\n Yes");else

printf("\n No");}----------------------------------------------------------------------------------------------------------------------------------------------

QUEUE USING LINKED LIST

#include<stdio.h>#include<stdlib.h>struct SQueue{

int iData;struct SQueue* psLink;

};

typedef struct SQueue theQueue;

/* theRear is a pointer at the rear end of the queue */theQueue* theRear=NULL;/* theFront is a pointer at the front end of the queue */theQueue* theFront=NULL;

/* Function Prototypes */ void fnCreateQueue(FILE*);void fnDisplay(theQueue*);void fnDeleteQueue(theQueue*);

int main(int iArgc, char* pcArgv[]){

FILE* fpFile;fpFile=fopen(pcArgv[1],"r");fnCreateQueue(fpFile);printf("Created queue is\n");fnDisplay(theFront);fnDeleteQueue(theFront);printf("Deleted queue is\n");fnDisplay(theFront);return (0);

}

-----------------------------CREATION-----------------------------

void fnCreateQueue(FILE* fp){

int m;theQueue* new1,*prev;/*Paste your code to create the queue here */

while(!feof(fp)){

fscanf(fp,"%d",&m);new1=(theQueue*)malloc(sizeof(theQueue));

new1->iData=m; new1->psLink=NULL;if(theRear==NULL && theFront==NULL){

theRear=new1;theFront=new1;

theRear->psLink=NULL;

}else{

prev=theRear;while(prev->psLink!=NULL){prev=prev->psLink;} prev->psLink=new1;theRear=new1;new1->psLink=NULL;}

}}

-------------------------------DISPLAY---------------------------------

/* fnDisplay will display the content of the queue from front to rear */ void fnDisplay(theQueue* psTraverse){

theQueue* prev;/*Paste your code to display the queue from front here */if(theFront==NULL)

printf("Data doesnot exists\n");else{

for(prev=theFront;prev;prev=prev->psLink)printf("%d ",prev->iData);

}}

-------------------------------DELETION--------------------------------

void fnDeleteQueue(theQueue*psTraverse){

int n;n=psTraverse->iData;theFront=theFront->psLink;if(theFront==NULL)

theRear=NULL;psTraverse->psLink=NULL;

}------------------------------------------------------------------------------------------------------------------------------------SINGLY LINKED LIST

#include<stdio.h>#include<stdlib.h>struct Slist{

int iData;struct Slist* psNext;

};typedef struct Slist theNode;

theNode* theHead=NULL,*prev=NULL; void fnCreateList(FILE*);void fnDisplay(theNode*);void insert_begin(int);void insert_end(theNode*,int);void insert_middle(theNode*,int,int);void delete_begin(theNode*);void delete_end(theNode*);void delete_middle(theNode*,int);void search(theNode*,int);int main(int iArgc, char *pcArgv[]){

FILE* fp;int dat,pos;fp=fopen(pcArgv[1],"r");

dat=atoi(pcArgv[2]);pos=atoi(pcArgv[3]);

fnCreateList(fp);printf("Created linked list is \n");fnDisplay(theHead);

insert_begin(dat);insert_end(theHead,dat);insert_middle(theHead,dat,pos);delete_begin(theHead);delete_end(theHead);delete_middle(theHead,pos);

search(theHead,dat);

printf("Result linked list is \n");fnDisplay(theHead);

return (0);}

----------------------------CREATION----------------------------

void fnCreateList(FILE* fp){

theNode *new1; int m;

while(!feof(fp)){

fscanf(fp,"%d",&m);new1=(theNode*)malloc(sizeof(theNode));

new1->iData=m; new1->psNext=NULL;

if(theHead==NULL){

theHead=new1;theHead->psNext=NULL;

}else{

prev=theHead;while(prev->psNext!=NULL){

prev=prev->psNext;}prev->psNext=new1;new1->psNext=NULL;

}

}}

-------------------DISPLAY-------------------

void fnDisplay(theNode* psTraverse){

if(theHead==NULL)printf("Data doesnot exists\n");

else{

for(prev=theHead;prev;prev=prev->psNext)printf("%d ",prev->iData);

}}

----------------------INSERT BEGIN----------------------

void insert_begin(int data){

theNode*p;p=(theNode*)malloc(sizeof(theNode*));p->iData=data;p->psNext=theHead;theHead=p;

}

-------------------------INSERT END--------------------------

void insert_end(theNode*psStart,int data){

theNode*p;theNode*temp=psStart;p=(theNode*)malloc(sizeof(theNode*));p->iData=data;p->psNext=NULL;while(temp->psNext!=NULL)

temp=temp->psNext;temp->psNext=p;

}

-------------------------------INSERT MIDDLE

-------------------------------

void insert_middle(theNode*psStart,int data,int position){

theNode*temp=psStart;theNode*p;int count=1;while(position!=count){

temp=temp->psNext;count++;

}p=(theNode*)malloc(sizeof(theNode*));p->iData=data;p->psNext=temp->psNext;temp->psNext=p;

}

--------------------------------DELETE BEGIN-------------------------------

void delete_begin(theNode*psStart){

int n;theNode*temp=psStart;n=theHead->iData;theHead=theHead->psNext;temp->psNext=NULL;

}

----------------------------------DELETE END-----------------------------------

void delete_end(theNode*psStart){

int n;theNode*temp=psStart;theNode*prev=NULL;while(temp->psNext!=NULL){

prev=temp;temp=temp->psNext;

}n=temp->iData;

prev->psNext=NULL;

}

-------------------------------------DELETE MIDDLE------------------------------------

void delete_middle(theNode*psStart,int position){

int count=1;theNode*temp=psStart;theNode*prev=NULL;while((temp!=NULL) && (count!=position)){

prev=temp;temp=temp->psNext;count++;

}prev->psNext=temp->psNext;temp->psNext=NULL;

}

-----------------------------SEARCH---------------------------------

void search(theNode*psStart,int data){

theNode*temp=psStart;while(temp->psNext!=NULL && temp->iData!=data){

temp=temp->psNext;}if(temp->iData==data)

printf("\nYes");else

printf("\nNo");}---------------------------------------------------------------------------------------------------------

-------------------------------------------------STACK USING LINKED LIST--------------------------------------------------

#include<stdio.h>#include<stdlib.h>

struct SStack{

int iStackData;struct SStack* psLink;

};typedef struct SStack theStack;

/*Top will point to the Top position of the Stack */theStack* theTop=NULL;

/* Function Prototypes */ void fnCreateStack(FILE*);void fnDisplayStack(theStack*);void fnDeleteStack(theStack*);

/* Main function */ int main(int iArgc, char *pcArgv[]){

/*fpFile will hold the pointer to the file where the contents to be put on the stack are saved */

FILE* fpFile;fpFile=fopen(pcArgv[1],"r");fnCreateStack(fpFile);printf("Stack contents are\n");fnDisplayStack(theTop);fnDeleteStack(theTop);printf("Stack contents after deletion\n");fnDisplayStack(theTop);return (0);

}

-----------------------------CREATION-----------------------------

void fnCreateStack(FILE* fp){theStack* new1; int m;

while(!feof(fp)){

fscanf(fp,"%d",&m);

new1=(theStack*)malloc(sizeof(theStack));new1->iStackData=m;new1->psLink=NULL;if(theTop==NULL)

theTop=new1;

else{

new1->psLink=theTop;theTop=new1;

}

}

}

---------DISPLAY---------

/* fnDisplayStack will display the contents of the stack from top to bottom */ void fnDisplayStack(theStack* Traverse){theStack* ptr;

/*Paste your code to display the stack here */if(theTop==NULL)

printf("Stack is empty\n");else{for(ptr=theTop;ptr;ptr=ptr->psLink)

printf("%d ",ptr->iStackData);

}

}

-----------DELETION-----------

void fnDeleteStack(theStack*Traverse){

int n;

//Traverse=theTop;n=theTop->iStackData;theTop=theTop->psLink;Traverse->psLink=NULL;

}------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------C Prog With O/P----------------------------------------------------------------------------------------------

1.Write a C program to convert the given binary number into its equivalent decimal */

#include <stdio.h>

void main(){ int num, bnum, dec = 0, base = 1, rem ;

printf("Enter the binary number(1s and 0s)\n"); scanf("%d", &num); /*Enter maximum five digits or use long int*/

bnum = num;

while( num > 0) { rem = num % 10;

dec = dec + rem * base; num = num / 10 ;

base = base * 2; }

printf("The Binary number is = %d\n", bnum); printf("Its decimal equivalent is =%d\n", dec);

} /* End of main() */

OutpputEnter the binary number(1s and 0s)1010The Binary number is = 1010Its decimal equivalent is =10

---------------------------------------------------------------------------------------------------2.Write a c program to multifly given number by 4 using bitwise operators

#include <stdio.h>

void main() {

long number, tempnum;

printf("Enter an integer\n");scanf("%ld",&number);tempnum = number;number = number << 2; /*left shift by two bits*/

printf("%ld x 4 = %ld\n", tempnum,number);}

OutputEnter an integer1515 x 4 = 60

RUN2Enter an integer262262 x 4 = 1048---------------------------------------------------------------------------------------------------

3.Write a C program to find the sum of cos(x) series

#include<stdio.h>#include<math.h>

void main(){ int n,x1,i,j; float x,sign,cosx,fact;

printf("Enter the number of the terms in aseries\n"); scanf("%d",&n);

printf("Enter the value of x(in degrees)\n");scanf("%f",&x);

x1=x;

x=x*(3.142/180.0); /* Degrees to radians*/

cosx=1;

sign=-1;

for(i=2;i<=n;i=i+2) {

fact=1;

for(j=1;j<=i;j++) {

fact=fact*j; }

cosx=cosx+(pow(x,i)/fact)*sign; sign=sign*(-1);

}

printf("Sum of the cosine series = %7.2f\n",cosx); printf("The value of cos(%d) using library function = %f\n",x1,cos(x));

} /*End of main() */

OutputEnter the number of the terms in aseries5Enter the value of x(in degrees)60Sum of the cosine series = 0.50The value of cos(60) using library function = 0.499882------------------------------------------------------------------------------------------------------------------4.Writ a C programme to cyclically permute the elements of an array A

i.e. the content of A1 become that of A2.And A2 contains that of A3 & so on as An contains A1

#include <stdio.h>

void main (){

int i,n,number[30];printf("Enter the value of the n = ");

scanf ("%d", &n);printf ("Enter the numbers\n");for (i=0; i<n; ++i){

scanf ("%d", &number[i]);}number[n] = number[0];

for (i=0; i<n; ++i){

number[i] = number[i+1];}printf ("Cyclically permted numbers are given below \n");for (i=0; i<n; ++i)printf ("%d\n", number[i]);

}

OutputEnter the value of the n = 5Enter the numbers1030204518Cyclically permted numbers are given below3020451810---------------------------------------------------------------------------------------------------------------------------5.Write a C program to accept a set of numbers and compute mean,varience and standard deviation

#include <stdio.h>#include <math.h>

void main(){ float x[10];

int i, n; float avrg, var, SD, sum=0, sum1=0;

printf("Enter howmany elements\n");

scanf("%d", &n);

for(i=0; i<n; i++) {

scanf("%f", &x[i]); }

/* Compute the sum of all elements */ for(i=0; i<n; i++)

{ sum = sum + x[i];

} avrg = sum /(float) n;

/* Compute varaience and standard deviation */for(i=0; i<n; i++)

{ sum1 = sum1 + pow((x[i] - avrg),2);

}

var = sum1 / (float) n;SD = sqrt(var);

printf("Avrage of all elements =%.2f\n", avrg); printf("Varience of all elements =%.2f\n", avrg);

printf("Standard deviation of all elements =%.2f\n", SD);

}

OutputEnter howmany elements51021325917Avrage of all elements =27.80Varience of all elements =27.80Standard deviation of all elements =17.15-----------------------------------------------------------------------------------------------------------------------6. Write a C program to accept N numbers and arrange them in an asceding order

#include <stdio.h>

void main (){ int i,j,a,n,number[30];

printf ("Enter the value of N\n"); scanf ("%d", &n);

printf ("Enter the numbers \n"); for (i=0; i<n; ++i) scanf ("%d",&number[i]);

for (i=0; i<n; ++i) { for (j=i+1; j<n; ++j)

{if (number[i] > number[j]){

a= number[i];number[i] = number[j];number[j] = a;

} }

}

printf ("The numbers arrenged in ascending order are given below\n"); for (i=0; i<n; ++i) printf ("%d\n",number[i]);} /* End of main() */

OutputEnter the value of N5Enter the numbers8020671045The numbers arrenged in ascending order are given below1020456780

---------------------------------------------------------------------------------------------------------------------------------7.Write a C program to accept a set of numbers and arrange them in a descending order

#include <stdio.h>

void main (){ int number[30]; int i,j,a,n;

printf ("Enter the value of N\n"); scanf ("%d", &n);

printf ("Enter the numbers \n"); for (i=0; i<n; ++i) scanf ("%d",&number[i]);

/* sorting begins ...*/ for (i=0; i<n; ++i) { for (j=i+1; j<n; ++j)

{if (number[i] < number[j]){

a = number[i];number[i] = number[j];number[j] = a;

} }

}

printf ("The numbers arranged in descending order are given below\n"); for (i=0; i<n; ++i) {

printf ("%d\n",number[i]); }

} /* End of main() */

Output Enter the value of N6Enter the numbers10

356710042688The numbers arranged in descending order are given below68810067423510

----------------------------------------------------------------------------------------------------------------------------------------8.Write a C program to accept a list of data items and find the second largest and second smallest elements in it. And also computer the average of both. And search for the average value whether it is present in the array or not. Display appropriate message on successful search.

#include <stdio.h>

void main (){ int number[30]; int i,j,a,n,counter,ave;

printf ("Enter the value of N\n"); scanf ("%d", &n);

printf ("Enter the numbers \n"); for (i=0; i<n; ++i) scanf ("%d",&number[i]);

for (i=0; i<n; ++i) {

for (j=i+1; j<n; ++j){

if (number[i] < number[j]){

a = number[i];number[i] = number[j];number[j] = a;

}}

}

printf ("The numbers arranged in descending order are given below\n"); for (i=0; i<n; ++i) {

printf ("%d\n",number[i]); }

printf ("The 2nd largest number is = %d\n", number[1]); printf ("The 2nd smallest number is = %d\n", number[n-2]);

ave = (number[1] +number[n-2])/2; counter = 0;

for (i=0; i<n; ++i) {

if (ave == number[i]) {

++counter; }

} if (counter == 0 )

printf ("The average of %d and %d is = %d is not in the array\n", number[1], number[n-2], ave); else

printf ("The average of %d and %d in array is %d in numbers\n",number[1], number[n-2], counter);} /* End of main() */

OutputEnter the value of N6Enter the numbers308010407090The numbers arranged in descending order are given below908070403010

The 2nd largest number is = 80The 2nd smallest number is = 30The average of 80 and 30 is = 55 is not in the array----------------------------------------------------------------------------------------------------------------------------9.Write a C program to accept an array of integers and delete the specified integer from the list

#include <stdio.h>

void main(){ int vectx[10];

int i, n, pos, element, found = 0;

printf("Enter how many elements\n"); scanf("%d", &n);

printf("Enter the elements\n"); for(i=0; i<n; i++)

{ scanf("%d", &vectx[i]);

}

printf("Input array elements are\n");for(i=0; i<n; i++){

printf("%d\n", vectx[i]);}

printf("Enter the element to be deleted\n"); scanf("%d",&element);

for(i=0; i<n; i++) {

if ( vectx[i] == element) {

found = 1;pos = i;break;

}}

if (found == 1){

for(i=pos; i< n-1; i++)

{vectx[i] = vectx[i+1];

}

printf("The resultant vector is \n"); for(i=0; i<n-1; i++)

{printf("%d\n",vectx[i]);

} } else

printf("Element %d is not found in the vector\n", element);

} /* End of main() */

Output

Run 1Enter how many elements5Enter the elements3010502040Input array elements are3010502040Enter the element to be deleted35Element 35 is not found in the vector

Run 2Enter how many elements4Enter the elements23105581Input array elements are

23105581Enter the element to be deleted55The resultant vector is231081

----------------------------------------------------------------------------------------------------------------------------------10.Write a C programme (1-D Array) store some elements in it.Accept key & split from that point. Add the first half to the end of second half

#include <stdio.h>

void main (){ int number[30]; int i,n,a,j;

printf ("Enter the value of n\n"); scanf ("%d",&n);

printf ("enter the numbers\n"); for (i=0; i<n; ++i) scanf ("%d", &number[i]);

printf ("Enter the position of the element to split the array \n"); scanf ("%d",&a);

for (i=0; i<a; ++i) {

number[n] = number[0];

for (j=0; j<n; ++j) {

number[j] = number[j+1]; }

}

printf("The resultant array is\n"); for (i=0; i<n; ++i) {

printf ("%d\n",number[i]); }} /* End of main() */

OutputEnter the value of n5enter the numbers3010405060Enter the position of the element to split the array2The resultant array is4050603010------------------------------------------------------------------------------------------------------------------------------11.Write a C program to find the frequency of odd numbers and even numbers in the input of a matrix

#include <stdio.h>

void main (){ static int ma[10][10]; int i,j,m,n,even=0,odd=0;

printf ("Enter the order ofthe matrix \n"); scanf ("%d %d",&m,&n);

printf ("Enter the coefficients if matrix \n"); for (i=0;i<m;++i) {

for (j=0;j<n;++j){

scanf ("%d", &ma[i][j]);if ((ma[i][j]%2) == 0){

++even;

}else

++odd;}

} printf ("The given matrix is\n"); for (i=0;i<m;++i) {

for (j=0;j<n;++j){

printf (" %d",ma[i][j]);}printf ("\n");

}

printf ("\nThe frequency of occurance of odd number = %d\n",odd);printf ("The frequency of occurance of even number = %d\n",even);

} /* End of main() */

OutputEnter the order ofthe matrix3 3Enter the coefficients if matrix1 2 34 5 67 8 9The given matrix is 1 2 3 4 5 6 7 8 9

The frequency of occurance of odd number = 5The frequency of occurance of even number = 4

-----------------------------------------------------------------------------------------------------------------------------12.Write a C program to accept a matrix of order M x N and store its elements and interchange the main diagonal elements of the matrix with that of the secondary diagonal elements

#include <stdio.h>

void main (){

static int ma[10][10];int i,j,m,n,a;

printf ("Enetr the order of the matix \n");scanf ("%d %d",&m,&n);

if (m ==n ){

printf ("Enter the co-efficients of the matrix\n");for (i=0;i<m;++i){

for (j=0;j<n;++j){

scanf ("%dx%d",&ma[i][j]);}

}

printf ("The given matrix is \n");for (i=0;i<m;++i){

for (j=0;j<n;++j){

printf (" %d",ma[i][j]);}printf ("\n");

}

for (i=0;i<m;++i){

a = ma[i][i];ma[i][i] = ma[i][m-i-1];ma[i][m-i-1] = a;

}

printf ("THe matrix after changing the \n");printf ("main diagonal & secondary diagonal\n");for (i=0;i<m;++i){

for (j=0;j<n;++j){

printf (" %d",ma[i][j]);}printf ("\n");

}}

elseprintf ("The givan order is not square matrix\n");

} /* end of main() */

OutputEnetr the order of the matix3 3Enter the co-efficients of the matrix1 2 34 5 67 8 9The given matrix is 1 2 3 4 5 6 7 8 9THe matrix after changing themain diagonal & secondary diagonal 3 2 1 4 5 6 9 8 7--------------------------------------------------------------------------------------------------------------------------------------13.Write a C program to find the sum of first 50 natural numbers using for loop

#include <stdio.h>

void main(){

int num,sum=0;

for(num = 1; num <= 50; num++){

sum = sum + num;}

printf("Sum = %4d\n", sum);}

OutputSum = 1275---------------------------------------------------------------------------------------------------------------------------------------14. Write a C program to accept two integers and check if they are equal

#include <stdio.h>void main(){

int m,n;

printf("Enter the values for M and N\n");scanf("%d %d", &m,&n);

if(m == n )printf("M and N are equal\n");

elseprintf("M and N are not equal\n");

}

outputEnter the values for M and N34 45M and N are not equal

--------------------------------------------------------------------------------------------------------------------------------------15.Write a C program to accept the height of a person in centermeter and categorize the person based on height as taller, dwarf and average height person

#include <stdio.h>void main(){

float ht;

printf("Enter the Height (in centimetres)\n");scanf("%f",&ht);

if(ht < 150.0) printf("Dwarf\n");else if((ht>=150.0) && (ht<=165.0))

printf(" Average Height\n");else if((ht>=165.0) && (ht <= 195.0)) printf("Taller\n");else printf("Abnormal height\n");

} /* End of main() */

OutputEnter the Height (in centimetres)45Dwarf------------------------------------------------------------------------------------------------------------------------------------16.Write a C program to accept a coordinate point in a XY coordinate system and determine its quadrant

*/

#include <stdio.h>

void main(){

int x,y;

printf("Enter the values for X and Y\n");scanf("%d %d",&x,&y);

if( x > 0 && y > 0) printf("point (%d,%d) lies in the First quandrant\n");else if( x < 0 && y > 0) printf("point (%d,%d) lies in the Second quandrant\n");else if( x < 0 && y < 0) printf("point (%d, %d) lies in the Third quandrant\n");else if( x > 0 && y < 0) printf("point (%d,%d) lies in the Fourth quandrant\n");else if( x == 0 && y == 0) printf("point (%d,%d) lies at the origin\n");

} /* End of main() */

OutputRUN 1Enter the values for X and Y3 5point (5,3) lies in the First quandrant

RUN 2Enter the values for X and Y-4-7

point (-7, -4) lies in the Third quandrant

------------------------------------------------------------------------------------------------------------------------------------------17.Write a C program to accept a figure code and find the ares of different geometrical figures such as circle, square, rectangle etc using switch

#include <stdio.h>

void main(){

int fig_code;float side,base,length,bredth,height,area,radius;

printf("-------------------------\n");printf(" 1 --> Circle\n");printf(" 2 --> Rectangle\n");printf(" 3 --> Triangle\n");printf(" 4 --> Square\n");printf("-------------------------\n");

printf("Enter the Figure code\n");scanf("%d",&fig_code);

switch(fig_code){

case 1: printf("Enter the radius\n");scanf("%f",&radius);area=3.142*radius*radius;printf("Area of a circle=%f\n", area);break;

case 2: printf("Enter the bredth and length\n");scanf("%f %f",&bredth, &length);area=bredth *length;printf("Area of a Reactangle=%f\n", area);break;

case 3: printf("Enter the base and height\n");scanf("%f %f",&base,&height);area=0.5 *base*height;printf("Area of a Triangle=%f\n", area);break;

case 4: printf("Enter the side\n");

scanf("%f",&side);area=side * side;printf("Area of a Square=%f\n", area);break;

default: printf("Error in figure code\n");break;

} /* End of switch */

}

OutputRun 1 1 --> Circle 2 --> Rectangle 3 --> Triangle 4 --> SquareEnter the Figure code2Enter the bredth and length26Area of a Reactangle=12.000000

Run 2

1 --> Circle 2 --> Rectangle 3 --> Triangle 4 --> Square

Enter the Figure code3 Enter the base and height57Area of a Triangle=17.500000

------------------------------------------------------------------------------------------------------------------------------------18. Write a C program to accept a string and find the sum of all digits present in the string

#include <stdio.h>void main()

{char string[80];int count,nc=0,sum=0;

printf("Enter the string containing both digits and alphabet\n");scanf("%s",string);

for(count=0;string[count]!='\0'; count++){

if((string[count]>='0') && (string[count]<='9')){

nc += 1;sum += (string[count] - '0');

} /* End of if */} /* End of For */

printf("NO. of Digits in the string=%d\n",nc);printf("Sum of all digits=%d\n",sum);

} /* End of main() */

OutputEnter the string containing both digits and alphabetgoodmorning25NO. of Digits in the string=2Sum of all digits=7

------------------------------------------------------------------------------------------------------------------------------------19.Write a C program to accept an integer find the sum of the digits in it

#include <stdio.h>

void main(){ long num, temp, digit, sum = 0;

printf("Enter the number\n"); scanf("%ld", &num);

temp = num;

while(num > 0) {

digit = num % 10;

sum = sum + digit; num /= 10; }

printf("Given number =%ld\n", temp); printf("Sum of the digits %ld =%ld\n", temp, sum);} /* End of main()*/

Enter the number123456Given number =123456Sum of the digits 123456 =21-----------------------------------------------------------------------------------------------------------------------------------20.Write a C program to accept a matrix of order M x N and find the sum of each row and each column of a matrix #include <stdio.h>

void main (){

static int m1[10][10];int i,j,m,n,sum=0;

printf ("Enter the order of the matrix\n");scanf ("%d %d", &m,&n);printf ("Enter the co-efficients of the matrix\n");

for (i=0;i<m;++i){

for (j=0;j<n;++j){

scanf ("%d",&m1[i][j]);}

}for (i=0;i<m;++i){

for (j=0;j<n;++j){

sum = sum + m1[i][j] ;}printf ("Sum of the %d row is = %d\n",i,sum);sum = 0;

}

sum=0;

for (j=0;j<n;++j){

for (i=0;i<m;++i){

sum = sum+m1[i][j];}printf ("Sum of the %d column is = %d\n", j,sum);sum = 0;

}}

OutputEnter the order of the matrix3 3Enter the co-efficients of the matrix1 2 34 5 67 8 9Sum of the 0 row is = 6Sum of the 1 row is = 15Sum of the 2 row is = 24Sum of the 0 column is = 12Sum of the 1 column is = 15Sum of the 2 column is = 18

--------------------------------------------------------------------------------------------------------------------------------------------21.Write a C program to find accept a matric of order M x N and find the sum of the main diagonal and off diagonal elements

#include <stdio.h>

void main (){

static int ma[10][10];int i,j,m,n,a=0,sum=0;

printf ("Enetr the order of the matix \n");scanf ("%d %d",&m,&n);

if ( m == n ){

printf ("Enter the co-efficients of the matrix\n");

for (i=0;i<m;++i){

for (j=0;j<n;++j){

scanf ("%d",&ma[i][j]);}

}

printf ("The given matrix is \n");for (i=0;i<m;++i){

for (j=0;j<n;++j){

printf (" %d",ma[i][j]);}printf ("\n");

}

for (i=0;i<m;++i){

sum = sum + ma[i][i];a = a + ma[i][m-i-1];

}

printf ("\nThe sum of the main diagonal elements is = %d\n",sum);printf ("The sum of the off diagonal elemets is = %d\n",a);

}else

printf ("The givan order is not square matrix\n");} /* End of main() */

OutputEnetr the order of the matix3 3Enter the co-efficients of the matrix1 2 34 5 67 8 9The given matrix is 1 2 3 4 5 6 7 8 9

The sum of the main diagonal elements is = 15The sum of the off diagonal elemets is = 15

Run 2Enetr the order of the matix2 3The givan order is not square matrix

--------------------------------------------------------------------------------------------------------------------------------------22. Write a C program to accept a matricx of order MxN and find the trcae and normal of a matrix HINT:Trace is defined as the sum of main diagonal elements and Normal is defined as squre root of the sum of all the elements

#include <stdio.h>#include <math.h>

void main (){

static int ma[10][10];int i,j,m,n,sum=0,sum1=0,a=0,normal;

printf ("Enter the order of the matrix\n");scanf ("%d %d", &m,&n);

printf ("Enter the ncoefficients of the matrix \n");for (i=0;i<m;++i){

for (j=0;j<n;++j){

scanf ("%d",&ma[i][j]);a = ma[i][j]*ma[i][j];sum1 = sum1+a;

}}normal = sqrt(sum1);printf ("The normal of the given matrix is = %d\n",normal);for (i=0;i<m;++i){

sum = sum + ma[i][i];}printf ("Trace of the matrix is = %d\n",sum);

} /*End of main() */

OutputEnter the order of the matrix3 3

Enter the ncoefficients of the matrix1 2 34 5 67 8 9The normal of the given matrix is = 16Trace of the matrix is = 15

Run 2Enter the order of the matrix2 2Enter the ncoefficients of the matrix2 46 8The normal of the given matrix is = 10Trace of the matrix is = 10

----------------------------------------------------------------------------------------------------------------------------

23.Write a C program to accept a matric and determine whether it is a sparse matrix. A sparse martix is matrix which has more zero elements than nonzero elements

#include <stdio.h>

void main (){

static int m1[10][10];int i,j,m,n;int counter=0;

printf ("Enter the order of the matix\n");scanf ("%d %d",&m,&n);

printf ("Enter the co-efficients of the matix\n");for (i=0;i<m;++i){

for (j=0;j<n;++j){

scanf ("%d",&m1[i][j]);if (m1[i][j]==0){

++counter;}

}}if (counter>((m*n)/2))

{printf ("The given matrix is sparse matrix \n");

}else

printf ("The given matrix is not a sparse matrix \n");

printf ("There are %d number of zeros",counter);

} /* EN dof main() */

OutputEnter the order of the matix2 2Enter the co-efficients of the matix1 23 4The given matrix is not a sparse matrixThere are 0 number of zeros

Run 2Enter the order of the matix3 3Enter the co-efficients of the matix1 0 00 0 10 1 0The given matrix is sparse matrixThere are 6 number of zeros

------------------------------------------------------------------------------------------------------------------------------------------24.Write a C program to accept a amtric of order MxN and sort all rows of the matrix in ascending order and all columns in descendng order

#include <stdio.h>

void main (){

static int ma[10][10],mb[10][10];int i,j,k,a,m,n;

printf ("Enter the order of the matrix \n");scanf ("%d %d", &m,&n);

printf ("Enter co-efficients of the matrix \n");for (i=0;i<m;++i)

{for (j=0;j<n;++j){

scanf ("%d",&ma[i][j]);mb[i][j] = ma[i][j];

}}printf ("The given matrix is \n");for (i=0;i<m;++i){

for (j=0;j<n;++j){

printf (" %d",ma[i][j]);}printf ("\n");

}

printf ("After arranging rows in ascending order\n");for (i=0;i<m;++i){

for (j=0;j<n;++j){

for (k=(j+1);k<n;++k){

if (ma[i][j] > ma[i][k]){

a = ma[i][j];ma[i][j] = ma[i][k];ma[i][k] = a;

}}

}} /* End of outer for loop*/

for (i=0;i<m;++i){

for (j=0;j<n;++j){

printf (" %d",ma[i][j]);}printf ("\n");

}

printf ("After arranging the columns in descending order \n");for (j=0;j<n;++j){

for (i=0;i<m;++i){

for (k=i+1;k<m;++k){

if (mb[i][j] < mb[k][j]){

a = mb[i][j];mb[i][j] = mb[k][j];mb[k][j] = a;

}}

}} /* End of outer for loop*/

for (i=0;i<m;++i){

for (j=0;j<n;++j){

printf (" %d",mb[i][j]);}printf ("\n");

}

}

Enter the order of the matrix2 2Enter co-efficients of the matrix3 15 2The given matrix is 3 1 5 2After arranging rows in ascending order 1 3 2 5After arranging the columns in descending order 5 2 3 1----------------------------------------------------------------------------------------------------------------------------------------------------25.Write a C program to accept a set of names and sort them in an alphabetical order, Use structures to store the names

#include<stdio.h>

#include<conio.h>#include<string.h>

struct person{

char name[10];int rno;

};typedef struct person NAME;NAME stud[10], temp[10];

void main(){

int no,i;

void sort(int N); /* Function declaration */

clrscr(); fflush(stdin);

printf("Enter the number of students in the list\n"); scanf("%d",&no);

for(i = 0; i < no; i++) {

printf("\nEnter the name of person %d : ", i+1); fflush(stdin); gets(stud[i].name);

printf("Enter the roll number of %d : ", i+1); scanf("%d",&stud[i].rno); temp[i] = stud[i];

}

printf("\n*****************************\n"); printf (" Names before sorting \n"); /* Print the list of names before sorting */ for(i=0;i<no;i++) {

printf("%-10s\t%3d\n",temp[i].name,temp[i].rno); }

sort(no); /* Function call */

printf("\n*****************************\n"); printf (" Names after sorting \n");

printf("\n*****************************\n"); /* Display the sorted names */ for(i=0;i<no;i++) {

printf("%-10s\t%3d\n",stud[i].name,stud[i].rno);

} printf("\n*****************************\n");

} /* End of main() */

/* Function to sort the given names */void sort(int N){

int i,j; NAME temp;

for(i = 0; i < N-1;i++) {

for(j = i+1; j < N; j++){

if(strcmp(stud[i].name,stud[j].name) > 0 ){

temp = stud[i];stud[i] = stud[j];stud[j] = temp;

}}

}} /* end of sort() */

Enter the number of students in the list5

Enter the name of person 1 : RajashreeEnter the roll number of 1 : 123

Enter the name of person 2 : JohnEnter the roll number of 2 : 222

Enter the name of person 3 : PriyaEnter the roll number of 3 : 331

Enter the name of person 4 : AnandEnter the roll number of 4 : 411

Enter the name of person 5 : NirmalaEnter the roll number of 5 : 311

***************************** Names before sortingRajashree 123John 222Priya 331Anand 411Nirmala 311

***************************** Names after sorting

*****************************Anand 411John 222Nirmala 311Priya 331Rajashree 123

*****************************---------------------------------------------------------------------------------------------------------------------------------------26.Write a C Program to convert the lower case letters to upper case and vice-versa

#include <stdio.h>#include <ctype.h>#include <conio.h>

void main(){ char sentence[100]; int count, ch, i; clrscr();

printf("Enter a sentence\n"); for(i=0; (sentence[i] = getchar())!='\n'; i++) { ; } count = i;

printf("\nInput sentence is : %s ",sentence);

printf("\nResultant sentence is : ");

for(i=0; i < count; i++) { ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]);

putchar(ch); }

} /*End of main()

OutputEnter a sentencegOOD mORNING

Input sentence is : gOOD mORNING

Resultant sentence is :Good Morning-----------------------------------------------------------------------------------------------------------------------------27.This program accepts an array of N elements and a key.Then it searches for the desired element. If the search is successful, it displays "SUCCESSFUL SEARCH". * Otherwise, a message "UNSUCCESSFUL SEARCH" is displayed.

#include <stdio.h>void main(){

int table[20];int i, low, mid, high, key, size;

printf("Enter the size of an array\n");scanf("%d", &size);

printf("Enter the array elements\n");for(i = 0; i < size; i++){

scanf("%d", &table[i]);}

printf("Enter the key\n");scanf("%d", &key);

/* search begins */

low = 0;high = (size - 1);

while(low <= high){

mid = (low + high)/2;if(key == table[mid]){

printf("SUCCESSFUL SEARCH\n");return;

}if(key < table[mid])

high = mid - 1;else

low = mid + 1;}

printf("UNSUCCESSFUL SEARCH\n");} /* End of main() */

OutputOutputEnter the size of an array5Enter the array elements1236457899Enter the key45SUCCESSFUL SEARCH-----------------------------------------------------------------------------------------------------------------------------------------------------------------

top related