Академический Документы
Профессиональный Документы
Культура Документы
//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.
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.
9.Write a program that prints out the sum of the integers in the 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);
}
----------------------------------------------------------------------------------------------
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.
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
#include<stdio.h>
#include<conio.h>
#include<string.h>
}
---------------------------------------------------------------------------------------------------------
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");
}
-----------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
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>
return 0;
}
------------------------------------------------------------------------------------------------------------
-----------
}
++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");
}
#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;
}
------------------------------------------------------------------------------------------------------
#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>
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*);
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;
}
}
}
------------------------------------------------------------------------------------------------------------
-------------
2.Binary Search Tree
#include<stdio.h>
#include<stdlib.h>
struct SBst
{
int iData;
struct SBst* psLeft;
struct SBst* psRight;
};
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;
else
prev->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;
};
/*Function Prototype */
void fnCreateList(FILE*);
void fnDisplay(theNode*);
{
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;
/* 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);
}
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;
/* Function Prototypes */
void fnCreateQueue(FILE*);
void fnDisplay(theQueue*);
}
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);
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);
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;
}
------------------------------------------------------------------------------------------------------------
-----
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int info;
struct node *next;
};
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;
};
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;
};
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;
}
}
#include <stdio.h>
#include <stdlib.h>
struct tnode {
int info;
struct tnode *l, *r;
};
T *makenode (int n) {
T *p = (T *) malloc (sizeof (T));
p -> info = n;
p -> r = p -> l = NULL;
return p;
}
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*);
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;
}
}
}
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;
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;
}
---------------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
struct SBst
{
int iData;
struct SBst* psLeft;
struct SBst* psRight;
};
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;
else
prev->psRight=btnew;
}
------------------------------------------------
INORDER
-----------------------------------------------
void fnInorderTraversal(theNode* psNode)
{
if(psNode!=NULL)
{
fnInorderTraversal(psNode->psLeft);
printf("%d ",psNode->iData);
fnInorderTraversal(psNode->psRight);
}
}
------------------------------------------------
PREORDER
-----------------------------------------------
if(psNode!=NULL)
{
printf("%d ",psNode->iData);
fnPreorderTraversal(psNode->psLeft);
fnPreorderTraversal(psNode->psRight);
}
}
--------------------------------------------------
POSTOREDER
---------------------------------------------------
if(psNode!=NULL)
{
fnPostorderTraversal(psNode->psLeft);
fnPostorderTraversal(psNode->psRight);
printf("%d ",psNode->iData);
}
}
------------------------------------------------------
COUNT NUMBER OF NODES IN TREE
-----------------------------------------------------
--------------------------------------------------------------
DOUBLY LINKED LIST
--------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
struct SDll
{
int iData;
struct SDll* psLeft;
struct SDll* psRight;
};
/*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);
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);
return (0);
}
--------------------------------------
CREATION
--------------------------------------
{
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
----------------------------------
-------------------------------------
INSERT END
--------------------------------------
--------------------------------------
INSERT MIDDLE
--------------------------------------
----------------------------------------
DELETE BEGIN
----------------------------------------
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
------------------------------------------
-------------------------------------------
SEARCHING A NODE
--------------------------------------------
#include<stdio.h>
#include<stdlib.h>
struct SQueue
{
int iData;
struct SQueue* psLink;
};
typedef struct SQueue theQueue;
/* Function Prototypes */
void fnCreateQueue(FILE*);
void fnDisplay(theQueue*);
void fnDeleteQueue(theQueue*);
-----------------------------
CREATION
-----------------------------
}
}
-------------------------------
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);
return (0);
}
----------------------------
CREATION
----------------------------
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
-------------------
----------------------
INSERT BEGIN
----------------------
-------------------------
INSERT END
--------------------------
-------------------------------
INSERT MIDDLE
-------------------------------
--------------------------------
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
------------------------------------
-----------------------------
SEARCH
---------------------------------
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;
/* 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
-----------------------------
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 ;
bnum = num;
} /* End of main() */
Outpput
Enter the binary number(1s and 0s)
1010
The Binary number is = 1010
Its 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*/
Output
Enter an integer
15
15 x 4 = 60
RUN2
Enter an integer
262
262 x 4 = 1048
---------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
void main()
{
int n,x1,i,j;
float x,sign,cosx,fact;
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);
}
} /*End of main() */
Output
Enter the number of the terms in aseries
5
Enter the value of x(in degrees)
60
Sum of the cosine series = 0.50
The 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];
Output
Enter the value of the n = 5
Enter the numbers
10
30
20
45
18
Cyclically permted numbers are given below
30
20
45
18
10
------------------------------------------------------------------------------------------------------------
---------------
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;
Output
Enter howmany elements
5
10
21
32
59
17
Avrage of all elements =27.80
Varience of all elements =27.80
Standard 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];
Output
Enter the value of N
5
Enter the numbers
80
20
67
10
45
The numbers arrenged in ascending order are given below
10
20
45
67
80
------------------------------------------------------------------------------------------------------------
---------------------
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;
} /* End of main() */
Output
Enter the value of N
6
Enter the numbers
10
35
67
100
42
688
The numbers arranged in descending order are given below
688
100
67
42
35
10
------------------------------------------------------------------------------------------------------------
----------------------------
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;
Output
Enter the value of N
6
Enter the numbers
30
80
10
40
70
90
The numbers arranged in descending order are given below
90
80
70
40
30
10
The 2nd largest number is = 80
The 2nd smallest number is = 30
The 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;
if (found == 1)
{
for(i=pos; i< n-1; i++)
{
vectx[i] = vectx[i+1];
}
} /* End of main() */
Output
Run 1
Enter how many elements
5
Enter the elements
30
10
50
20
40
Input array elements are
30
10
50
20
40
Enter the element to be deleted
35
Element 35 is not found in the vector
Run 2
Enter how many elements
4
Enter the elements
23
10
55
81
Input array elements are
23
10
55
81
Enter the element to be deleted
55
The resultant vector is
23
10
81
------------------------------------------------------------------------------------------------------------
----------------------
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 position of the element to split the array \n");
scanf ("%d",&a);
Output
Enter the value of n
5
enter the numbers
30
10
40
50
60
Enter the position of the element to split the array
2
The resultant array is
40
50
60
30
10
------------------------------------------------------------------------------------------------------------
------------------
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;
} /* End of main() */
Output
Enter the order ofthe matrix
33
Enter the coefficients if matrix
123
456
789
The given matrix is
123
456
789
------------------------------------------------------------------------------------------------------------
-----------------
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;
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]);
}
}
for (i=0;i<m;++i)
{
a = ma[i][i];
ma[i][i] = ma[i][m-i-1];
ma[i][m-i-1] = a;
}
} /* end of main() */
Output
Enetr the order of the matix
33
Enter the co-efficients of the matrix
123
456
789
The given matrix is
123
456
789
THe matrix after changing the
main diagonal & secondary diagonal
321
456
987
------------------------------------------------------------------------------------------------------------
--------------------------
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;
Output
Sum = 1275
------------------------------------------------------------------------------------------------------------
---------------------------
14. Write a C program to accept two integers and check if they are equal
#include <stdio.h>
void main()
{
int m,n;
if(m == n )
printf("M and N are equal\n");
else
printf("M and N are not equal\n");
output
Enter the values for M and N
34 45
M 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;
Output
Enter the Height (in centimetres)
45
Dwarf
------------------------------------------------------------------------------------------------------------
------------------------
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;
} /* End of main() */
Output
RUN 1
Enter the values for X and Y
35
point (5,3) lies in the First quandrant
RUN 2
Enter 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");
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;
} /* End of switch */
Output
Run 1
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
Enter the Figure code
2
Enter the bredth and length
2
6
Area of a Reactangle=12.000000
Run 2
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
------------------------------------------------------------------------------------------------------------
------------------------
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;
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 */
} /* End of main() */
Output
Enter the string containing both digits and alphabet
goodmorning25
NO. of Digits in the string=2
Sum 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;
temp = num;
while(num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
void main ()
{
static int m1[10][10];
int i,j,m,n,sum=0;
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;
}
}
Output
Enter the order of the matrix
33
Enter the co-efficients of the matrix
123
456
789
Sum of the 0 row is = 6
Sum of the 1 row is = 15
Sum of the 2 row is = 24
Sum of the 0 column is = 12
Sum of the 1 column is = 15
Sum 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;
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]);
}
}
for (i=0;i<m;++i)
{
sum = sum + ma[i][i];
a = a + ma[i][m-i-1];
}
Output
Enetr the order of the matix
33
Enter the co-efficients of the matrix
123
456
789
The given matrix is
123
456
789
------------------------------------------------------------------------------------------------------------
--------------------------
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;
} /*End of main() */
Output
Enter the order of the matrix
33
Enter the ncoefficients of the matrix
123
456
789
The normal of the given matrix is = 16
Trace of the matrix is = 15
Run 2
Enter the order of the matrix
22
Enter the ncoefficients of the matrix
24
68
The normal of the given matrix is = 10
Trace of the matrix is = 10
------------------------------------------------------------------------------------------------------------
----------------
#include <stdio.h>
void main ()
{
static int m1[10][10];
int i,j,m,n;
int counter=0;
} /* EN dof main() */
Output
Enter the order of the matix
22
Enter the co-efficients of the matix
12
34
The given matrix is not a sparse matrix
There are 0 number of zeros
Run 2
Enter the order of the matix
33
Enter the co-efficients of the matix
100
001
010
The given matrix is sparse matrix
There 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;
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)
{
for (j=0;j<n;++j)
{
printf (" %d",mb[i][j]);
}
printf ("\n");
}
#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;
clrscr();
fflush(stdin);
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);
}
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() */
*****************************
Names before sorting
Rajashree 123
John 222
Priya 331
Anand 411
Nirmala 311
*****************************
Names after sorting
*****************************
Anand 411
John 222
Nirmala 311
Priya 331
Rajashree 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;
} /*End of main()
Output
Enter a sentence
gOOD mORNING
#include <stdio.h>
void main()
{
int table[20];
int i, low, mid, high, key, size;
/* 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() */
Output
Output
Enter the size of an array
5
Enter the array elements
12
36
45
78
99
Enter the key
45
SUCCESSFUL SEARCH
------------------------------------------------------------------------------------------------------------
-----------------------------------------------------