This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Tuesday, February 8, 2011

C-programms



/* 45. CREATE A FILE OF TEN RECORDS WHERE EACH RECORD HAS
THE FOLLOWING FIELD : IDNO,NAME,MARKS AND AGE.
READ DATA INTO THIS FILE. FIND THE AVERAGE AGE OF
THESE TEN STUDENTS AND ALSO THE NAME OF THE STUDENT
WHO SCORED THE MAXIMUM MARKS.
*/
#include
#include

#define MAX 10

struct student{
char name[20],idno[11];
int age;
int marks;
};

void main()
{
struct student stu;
FILE *fp;
int i,maxmarks;
char maxname[20];
float avg=0;
clrscr();
printf("Enter the records for each student :");
fp=fopen("c:\\student.txt","w");
for(i=0;i {
printf("\nEnter %d student's record",i+1);
printf("\n NAME : ");
scanf("%s",stu.name);
printf(" IDNO : ");
scanf("%s",stu.idno);
printf(" AGE : ");
scanf("%d",&stu.age);
printf(" MARKS : ");
scanf("%d",&stu.marks);
fprintf(fp,"%s %s %d %d\n",stu.name,stu.idno,stu.age,stu.marks);
/* fwrite(&stu,sizeof(stu),1,fp);*/
}
fclose(fp);
fp=fopen("c:\\student.txt","r");
maxmarks=0;
/* while(fread(&stu,sizeof(stu),1,fp)==1)*/
while(fscanf(fp,"%s %s %d %d\n",stu.name,stu.idno,&stu.age,&stu.marks)!=EOF)
{
avg+=stu.age;
if(stu.marks>maxmarks)
{
maxmarks=stu.marks;
strcpy(maxname,stu.name);
}
}
fclose(fp);
avg=avg/MAX;
printf("\n\tAverage age of %d students is %f",MAX,avg);
printf("\n\t\"%s\" has scored max marks of %d",maxname,maxmarks);
getch();
}

/*
=====OUT PUT=====
Enter the records for each student :
Enter 1 student's record
NAME : QQQQQ
IDNO : 001
AGE : 20
MARKS : 67

Enter 2 student's record
NAME : WWWWWWW
IDNO : 002
AGE : 21
MARKS : 35

Enter 3 student's record
NAME : EEEEEEE
IDNO : 006
AGE : 22
MARKS : 54

Enter 4 student's record
NAME : RRRRRR
IDNO : 009
AGE : 22
MARKS : 89

Enter 5 student's record
NAME : TTTTTT
IDNO : 005
AGE : 21
MARKS : 58

Enter 6 student's record
NAME : YYYYYY
IDNO : 007
AGE : 22
MARKS : 98

Enter 7 student's record
NAME : UUUUUUU
IDNO : 0011
AGE : 23
MARKS : 70

Enter 8 student's record
NAME : IIIIIII
IDNO : 0012
AGE : 20
MARKS : 45

Enter 9 student's record
NAME : OOOOOO
IDNO : 0013
AGE : 19
MARKS : 88

Enter 10 student's record
NAME : PPPPPP
IDNO : 0015
AGE : 23
MARKS : 76

Average age of 10 students is 21.299999
"YYYYYY" has scored max marks of 98

*/






















/* 46. CONVERT ALL LOWER CASE LETTERS IN A FILE TO
UPPER CASE LETTERS.
*/

#include
#include

void main()
{
FILE *fp,*fp1;
char ch;
clrscr();

fp=fopen("c:\\stu.txt","r+");
if(fp==NULL)
printf("Cannot open source file");

fp1=fopen("c:\\stu1.txt","w");
if(fp1==NULL)
printf("Cannot open dest file");

else
{
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
else if(ch>='a' && ch<='z')
{
ch=ch-32;
fprintf(fp1,"%c",ch);
/* fputc(ch,fp);*/
}
else
fprintf(fp1,"%c",ch);
/* fputc(ch,fp);*/
}

fclose(fp);
fclose(fp1);

fp1=fopen("c:\\stu1.txt","r");
if(fp1==NULL)
printf("\nCannot open source2 file");


fp=fopen("c:\\stu.txt","w");
if(fp==NULL)
printf("\nCannot open dest2 file");

while(1)
{
ch=fgetc(fp1);
if(ch==EOF)
break;
else
fputc(ch,fp);
}

fclose(fp);
fclose(fp1);
}
getch();
}

/*
=====OUT PUT=====

CONTENTS IN FILE stu.txt:
kaundinya
bharath
srikanth

AFTER EXECUTING stu.txt:

KAUNDINYA
BHARATH
SRIKANTH
*/




















/* 47. WRITE A C PROGRAM TO COPY THE CONTENTS OF ONE FILE
TO ANOTHER.
*/

#include
#include

void main()
{
FILE *fp,*fp1;
char ch;
clrscr();

fp=fopen("c:\\stu.txt","r");
if(fp==NULL)
printf("\nCannot open source file");

fp1=fopen("c:\\stu1.txt","w");
if(fp1==NULL)
printf("\nCannot open dest file");

while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
else
fputc(ch,fp1);
}

fclose(fp);
fclose(fp1);
getch();
}
/*
=====OUT PUT=====

CONTENTS OF FILE stu.txt:
BHARATH
kaundinya
srikanth
bond007

AFTER EXECUTION CONTENTS OF
FILE STU1.txt:
BHARATH
kaundinya
srikanth
bond007
*/
/*49. WRITE A C PROGRAM TO APPEND A FILE */

#include
#include

void main()
{
FILE *fp;
char ch,arr[20];
int i=0;
clrscr();

fp=fopen("c:\\stu.txt","a");
if(fp==NULL)
printf("\nCannot open source file");

printf("Enter data to append\n");
gets(arr);

while(1)
{
ch=arr[i++];
if(ch=='\0')
break;
else
fputc(ch,fp);
}

fclose(fp);
getch();
}
/*
=====OUT PUT=====

CONTENTS OF FILE stu.txt:
BHARATH
kaundinya
srikanth
bond007

AFTER APPENDING:
BHARATH
kaundinya
srikanth
bond007
All are friends
*/



/* 50. CONSIDER TWO SORTED FILES FILE1 AND FILE2. WRITE A PROGRAM
TO GENERATE FILE3 WHICH IS THE RESULT OF MERGING FILE1 AND FILE2
IN THE SORTED ORDER.
*/
#include
#include

void main()
{
FILE *fp1,*fp2,*fp;
char ch1,ch2;
clrscr();

fp1=fopen("c:\\file1.txt","r");
if(fp==NULL)
printf("\nCannot open source1 file");

fp2=fopen("c:\\file2.txt","r");
if(fp1==NULL)
printf("\nCannot open source2 file");

fp=fopen("c:\\file3.txt","w");
if(fp1==NULL)
printf("\nCannot open destination file");

ch1=fgetc(fp1);
ch2=fgetc(fp2);
while(ch1!=EOF && ch2!=EOF)
{
if(ch1==ch2)
{
fputc(ch1,fp);
fputc(ch2,fp);
ch1=fgetc(fp1);
ch2=fgetc(fp2);
}
else if(ch1 < ch2)
{
fputc(ch1,fp);
ch1=fgetc(fp1);
}
else
{
fputc(ch2,fp);
ch2=fgetc(fp2);
}
}



while(ch1!=EOF)
{
fputc(ch1,fp);
ch1=fgetc(fp1);
}

while(ch2!=EOF)
{
fputc(ch2,fp);
ch2=fgetc(fp2);
}


fclose(fp1);
fclose(fp2);
fclose(fp);
getch();
}
/*
=====OUT PUT=====

CONTENTS OF FILE1:
a b j m o q st w

CONTENTS OF FILE2:
c k p r x z

CONTENTS OF FILE3:
a b c j k m o p q r st wx z
*/




















/* 51.WRITE A C PROGRAM TO DEMONSTRATE THE WORKING OF A STACK OF
SIZE N USING AN ARRAY.THE ELEMENTS OF THE STACK MAY BE
ASSUMED TO BE OF TYPE INTEGER OR REAL. THE OPERATIONS TO BE
SUPPORTED ARE:
a.PUSH b.POP C.DISPLAY
THE SHOULD PRINT APPROPRIATE MESSAGES FOR STACK OVERFLOW,
STACK UNDERFLOW AND STACK EMPTY. YOU MAY WRITE SEPERATE
FUNCTIONS TO DETECT THESE CASES.
*/
#include
#include

#define N 5

struct stack
{
int top;
int arr[N];
}st;


int empty()
{
if(st.top==-1)
return 1;
else
return 0;
}

int full()
{
if(st.top==N-1)
return 1;
else
return 0;
}

void push()
{
if(full())
printf("\n\tSTACK OVERFLOW...");
else
{
printf("\nEnter a element : ");
scanf("%d",&st.arr[++st.top]);
}
}



void pop()
{
if(empty())
printf("\n\tSTACK UNDERFLOW...");
else
printf("\n\tPoped element is %d ",st.arr[st.top--]);

}

void display()
{
int i;
if(empty())
printf("\n NO ELEMENTS\n");
else
for(i=0;i<=st.top;i++)
printf("\t%d",st.arr[i]);

}
void main()
{
int choice=0;
clrscr();
st.top=-1;
while(1)
{
printf("\n\n1.PUSH");
printf("\n2.POP");
printf("\n3.DISPLAY");
printf("\n4.EXIT");
printf("\n\tenter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
default:exit(0);break;
}
}
}









/*
=====OUT PUT=====
1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter your choice : 3

NO ELEMENTS


1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter your choice : 2

STACK UNDERFLOW...

MAIN MENU

enter your choice : 1

Enter a element : 2

MAIN MENU

enter your choice : 1

Enter a element : 45

MAIN MENU

enter your choice : 1

Enter a element : 68

MAIN MENU

enter your choice : 1

Enter a element : 12

MAIN MENU

enter your choice : 1

Enter a element : 34


MAIN MENU

enter your choice : 1

STACK OVERFLOW...

MAIN MENU

enter your choice : 3
2 45 68 12 34

MAIN MENU

enter your choice : 2

Poped element is 34
*/

/*59. WRITE A PROGRAM TO SIMULATE THE WORKING OF A QUEUE OF
NAMES USING AN ARRAY.PROVIDE THE FOLLOWING OPERATIONS:
a.CQINSERT b.CQDELETE c.CQDISPLAY
*/
#include
#include
#include

#define MAX 5
#define SIZE 20

struct cqueue
{
char name[MAX][SIZE];
int rear;
int front;
}cq;

int cqempty()
{
int i,count=0,flag=1;
if(cq.front==-1 && cq.rear ==-1)
return 1; /* Empty */
else
{
for(i=cq.front;count {
if( strcmp(cq.name[i],"")==0)
count++;
else
flag=0;
}
if(count==MAX)
return 1;
}
return 0; /* Not Empty */
}

int cqfull()
{
int i,count=0,flag=1;
for(i=cq.rear;count {
if( strcmp(cq.name[i],"")==0)
flag=0;
else
count++;
}
if(count==MAX)
return 1; /* Full */
return 0; /* Not Full */
}

void cqinsert()
{
int i;
if(!cqfull())
{
if(cq.front==-1 && cq.rear==-1)
cq.front=cq.rear=0;
else
cq.rear=(cq.rear+1)%MAX;
printf("\nEnter Name to Insert\t");
scanf("%s",cq.name[cq.rear]);
printf("\n INSERTED ");
}
else
printf("\nQueue is Full");
}

void cqdelete()
{
int i;
if(!cqempty())
{
printf("\nDeleted name is %s",cq.name[cq.front]);
strcpy(cq.name[cq.front],"");
if(!cqempty())
cq.front=(cq.front+1)%MAX;
else
cq.front=cq.rear=-1;
}
else
printf("Queue is empty");
}

void cqdisplay()
{
int i;
if(cqempty())
printf("QUEUE IS EMPTY");
else
{
for(i=cq.front;i!=cq.rear ; i=(i+1)%MAX)
printf("\n%s",cq.name[i]);
printf("\n%s",cq.name[i]);
}
}

void main()
{

int i,choice=4;

cq.rear=-1;
cq.front=-1;
for(i=0;i<5 data-blogger-escaped-br="br" data-blogger-escaped-i="i"> strcpy(cq.name[i],"");

while(choice!=0)
{
clrscr();
printf("\n 1. CQINSERT");
printf("\n 2. CQDELETE");
printf("\n 3. CQDISPLAY");
printf("\n 4. EXIT \t:");
scanf("%d",&choice);
switch(choice)
{
case 1:cqinsert();getch();break;
case 2:cqdelete();getch();break;
case 3:cqdisplay();getch();break;
default:choice=0;break;
}
}
}






/*
=====OUT PUT=====

1. CQINSERT
2. CQDELETE
3. CQDISPLAY
4. EXIT :3
QUEUE IS EMPTY

1. CQINSERT
2. CQDELETE
3. CQDISPLAY
4. EXIT :2
Queue is empty

MAIN MENU
:1
Enter Name to Insert BHARATH

INSERTED

MAIN MENU
:1
Enter Name to Insert KAUNDINYA

INSERTED

MAIN MENU
:1
Enter Name to Insert SRIKANTH

INSERTED

MAIN MENU
:1
Enter Name to Insert JAYANTH

INSERTED

MAIN MENU
:1
Enter Name to Insert SHARMA

INSERTED

MAIN MENU
:1
Queue is Full


MAIN MENU
:3
BHARATH
KAUNDINYA
SRIKANTH
JAYANTH
SHARMA

MAIN MENU
:2
Deleted name is BHARATH

MAIN MENU
:3
KAUNDINYA
SRIKANTH
JAYANTH
SHARMA
*/

/* 60.USING DYNAMIC VARIABLES AND POINTERS, CONSTRUCT A
SINGLY LINKED LIST CONSISTING OF THE FOLLOWING
INFORMATION IN EACH NODE :
a.LINSERT-INSERTING IN FRONT OF THE LIST
b.LDELETE-DELETING A NODE BASED ON JOB-ID
c.LSEARCH-SEARCHING A NODE BASED ON JOB-ID
d.LDISPLAY-DISPLAYING ALL THE NODES IN THE LIST
*/
#include
#include

struct node
{
int jobid;
char jobname[20],jobdesc[20];
struct node *next;
};

typedef struct node* NODEPTR;

NODEPTR getnode()
{
NODEPTR p=(NODEPTR)malloc(sizeof(struct node));
p->next=NULL;
return p;
}




NODEPTR Linsert(NODEPTR head)
{
NODEPTR p=getnode();

printf("\nEnter job id : ");
scanf("%d",&p->jobid);
printf("Enter job name : ");
scanf("%s",p->jobname);
printf("Enter job description : ");
scanf("%s",p->jobdesc);

if(head==NULL)
head=p;
else
{
p->next=head;
head=p;
}
printf("Inserted");
getch();
return head; /* Return the address to which head is pointing */
}

NODEPTR Ldelete(NODEPTR head)
{
int tempjobid;
NODEPTR p,q;
printf("\nEnter the jobid of node u want to delete : ");
scanf("%d",&tempjobid);

for(p=head,q=NULL ; p!=NULL ; q=p,p=p->next)
{
if(p->jobid==tempjobid)
{
if(q==NULL)
head=p->next;
else
q->next=p->next;

free(p);
break; /* Once deleted leave the loop */
}
}
if(p==NULL)
printf("NO NODE WITH THAT JOBID");
else
printf("Deleted.");
getch();
return(head);
}

void Lsearch(NODEPTR head)
{
NODEPTR p;
int tempjobid;

printf("Enter the job id to search : ");
scanf("%d",&tempjobid);
for(p=head;p!=NULL;p=p->next)
{
if(p->jobid==tempjobid)
{
printf("Found");
break; /* Once found leave the loop */
}
}
if(p==NULL)
printf("NOT FOUND");
getch();
}

void Ldisplay(NODEPTR head)
{
NODEPTR p;
printf("\n\tjobid\tjobname\tjobdesc\n");
for(p=head;p!=NULL;p=p->next)
printf("\n\t%d\t%s\t%s",p->jobid,p->jobname,p->jobdesc);
getch();
}

void main()
{
int choice=0;
NODEPTR head=NULL;
clrscr();

while(1)
{
clrscr();
printf("\n1. Insert front of the list");
printf("\n2. Delete node based on job-id");
printf("\n3. Searching a node based on job-id");
printf("\n4. Displaying all nodes in list");
printf("\n5. Exit");
printf("\n\tEnter your choice : ");
scanf("%d",&choice);




switch(choice)
{
case 1:head=Linsert(head);break;
case 2:head=Ldelete(head);break;
case 3:Lsearch(head);break;
case 4:Ldisplay(head);break;
default:exit(0);break;
}
}
}

/*
=====OUT PUT=====

1. Insert front of the list
2. Delete node based on job-id
3. Searching a node based on job-id
4. Displaying all nodes in list
5. Exit
Enter your choice : 1

Enter job id : 001
Enter job name : aaaaa
Enter job description : sssss
Inserted

MAIN MENU
Enter your choice : 1

Enter job id : 002
Enter job name : DDDDD
Enter job description : FFFFF
Inserted

MAIN MENU
Enter your choice : 1

Enter job id : 003
Enter job name : GGGG
Enter job description : HHHHH
Inserted

MAIN MENU
Enter your choice : 4

jobid jobname jobdesc

3 GGGG HHHHH
2 DDDDD FFFFF
1 aaaaa sssss

MAIN MENU
Enter your choice : 3
Enter the job id to search : 002
Found

MAIN MENU
Enter your choice : 3
Enter the job id to search : 004
NOT FOUND

MAIN MENU
Enter your choice : 2

Enter the jobid of node u want to delete : 003
Deleted.

MAIN MENU
Enter your choice : 2

Enter the jobid of node u want to delete : 003
NO NODE WITH THAT JOBID

MAIN MENU
Enter your choice : 4

jobid jobname jobdesc

2 DDDDD FFFFF
1 aaaaa sssss

*/


















/*61. A DEQUE IS A LIST IN WHICH ITEMS CAN BE ADDED OR DELETED FROM
EITHER END OF THE LIST.IMPLEMENT THE WORKING OF SUCH A DEQUE OF
INTEGERS
*/
#include
#include

struct node
{
int val;
struct node *next;
};

typedef struct node* NODEPTR;

NODEPTR getnode()
{
NODEPTR p=(NODEPTR)malloc(sizeof(struct node));
p->next=NULL;
return p;
}

NODEPTR Insertleft(NODEPTR front,NODEPTR rear)
{
NODEPTR p=getnode();

printf("Enter value of node : ");
scanf("%d",&p->val);

if(front==NULL && rear ==NULL)
{
front=p;
rear=p;
}
else
{
p->next=front;
front=p;
}
return front;
}

NODEPTR Insertright(NODEPTR front,NODEPTR rear)
{
NODEPTR p=getnode();

printf("Enter value of node : ");
scanf("%d",&p->val);


if(rear==NULL && front==NULL)
front=rear=p;
else
{
rear->next=p;
rear=p;
}
return rear;
}
NODEPTR Removeleft(NODEPTR front,NODEPTR rear)
{
NODEPTR p;
if(front==NULL)
printf("No element to delete");
else
{
if(front==rear)
front=NULL;
else
{
p=front;
front=p->next;
}
printf("\nDeleted element from left is %d",p->val);
free(p);
}
return front;
}

NODEPTR Removeright(NODEPTR front,NODEPTR rear)
{
NODEPTR p;
if(rear==NULL)
printf("No elements to delete");
else
{
if(front==rear)
rear=NULL;
else
{
for(p=front;p->next!=rear;p=p->next);
rear=p;
p=p->next;
rear->next=NULL;
}
printf("\nDeleted element from right is %d",p->val);
free(p);
}
return rear;
}

void Display(NODEPTR front,NODEPTR rear)
{
NODEPTR p;
if(front==NULL && rear==NULL)
printf("No elements");
else
{
for(p=front;p!=NULL;p=p->next)
printf("\t%d",p->val);
}
}

NODEPTR placefront(NODEPTR front,NODEPTR rear)
{
if(rear==NULL)
front=NULL;
else if(front==NULL)
front=rear;
return front;
}

NODEPTR placerear(NODEPTR front,NODEPTR rear)
{
NODEPTR p;
if(front==NULL)
rear=NULL;
else
{
for(p=front;p->next!=NULL;p=p->next);
rear=p;
}
return rear;
}

void main()
{
int choice=0;
NODEPTR front=NULL,rear=NULL;
clrscr();
while(1)
{
printf("\n\n1. Insert left");
printf("\n2. Insert Right");
printf("\n3. Remove left");
printf("\n4. Remove right");
printf("\n5. Display");
printf("\n6. Exit");
printf("\n\tEnter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:front=Insertleft(front,rear);
rear=placerear(front,rear);
break;
case 2:rear=Insertright(front,rear);
front=placefront(front,rear);
break;
case 3:front=Removeleft(front,rear);
rear=placerear(front,rear);
break;
case 4:rear=Removeright(front,rear);
front=placefront(front,rear);
break;
case 5:Display(front,rear);break;
default:exit(0);break;
}
}
}

/*
=====OUT PUT=====

1. Insert left
2. Insert Right
3. Remove left
4. Remove right
5. Display
6. Exit
Enter your choice : 5
No elements

MAIN MENU
Enter your choice : 4
No elements to delete

MAIN MENU
Enter your choice : 3
No element to delete

MAIN MENU
Enter your choice : 1
Enter value of node : 1

MAIN MENU
Enter your choice : 1
Enter value of node : 2


MAIN MENU
Enter your choice : 2
Enter value of node : 5

MAIN MENU
Enter your choice : 2
Enter value of node : 4

MAIN MENU
Enter your choice : 5
2 1 5 4

MAIN MENU
Enter your choice : 3

Deleted element from left is 2

MAIN MENU
Enter your choice : 3

Deleted element from left is 1

MAIN MENU
Enter your choice : 3

Deleted element from left is 5

MAIN MENU
Enter your choice : 5
4

MAIN MENU
Enter your choice : 2
Enter value of node : 6

MAIN MENU
Enter your choice : 4

Deleted element from right is 6

MAIN MENU
Enter your choice : 4

Deleted element from right is 4

MAIN MENU
Enter your choice : 5
No elements
*/

/* 63.USING POINTERS AND DYNAMIC VARIABLES, CONSTRUCT A
BINARY SEARCH TREE (BST) OF INTEGERS.
a.GIVEN A KEY, PERFORM A SEARCH IN THE BST.IF THE KEY IS
FOUND THEN DISPLAY "KEY FOUND", ELSE INSERT THE KEY IN BST
b.WHILE CONSTRUCTING THE BST, DO NOT ADD ANY DUPLICATES.
c.DISPLAY THE TREE USING ANY ONE OF THE TRAVERSAL METHODS.
*/
#include
#include

struct node
{
int val;
struct node *left;
struct node *right;
};

typedef struct node* NODEPTR;

NODEPTR getnode()
{
NODEPTR p=(NODEPTR)malloc(sizeof(struct node));
p->left=NULL;
p->right=NULL;
return p;
}

NODEPTR searchbst(NODEPTR root)
{
NODEPTR p,q;
int num,flag=1;
printf("\n\nEnter an element to search\t");
scanf("%d",&num);

if(root == NULL)
{
p=getnode();
p->val=num;
root=p;
printf("\tINSERTED");
}
else
{
q=root;
while(flag!=0)
{

if(num==q->val)
{
printf("KEY FOUND");
flag=0;
}
else if(num < q->val) /*less than */
{
if(q->left != NULL)
q=q->left;
else
{
p=getnode();
p->val=num;
q->left=p;
flag=0;
printf("\tINSERTED");
}
}
else/* greater than */
{
if(q->right != NULL)
q=q->right;
else
{
p=getnode();
p->val=num;
q->right=p;
flag=0;
printf("\tINSERTED");
}
}

} /* end while(flag) */
} /* end else */
return root;
}

void inorder(NODEPTR root)
{
if(root!=NULL)
{
inorder(root->left);
printf(" %d ",root->val);
inorder(root->right);
}
}

void main()
{
NODEPTR root=NULL;
int choice;
clrscr();
while(choice!=0)
{
clrscr();
printf("\nEnter 1 to search");
printf("\nEnter 2 to traverse using inorder");
printf("\nEnter 3 to exit");
printf("\n\t\tchoice = ");
scanf("%d",&choice);
switch(choice)
{
case 1: root=searchbst(root);getch();break;
case 2: inorder(root);getch();break;
default:choice=0;break;
}
}
}
/*
=====OUT PUT=====

Enter 1 to search
Enter 2 to traverse using inorder
Enter 3 to exit
choice = 1
Enter an element to search 123
INSERTED

MAIN MENU
choice = 1
Enter an element to search 50
INSERTED

MAIN MENU
choice = 1
Enter an element to search 150
INSERTED
MAIN MENU
choice = 1
Enter an element to search 75
INSERTED

MAIN MENU
choice = 2
50 75 123 125 150
*/







/* 66. WRITE A C PROGRAM TO SORT A LIST OF N ELEMENTS OF
INTEGER TYPES USING THE QUICK SORT ALGORITHM.
*/
#include
#include

/* Function to Partition */
int partition(int a[],int low,int high)
{
int i,j,temp,key;
key=a[low];
i=low+1;
j=high;
while(1)
{
while(i=a[i])
i++;

while(key < a[j])
j--;

if(i temp=a[i],a[i]=a[j],a[j]=temp;
else
{
temp=a[low],a[low]=a[j],a[j]=temp;
return j;
}

}
}

/* Function to Quick Sort */
void quicksort(int a[],int low,int high)
{
int j;
if(low < high)
{
j=partition(a,low,high);
quicksort(a,low,j-1);
quicksort(a,j+1,high);
}
}







void main()
{
int i,n,a[20];
clrscr();
printf("Enter The Value for N : ");
scanf("%d",&n);
printf("Enter The Numbers To be Sorted :\n");
for(i=0;i scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("The Sorted array is ...\n");
for(i=0;i printf("\n%d",a[i]);
getch();
}

/*
=====OUT PUT=====

Enter The Value for N : 5
Enter The Numbers To be Sorted :
9
7
8
4
6
The Sorted array is ...

4
6
7
8
9

*/















/*67. WRITE A C PROGRAM TO SIMULATE THE WORKING OF
TOWERS OF HANOI PROBLEM FOR N DISC.PRINT THE NUMBER OF
MOVES BY YOUR PROGRAM.
*/
#include
#include
int count=0;
void tower(int n,int source,int temp,int destination)
{
if(n==1)
{
printf("Move disk 1 from %c to %c\n",source,destination);
count++;
return;
}

/* Move n-1 disks from A to B using C as Temporary Peg */
tower(n-1,source,destination,temp);
printf("Move disk %d from %c to %c\n",n,source,destination);
count++;

/* Move n-1 disks from B to C using A as Temporary peg */
tower(n-1,temp,source,destination);
}

void main()
{
int n;
clrscr();
printf("\n Enter the Number of disks : ");
scanf("%d",&n);
tower(n,'A','B','C');
printf("\n Total number of disk moves = %d",count);
getch();
}















/*
=====OUT PUT=====

Enter the Number of disks : 4
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
Move disk 3 from A to B
Move disk 1 from C to A
Move disk 2 from C to B
Move disk 1 from A to B
Move disk 4 from A to C
Move disk 1 from B to C
Move disk 2 from B to A
Move disk 1 from C to A
Move disk 3 from B to C
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C

Total number of disk moves = 15
*/




























BONUS PROGRAMS

/* C PROGRAM TO PRINT ASCII CODES */

#include
#include

void main()
{
int i;
char choice;
clrscr();
printf("\t\tEnter 'n' to stop\n\n");
for(i=0;i<256 data-blogger-escaped-br="br" data-blogger-escaped-i="i"> {
printf(" %d is %c \n",i,i);
choice=getch();
if(choice=='n' || choice=='N')
break;
}

}

/* C PROGRAM TO PRINT BIOSKEY */

#include
#include
#include

void main()
{
unsigned int a;
clrscr();
printf("Enter a key : ");
a=bioskey(0);
printf("The value is %u",a);
getch();

}
http://rajkumardownloads.blogspot.com/

C-programms

C-programms
/* 33. USE RECURSION TO FIND THE FIRST N TERMS OF THE FIBONACCI
SERIES
*/
#include
#include
int fib(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return(fib(n-1)+fib(n-2));
}
void main()
{
int n,i;
clrscr();
printf("Enter the value of N : ");
scanf("%d",&n);
printf("\n The fibonacci series is...\n");
for(i=0;i
{
printf("%d\n",fib(i));
}
getch();
}
/*
=====OUT PUT=====
Enter the value of N : 10
The fibonacci series is...
0
1
1
2
3
5
8
13
21
34
*/
/* 34. WRITE A RECURSIVE FUNCTION TO FIND THE FACTORIAL OF A NUMBER */
#include
#include
long int fact(int n)
{
if(n==0)
return 1;
else
return(n*fact(n-1));
}
void main()
{
int n;
clrscr();
printf("Enter the value of N : ");
scanf("%d",&n);
printf("Factorial of %d is %ld",n,fact(n));
getch();
}
/*
=====OUT PUT=====
Enter the value of N : 6
Factorial of 6 is 720
*/
/* 35. WRITE A RECURSIVE FUNCTION TO FIND THE SUM OF N NATURAL
NUMBERS
*/
#include
#include
int sum(int n)
{
if(n==1)
return 1;
else
return(n+sum(n-1));
}
void main()
{
int n;
clrscr();
printf("Enter the value of N : ");
scanf("%d",&n);
printf("Sum of %d integers is %d",n,sum(n));
getch();
}
/*
=====OUT PUT=====
Enter the value of N : 10
Sum of 10 integers is 55
*/
/* 37. USE RECURSION TO FIND X^n WHERE X IS A REAL NUMBER AND n
IS AN INTEGER. n CAN BE NEGATIVE, ZERO OR POSITIVE
*/
#include
#include
float fun(float x,int n)
{
if(n==0)
return 1;
else if(n>0)
return(x*fun(x,n-1));
else
return(1/x*fun(x,n+1));
}
void main()
{
int n;
float x;
clrscr();
printf("Enter value of X and n for [X^n] : ");
printf("\n X = ");
scanf("%f",&x);
printf(" n = ");
scanf("%d",&n);
printf("\nThe result is %f",fun(x,n));
getch();
}
/*
=====OUT PUT=====
1. Enter value of X and n for [X^n] :
X = 4
n = 2
The result is 16.000000
2. Enter value of X and n for [X^n] :
X = 2
n = 4
The result is 16.000000
*/
/* 38. FIND THE SOLUTION OF THE GIVEN QUADRATIC EQUATION. ( CREATE
A MACRO WHICH CALCULATES THE DETERMINANT.
____________
-A ± Ö B^2 - 4*A*C
Formula used:- X = --------------------------------
2*A
*/
#include
#include
#include
#define DET(a,b,c) b*b-4*a*c
void main()
{
float a,b,c;
clrscr();
printf("Enter coordinates of quad eqn: Ax2 + Bx + C : ");
printf("\n A = ");
scanf("%f",&a);
printf(" B = ");
scanf("%f",&b);
printf(" C = ");
scanf("%f",&c);
printf("\nRoots are : ");
if(DET(a,b,c)>=0)
{
printf("\n(1). %f",(-a+sqrt(DET(a,b,c)))/(2*a));
printf("\n(2). %f",(-a-sqrt(DET(a,b,c)))/(2*a));
}
else
{
printf("\n(1). %f + %f i",-a/(2*a),sqrt(-(DET(a,b,c)))/(2*a) );
printf("\n(2). %f + %f i",-a/(2*a),-sqrt(-(DET(a,b,c)))/(2*a) );
}
getch();
}
/*
=====OUTPUT=====
1.Enter coordinates of quad eqn: Ax2 + Bx + C :
A = 2
B = 4
C = 1
Roots are :
(1). 0.207107
(2). -1.207107
2.Enter coordinates of quad eqn: Ax2 + Bx + C :
A = 4
B = 1
C = 9
Roots are :
(1). -0.500000 + 1.494783 i
(2). -0.500000 + -1.494783 i
*/
/* 39. FIND THE LARGEST AND SECOND LARGEST OF THREE INTEGERS.
( CREATE MACRO WHICH FINDS LARGEST OF TWO INTEGERS.USE THIS
MACRO MULTIPLE TIMES. USE A NESTED IF ELSE STRUCTURE.)
*/
#include
#include
#define LARGE(A,B) A>B ? A : B
void main()
{
int a,b,c,temp;
clrscr();
printf("Enter three integers : ");
scanf("%d %d %d",&a,&b,&c);
temp=LARGE(b,c);
temp=LARGE(a,temp);
printf("\nLargest is : %d",temp);
if(temp==a)
printf("\nSecond Largest is : %d",LARGE(b,c));
else if(temp==b)
printf("\nSecond Largest is : %d",LARGE(a,c));
else
printf("\nSecond Largest is : %d",LARGE(a,b));
getch();
}
/*
=====OUTPUT=====
Enter three integers : 546 234 987
Largest is : 987
Second Largest is : 546
*/
/* 40. CREATE AN ARRAY OF TEN ELEMENTS WHERE EACH ELEMENT IS A
STRUCTURE WITH THE FOLLOWING FIELDS : IDNO, NAME,MARKS AND
AGE. READ DATA INTO THIS ARRAY. FIND THE AVERAGE MARKS OF THESE
TEN STUDENTS AND ALSO THE NAME OF THE YOUNGEST STUDENT.
*/
#include
#include
#define MAX 10
struct student{
char name[20],idno[10];
int age;
int marks;
};
void main()
{
struct student arr[MAX];
int i,count,minage;
char minname[MAX][20];
float avg=0;
clrscr();
printf("Enter the records for each student :");
for(i=0;i
{
printf("\nEnter %d student's record",i+1);
printf("\n NAME : ");
scanf("%s",arr[i].name);
printf(" IDNO : ");
scanf("%s",arr[i].idno);
printf(" AGE : ");
scanf("%d",&arr[i].age);
printf(" MARKS : ");
scanf("%d",&arr[i].marks);
}
for(i=0;i
avg+=arr[i].marks;
avg=avg/MAX;
count=0;
i=0;
minage=arr[i].age;
strcpy(minname[count++],arr[i].name);
i++;
for(;i
{
if(arr[i].age <>
{
count=0;
minage=arr[i].age;
strcpy(minname[count++],arr[i].name);
}
else if(arr[i].age == minage)
strcpy(minname[count++],arr[i].name);
}
printf("\n\tAverage marks of %d students is %f",MAX,avg);
printf("\n\tThe youngest student/s is/are");
for(i=0;i
printf("\n\t%s",minname[i]);
getch();
}
/*
=====OUTPUT=====
Enter the records for each student :
Enter 1 student's record
NAME : AAAAAAA
IDNO : 0111
AGE : 19
MARKS : 56
Enter 2 student's record
NAME : BBBBBBB
IDNO : 0112
AGE : 20
MARKS : 89
Enter 3 student's record
NAME : CCCCCCC
IDNO : 0114
AGE : 23
MARKS : 78
Enter 4 student's record
NAME : DDDDDDD
IDNO : 0118
AGE : 24
MARKS : 67
Enter 5 student's record
NAME : EEEEEE
IDNO : 0115
AGE : 22
MARKS : 87
Enter 6 student's record
NAME : FFFFFF
IDNO : 0117
AGE : 21
MARKS : 90
Enter 7 student's record
NAME : GGGGGGG
IDNO : 0120
AGE : 20
MARKS : 35
Enter 8 student's record
NAME : HHHHH
IDNO : 0122
AGE : 23
MARKS : 59
Enter 9 student's record
NAME : IIIIII
IDNO : 0128
AGE : 22
MARKS : 45
Enter 10 student's record
NAME : JJJJJJ
IDNO : 0116
AGE : 19
MARKS : 99
Average marks of 10 students is 70.500000
The youngest student/s is/are
AAAAAAA
JJJJJJ
*/
/* 41. DECLARE THREE POINTER VARIABLES TO STORE A CHARACTER,
A CHARACTER STRING AND AN INTEGER RESPECTIVELY. INPUT VALUES
INTO THESE VARIABLES. OUTPUT THESE POINTER VARIABLES AS WELL
AS THEIR CONTENTS.
*/
#include
#include
void main()
{
int a,*pi;
char c,str[20];
char *pc,*ps;
clrscr();
printf("Enter a character : ");
scanf("%c",&c);
printf("Enter an integer : ");
scanf("%d",&a);
printf("Enter a string : ");
scanf("%s",str);
pi=&a;
pc=&c;
ps=str;
printf("\nThe value of character pointer is : %u",pc);
printf("\nThe content of character pointer is : %c\n",*pc);
printf("\nThe value of integer pointer is : %u",pi);
printf("\nThe content of integer pointer is : %d\n",*pi);
printf("\nThe value of string pointer is : %u",ps);
printf("\nThe content of string pointer is : %s",ps);
getch();
}
/*
=====OUTPUT=====
Enter a character : S
Enter an integer : 9
Enter a string : kaun
The value of character pointer is : 65523
The content of character pointer is : S
The value of integer pointer is : 65524
The content of integer pointer is : 9
The value of string pointer is : 65500
The content of string pointer is : kaun
*/
/* 44. INPUT THREE INTEGERS X, Y AND N.PERFORM
a. BIT WISE AND OF X AND Y
b. BIT WISE INCLUSIVE OR OF X AND Y
c. BIT WISE EXCLUSIVE OR OF X AND Y
d. BIT WISE COMPLEMENT OF X
e. LEFT SHIFT OF X BY N BITS
f. RIGHT SHIFT OF Y BY N BITS
DISPLAY X,Y AND THE RESULTS OF THE ABOVE OPERATIONS IN BIT
FORMAT.
*/
#include
#include
#define MAX 25
char* dectobin(unsigned int dec,char arr[])
{
unsigned int n;
int i=0,r;
char a,temp;
char *f,*re;
n=dec;
if(n==0)
arr[i++]='0';
else
{
while(n>0)
{
r=n%2;
n=n/2;
a=(char)r+'0';
arr[i++]=a;
}
}
arr[i]='\0';
/* To reverse array */
f=arr;
re=&arr[strlen(arr)-1];
while(f
{
temp= *f;
*f=*re;
*re=temp;
f++;
re--;
}
return arr;
}
void main()
{
unsigned int x,y,n,ans;
char arr[MAX];
clrscr();
printf("Enter three integers : ");
printf("\n X = ");
scanf("%d",&x);
printf(" Y = ");
scanf("%d",&y);
printf(" N = ");
scanf("%d",&n);
printf("\n X = decimal : %u \t bin : %s",x,dectobin(x,arr));
printf("\n Y = decimal : %u \t bin : %s\n",y,dectobin(y,arr));
ans=x&y;/* bitwise AND */
printf("\n X&Y = decimal : %u \t bin : %s",ans,dectobin(ans,arr));
ans=x|y;/* bitwise INCLUSIVE OR */
printf("\n X|Y = decimal : %u \t bin : %s",ans,dectobin(ans,arr));
ans=x^y;/* bitwise EXCLUSIVE OR */
printf("\n X^Y = decimal : %u \t bin : %s",ans,dectobin(ans,arr));
ans=~x;/* bitwise COMPLEMENT */
printf("\n !X = decimal : %u \t bin : %s",ans,dectobin(ans,arr));
ans=x<
printf("\n X<
ans=y>>n;/* Right shift by n bits*/
/*printf("\n%d",ans); */
printf("\n Y>>N = decimal : %u \t bin : %s",ans,dectobin(ans,arr));
getch();
}
/*
=====OUT PUT=====
Enter three integers :
X = 5
Y = 9
N = 2
X = decimal : 5 bin : 101
Y = decimal : 9 bin : 1001
X&Y = decimal : 1 bin : 1
X|Y = decimal : 13 bin : 1101
X^Y = decimal : 12 bin : 1100
!X = decimal : 65530 bin : 1111111111111010
X< bin : 10100
Y>>N = decimal : 2 bin : 10