Tuesday 29 September 2015

Write a CPP Program To Print Hello World

1:  #include<iostream.h>  
2:  #include<conio.h>  
3:  #include<stdio.h>  
4:  void main()  
5:  {  
6:    cout << "Hello World" << endl;  
7:       getch();  
8:  }  

Write a CPP Program To Find Greater Number From Inputed Two Numbers

1:  #include<iostream.h>  
2:  #include<conio.h>  
3:  #include<stdio.h>  
4:  void main()  
5:  {  
6:    int a,b;  
7:    cout << "Enter 2 numbers" << endl;  
8:    cin>>a>>b;  
9:    if (a>b)  
10:      cout << "Greater is : " << a<<endl;  
11:    else  
12:      cout << "Greater is : " << b<<endl;  
13:       getch();  
14:  }  

Write a CPP Program To Perform Addition,Subtraction,Multiliation And Division using Float Data type

1:  #include<iostream.h>  
2:  #include<conio.h>  
3:  #include<stdio.h>  
4:  void main()  
5:  {  
6:    float a,b;  
7:    cout << "Enter 2 numbers" << endl;  
8:    cin >> a>>b;  
9:    int sum=a+b;  
10:    int diff=a-b;  
11:    int mul=a*b;  
12:    float div=a/b;  
13:    cout << "Sum = " << sum <<endl;  
14:    cout << "Difference = " << diff <<endl;  
15:    cout << "Multiplication = " << mul <<endl;  
16:    cout << "Division = " << div <<endl;  
17:       getch();  
18:  }  

Write a C rogram To Perform Radix Sort in Data Structure

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void radix_sort(int arr[], int n);  
4:  void main()  
5:  {  
6:   int a[20],n,i;  
7:   clrscr();  
8:   printf("Enter the number of elements to be sorted:\n");  
9:   scanf("%d",&n);  
10:   printf("Enter the elements:\n");  
11:   for(i=0;i<n;i++)  
12:   {  
13:   scanf("%d",&a[i]);  
14:   }  
15:   printf("\nArray elements before Radix Sort:\n");  
16:   for(i=0;i<n;i++)  
17:   {  
18:   printf("%d\t",a[i]);  
19:   }  
20:   radix_sort(a,n);  
21:   printf("\nArray elemants after Radix Sort:\n");  
22:   for(i=0;i<n;i++)  
23:   {  
24:   printf("%d\t",a[i]);  
25:   }  
26:   getch();  
27:  }  
28:  void radix_sort(int arr[],int n)  
29:  {  
30:   int bucket[10][5],buck[10],b[10];  
31:   int i,j,k,l,num,div,large,passes;  
32:   div=1;  
33:   num=0;  
34:   large=arr[0];  
35:   for(i=0;i<n;i++)  
36:   {  
37:   if(arr[i]>large)  
38:   {  
39:    large=arr[i];  
40:   }  
41:   while(large>0)  
42:   {  
43:    num++;  
44:    large=large/10;  
45:   }  
46:   for(passes=0;passes<num;passes++)  
47:   {  
48:    for(k=0;k<10;k++)  
49:    {  
50:    buck[k]=0;  
51:    }  
52:    for(i=0;i<n;i++)  
53:    {  
54:    l=((arr[i]/div)%10);  
55:    bucket[l][buck[l]++]=arr[i];  
56:    }  
57:    i=0;  
58:    for(k=0;k<10;k++)  
59:    {  
60:    for(j=0;j<buck[k];j++)  
61:    {  
62:     arr[i++]=bucket[k][j];  
63:    }  
64:    }  
65:    div*=10;  
66:   }  
67:   }  
68:  }  

Write a C Program To Perform Shell Sort

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void main()  
4:  {  
5:   int a[20];  
6:   int i,j,k,temp,n;  
7:   clrscr();  
8:   printf("Enter the number of elements to be sorted:\n");  
9:   scanf("%d",&n);  
10:   printf("Enter the elements:\n");  
11:   for(i=0;i<n;i++)  
12:   {  
13:   scanf("%d",&a[i]);  
14:   }  
15:   for(i=n/2;i>0;i=i/2)  
16:   {  
17:   for(j=i;j<n;j++)  
18:   {  
19:    for(k=j-i;k>=0;k=k-i)  
20:    {  
21:    if(a[k+i]>=a[k])  
22:     break;  
23:    else  
24:    {  
25:     temp=a[k];  
26:     a[k]=a[k+i];  
27:     a[k+i]=temp;  
28:    }  
29:    }  
30:   }  
31:   }  
32:   printf("\nArray elements after shell sorting are:\n");  
33:   for(i=0;i<n;i++)  
34:   {  
35:   printf("%d\t",a[i]);  
36:   }  
37:   getch();  
38:  }  

Write a C Program To Perform Quick Sorting Technique

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void quicksort(int b[10],int first,int last);  
4:  void main()  
5:  {  
6:   int a[20],n,i;  
7:   clrscr();  
8:   printf("Enter the number of elements to be sorted:\n");  
9:   scanf("%d",&n);  
10:   printf("Enter the elements:\n");  
11:   for(i=0;i<n;i++)  
12:   {  
13:   scanf("%d",&a[i]);  
14:   }  
15:   printf("\nArray elements before quick sort:\n");  
16:   for(i=0;i<n;i++)  
17:   {  
18:   printf("%d\t",a[i]);  
19:   }  
20:   quicksort(a,0,n-1);  
21:   printf("\n\nArray elements after quick sort:\n");  
22:   for(i=0;i<n;i++)  
23:   {  
24:   printf("%d\t",a[i]);  
25:   }  
26:   getch();  
27:  }  
28:  void quicksort(int b[10],int first,int last)  
29:  {  
30:   int pivot,j,temp,i;  
31:   if(first<last)  
32:   {  
33:   pivot=first;  
34:   i=first;  
35:   j=last;  
36:   while(i<j)  
37:   {  
38:    while(b[i]<=b[pivot] && i<last)  
39:    i++;  
40:    while(b[j]>b[pivot])  
41:    j--;  
42:    if(i<j)  
43:    {  
44:    temp=b[i];  
45:    b[i]=b[j];  
46:    b[j]=temp;  
47:    }  
48:   }  
49:   temp=b[pivot];  
50:   b[pivot]=b[j];  
51:   b[j]=temp;  
52:   quicksort(b,first,j-1);  
53:   quicksort(b,j+1,last);  
54:   }  
55:  }  

Write a C Program That performing Merge Sort(Sorting Techniues)

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #define MAX 20  
4:  void mergesort(int a[],int low,int high);  
5:  void merge(int a[],int beg,int mid,int end);  
6:  void display(int a[],int num);  
7:  void main()  
8:  {  
9:   int n,i,a[MAX];  
10:   clrscr();  
11:   printf("Enter the number of elements (<MAX )\n");  
12:   scanf("%d",&n);  
13:   printf("Enter the elements\n");  
14:   for(i=0;i<n;i++)  
15:   {  
16:   scanf("%d",&a[i]);  
17:   }  
18:   printf("\nArray elements before sorting are:\n");  
19:   display(a,n);  
20:   mergesort(a,0,n-1);  
21:   printf("\nArray elements after sorting are:\n");  
22:   display(a,n);  
23:   getch();  
24:  }  
25:  void mergesort(int a[],int low,int high)  
26:  {  
27:   int mid;  
28:   if(low<high)  
29:   {  
30:   mid=(low+high)/2;  
31:   mergesort(a,low,mid);  
32:   mergesort(a,mid+1,high);  
33:   merge(a,low,mid,high);  
34:   }  
35:  }  
36:  void merge(int a[],int beg, int mid,int end)  
37:  {  
38:   int temp,b[MAX],left,right,i;  
39:   left=beg;  
40:   right=mid+1;  
41:   temp=beg;  
42:   while((left<=mid) && (right<=end))  
43:   {  
44:   if(a[left]<a[right])  
45:   {  
46:    b[temp]=a[left];  
47:    left=left+1;  
48:   }  
49:   else  
50:   {  
51:    b[temp]=a[right];  
52:    right=right+1;  
53:   }  
54:   temp=temp+1;  
55:   }  
56:   while(left<=mid)  
57:   {  
58:   b[temp]=a[left];  
59:   left=left+1;  
60:   temp=temp+1;  
61:   }  
62:   while(right<=end)  
63:   {  
64:   b[temp]=a[right];  
65:   right=right+1;  
66:   temp=temp+1;  
67:   }  
68:   for(i=beg;i<=end;i++)  
69:   {  
70:   a[i]=b[i];  
71:   }  
72:  }  
73:  void display(int a[],int num)  
74:  {  
75:   int i;  
76:   for(i=0;i<num;i++)  
77:   {  
78:   printf("%d\t",a[i]);  
79:   }  
80:  }  

Write a C Program To perform Heap Sort in Data Structure(Sorting Techniques)

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void heapsort(int a[50], int n);  
4:  void heapify(int[], int);  
5:  void adjust(int[], int);  
6:  void main()  
7:  {  
8:   int a[50],n,i;  
9:   clrscr();  
10:   printf("Enter the no. of elements to be sorted:\n");  
11:   scanf("%d",&n);  
12:   printf("Enter the elements:\n");  
13:   for(i=0;i<n;i++)  
14:   {  
15:   scanf("%d",&a[i]);  
16:   }  
17:   printf("\nArray elements before Heapsort:\n");  
18:   for(i=0;i<n;i++)  
19:   {  
20:   printf("%d\t",a[i]);  
21:   }  
22:   heapsort(a,n);  
23:   printf("\nArray elements after Heapsort:\n");  
24:   for(i=0;i<n;i++)  
25:   {  
26:   printf("%d\t",a[i]);  
27:   }  
28:   getch();  
29:  }  
30:  void heapsort(int a[50], int n)  
31:  {  
32:   int i,t;  
33:   heapify(a,n);  
34:   for(i=n-1;i>0;i--)  
35:   {  
36:   t=a[0];  
37:   a[0]=a[i];  
38:   a[i]=t;  
39:   adjust(a,i);  
40:   }  
41:  }  
42:  void heapify(int a[50], int n)  
43:  {  
44:   int item,i,j,k;  
45:   for(k=1;k<n;k++)  
46:   {  
47:   item=a[k];  
48:   i=k;  
49:   j=(i-1)/2;  
50:   while((i>0) && (item>a[j]))  
51:   {  
52:    a[i] = a[j];  
53:    i=j;  
54:    j=(i-1)/2;  
55:   }  
56:   a[i]=item;  
57:   }  
58:  }  
59:  void adjust(int a[50], int n)  
60:  {  
61:   int item,i,j;  
62:   j=0;  
63:   item=a[j];  
64:   i=2*j+1;  
65:   while(i<=n-1)  
66:   {  
67:   if(i+1<=n-1)  
68:   if(a[i]<a[i+1])  
69:   i++;  
70:   if(item<a[i])  
71:   {  
72:    a[j]=a[i];  
73:    j=i;  
74:    i=2*j+1;  
75:   }  
76:   else  
77:   break;  
78:   }  
79:   a[j]=item;  
80:  }  

Write a C rogram To Perform Bubble Sort using Insertion Of Numbers

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<stdlib.h>  
4:  #define MAX 5  
5:  void main()  
6:  {  
7:   int i,hold,pass,a[MAX];  
8:   clrscr();  
9:   printf("Enter the numbers:\n");  
10:   for(i=0;i<=MAX-1;i++)  
11:   {  
12:   scanf("%d",&a[i]);  
13:   }  
14:   printf("\n");  
15:   printf("Array in sorted order is:\n");  
16:   for (pass=1;pass<MAX;pass++)  
17:   {  
18:   for (i=0;i<=MAX-pass;i++)  
19:   {  
20:    if (a[i]>a[i+1])   
21:    {   
22:    hold=a[i];  
23:    a[i]=a[i+1];  
24:    a[i+1]=hold;  
25:    }  
26:   }  
27:   }  
28:   for(i=0;i<=MAX-1;i++)  
29:   {  
30:   printf("%d\t",a[i]);  
31:   }  
32:   getch();  
33:  }  

Thursday 24 September 2015

Write a C Program To Find Predecessor And Successor Of An Element

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  struct tree  
4:  {  
5:  struct tree *left;  
6:  int info;  
7:  struct tree *right;  
8:  };  
9:  struct tree *root;  
10:  void insert(int element);  
11:  void display(struct tree *root,int choice);  
12:  void pd(struct tree *root,int value,int option);  
13:  void main()  
14:  {  
15:  int i,x,a,data;  
16:  char ch;  
17:  struct tree *s;  
18:  clrscr();  
19:  root=NULL;  
20:  do  
21:  {  
22:  printf("Enter your choice\n1.Insert an element\n2.Display\n");  
23:  printf("3.Find predecessor and successor in Inorder traversal\n");  
24:  scanf("%d",&x);  
25:  if(x==1)  
26:  {  
27:  printf("Enter the element\n");  
28:  scanf("%d",&i);  
29:  insert(i);  
30:  }  
31:  else if(x==2)  
32:  {  
33:  clrscr();  
34:  printf("1.Inorder notation\n2.Preorder notation\n4.Postorder notation\n");  
35:  scanf("%d",&a);  
36:  display(root,a);  
37:  }  
38:  else if(x==3)  
39:  {  
40:  clrscr();  
41:  printf("Enter the element\n");  
42:  scanf("%d",&data);  
43:  printf("1.Inorder notation\n2.Preorder notation\n3.Postorder notation\n");  
44:  scanf("%d",&a);  
45:  pd(root,data,a);  
46:  }  
47:  printf("\nDo you want to continue> y/n\n");  
48:  scanf(" %c",&ch);  
49:  clrscr();  
50:  }  
51:  while(ch=='y');  
52:  getch();  
53:  }  
54:  void insert(int element)  
55:  {  
56:  struct tree *newptr,*ptr,*parent;  
57:  newptr=(struct tree *)malloc(sizeof(struct tree));  
58:  newptr->info=element;  
59:  newptr->left=newptr->right=NULL;  
60:  if(root==NULL)  
61:  {  
62:  root=newptr;  
63:  }  
64:  else  
65:  {  
66:  parent=NULL;  
67:  ptr=root;  
68:  while(ptr!=NULL)  
69:  {  
70:  parent=ptr;  
71:  if(element<ptr->info)  
72:  {  
73:  ptr=ptr->left;  
74:  }  
75:  else  
76:  {  
77:  ptr=ptr->right;  
78:  }  
79:  }  
80:  if(element<parent->info)  
81:  {  
82:  parent->left=newptr;  
83:  }  
84:  else  
85:  {  
86:  parent->right=newptr;  
87:  }  
88:  }  
89:  }  
90:  void display(struct tree *root,int choice)  
91:  {  
92:  if(choice==1)  
93:  {  
94:  if(root!=NULL)  
95:  {  
96:  display(root->left,choice);  
97:  printf("%d\t",root->info);  
98:  display(root->right,choice);  
99:  }  
100:  }  
101:  else if(choice==2)  
102:  {  
103:  if(root!=NULL)  
104:  {  
105:  printf("%d\t",root->info);  
106:  display(root->left,choice);  
107:  display(root->right,choice);  
108:  }  
109:  }  
110:  else if(choice==3)  
111:  {  
112:  if(root!=NULL)  
113:  {  
114:  display(root->left,choice);  
115:  display(root->right,choice);  
116:  printf("%d\t",root->info);  
117:  }  
118:  }  
119:  }  
120:  void pd(struct tree *root,int data,int option)  
121:  {  
122:  int array[20];  
123:  int i=0,k=0,a=0;  
124:  if(option==1)  
125:  {  
126:  if(root!=NULL)  
127:  {  
128:  pd(root->left,data,option);  
129:  array[i]=root->info;  
130:  i++;  
131:  pd(root->right,data,option);  
132:  }  
133:  }  
134:  else if(option==2)  
135:  {  
136:  if(root!=NULL)  
137:  {  
138:  array[i]=root->info;  
139:  i++;  
140:  pd(root->left,data,option);  
141:  pd(root->right,data,option);  
142:  }  
143:  }  
144:  else if(option==3)  
145:  {  
146:  if(root!=NULL)  
147:  {  
148:  pd(root->left,data,option);  
149:  pd(root->right,data,option);  
150:  array[i]=root->info;  
151:  i++;  
152:  }  
153:  }  
154:  for(i=0;i<19;i++)  
155:  {  
156:  if(array[i]==data)  
157:  {  
158:  k=i;  
159:  }  
160:  else  
161:  i++;  
162:  }  
163:  if(k==0)  
164:  {  
165:  printf("No Predecessor :-(\n");  
166:  }  
167:  else  
168:  printf("Predecessor = %d",array[k-1]);  
169:  printf("Successor = %d",array[k+1]);  
170:  }  

Write a C Program To Traverse Binary Search Tree (INORDER , PREORDER and POSTORDER)

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  struct tree  
4:  {  
5:  struct tree *left;  
6:  int info;  
7:  struct tree *right;  
8:  };  
9:  struct tree *root;  
10:  void insert(int element);  
11:  void display(struct tree *root,int choice);  
12:  void main()  
13:  {  
14:  int i,x,a;  
15:  char ch;  
16:  clrscr();  
17:  root=NULL;  
18:  do  
19:  {  
20:  printf("Enter your choice\n1.Insert an element\n2.Display\n");  
21:  scanf("%d",&x);  
22:  if(x==1)  
23:  {  
24:  printf("Enter the element\n");  
25:  scanf("%d",&i);  
26:  insert(i);  
27:  }  
28:  else if(x==2)  
29:  {  
30:  clrscr();  
31:  printf("1.Infix notation\n2.Prefix notation\n4.Postfix notation\n");  
32:  scanf("%d",&a);  
33:  display(root,a);  
34:  }  
35:  printf("\nDo you want to continue> y/n\n");  
36:  scanf(" %c",&ch);  
37:  clrscr();  
38:  }  
39:  while(ch=='y');  
40:  getch();  
41:  }  
42:  void insert(int element)  
43:  {  
44:  struct tree *newptr,*ptr,*parent;  
45:  newptr=(struct tree *)malloc(sizeof(struct tree));  
46:  newptr->info=element;  
47:  newptr->left=newptr->right=NULL;  
48:  if(root==NULL)  
49:  {  
50:  root=newptr;  
51:  }  
52:  else  
53:  {  
54:  parent=NULL;  
55:  ptr=root;  
56:  while(ptr!=NULL)  
57:  {  
58:  parent=ptr;  
59:  if(element<ptr->info)  
60:  {  
61:  ptr=ptr->left;  
62:  }  
63:  else  
64:  {  
65:  ptr=ptr->right;  
66:  }  
67:  }  
68:  if(element<parent->info)  
69:  {  
70:  parent->left=newptr;  
71:  }  
72:  else  
73:  {  
74:  parent->right=newptr;  
75:  }  
76:  }  
77:  }  
78:  void display(struct tree *root,int choice)  
79:  {  
80:  if(choice==1)  
81:  {  
82:  if(root!=NULL)  
83:  {  
84:  display(root->left,choice);  
85:  printf("%d\t",root->info);  
86:  display(root->right,choice);  
87:  }  
88:  }  
89:  else if(choice==2)  
90:  {  
91:  if(root!=NULL)  
92:  {  
93:  printf("%d\t",root->info);  
94:  display(root->left,choice);  
95:  display(root->right,choice);  
96:  }  
97:  }  
98:  else if(choice==3)  
99:  {  
100:  if(root!=NULL)  
101:  {  
102:  display(root->left,choice);  
103:  display(root->right,choice);  
104:  printf("%d\t",root->info);  
105:  }  
106:  }  
107:  }  

Write a C Program To Traverse Binary Tree (Creation , Insertion And Traversal

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  struct tree  
4:  {  
5:  struct tree *left;  
6:  int info;  
7:  struct tree *right;  
8:  };  
9:  struct tree *root;  
10:  void insert(int element);  
11:  void display(struct tree *root,int choice);  
12:  void pd(struct tree *root,int value,int option);  
13:  void main()  
14:  {  
15:  int i,x,a,data;  
16:  char ch;  
17:  struct tree *s;  
18:  clrscr();  
19:  root=NULL;  
20:  do  
21:  {  
22:  printf("Enter your choice\n1.Insert an element\n2.Display\n");  
23:  printf("3.Find predecessor and successor in Inorder traversal\n");  
24:  scanf("%d",&x);  
25:  if(x==1)  
26:  {  
27:  printf("Enter the element\n");  
28:  scanf("%d",&i);  
29:  insert(i);  
30:  }  
31:  else if(x==2)  
32:  {  
33:  clrscr();  
34:  printf("1.Inorder notation\n2.Preorder notation\n4.Postorder notation\n");  
35:  scanf("%d",&a);  
36:  display(root,a);  
37:  }  
38:  else if(x==3)  
39:  {  
40:  clrscr();  
41:  printf("Enter the element\n");  
42:  scanf("%d",&data);  
43:  printf("1.Inorder notation\n2.Preorder notation\n3.Postorder notation\n");  
44:  scanf("%d",&a);  
45:  pd(root,data,a);  
46:  }  
47:  printf("\nDo you want to continue> y/n\n");  
48:  scanf(" %c",&ch);  
49:  clrscr();  
50:  }  
51:  while(ch=='y');  
52:  getch();  
53:  }  
54:  void insert(int element)  
55:  {  
56:  struct tree *newptr,*ptr,*parent;  
57:  newptr=(struct tree *)malloc(sizeof(struct tree));  
58:  newptr->info=element;  
59:  newptr->left=newptr->right=NULL;  
60:  if(root==NULL)  
61:  {  
62:  root=newptr;  
63:  }  
64:  else  
65:  {  
66:  parent=NULL;  
67:  ptr=root;  
68:  while(ptr!=NULL)  
69:  {  
70:  parent=ptr;  
71:  if(element<ptr->info)  
72:  {  
73:  ptr=ptr->left;  
74:  }  
75:  else  
76:  {  
77:  ptr=ptr->right;  
78:  }  
79:  }  
80:  if(element<parent->info)  
81:  {  
82:  parent->left=newptr;  
83:  }  
84:  else  
85:  {  
86:  parent->right=newptr;  
87:  }  
88:  }  
89:  }  
90:  void display(struct tree *root,int choice)  
91:  {  
92:  if(choice==1)  
93:  {  
94:  if(root!=NULL)  
95:  {  
96:  display(root->left,choice);  
97:  printf("%d\t",root->info);  
98:  display(root->right,choice);  
99:  }  
100:  }  
101:  else if(choice==2)  
102:  {  
103:  if(root!=NULL)  
104:  {  
105:  printf("%d\t",root->info);  
106:  display(root->left,choice);  
107:  display(root->right,choice);  
108:  }  
109:  }  
110:  else if(choice==3)  
111:  {  
112:  if(root!=NULL)  
113:  {  
114:  display(root->left,choice);  
115:  display(root->right,choice);  
116:  printf("%d\t",root->info);  
117:  }  
118:  }  
119:  }  
120:  void pd(struct tree *root,int data,int option)  
121:  {  
122:  int array[20];  
123:  int i=0,k=0,a=0;  
124:  if(option==1)  
125:  {  
126:  if(root!=NULL)  
127:  {  
128:  pd(root->left,data,option);  
129:  array[i]=root->info;  
130:  i++;  
131:  pd(root->right,data,option);  
132:  }  
133:  }  
134:  else if(option==2)  
135:  {  
136:  if(root!=NULL)  
137:  {  
138:  array[i]=root->info;  
139:  i++;  
140:  pd(root->left,data,option);  
141:  pd(root->right,data,option);  
142:  }  
143:  }  
144:  else if(option==3)  
145:  {  
146:  if(root!=NULL)  
147:  {  
148:  pd(root->left,data,option);  
149:  pd(root->right,data,option);  
150:  array[i]=root->info;  
151:  i++;  
152:  }  
153:  }  
154:  for(i=0;i<19;i++)  
155:  {  
156:  if(array[i]==data)  
157:  {  
158:  k=i;  
159:  }  
160:  else  
161:  i++;  
162:  }  
163:  if(k==0)  
164:  {  
165:  printf("No Predecessor :-(\n");  
166:  }  
167:  else  
168:  printf("Predecessor = %d",array[k-1]);  
169:  printf("Successor = %d",array[k+1]);  
170:  }  

Write a C Program to Evaluate Postfix Expression using Stack

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<string.h>  
4:  #define MAX 50  
5:  int stack[MAX];  
6:  char post[MAX];  
7:  int top=-1;  
8:  void pushstack(int tmp);  
9:  void calculator(char c);  
10:  void main()  
11:  {  
12:   int i;  
13:   clrscr();  
14:   printf("Insert a postfix notation :: ");  
15:   gets(post);  
16:   for(i=0;i<strlen(post);i++)  
17:   {  
18:   if(post[i]>='0' && post[i]<='9')  
19:   {  
20:    pushstack(i);  
21:   }  
22:   if(post[i]=='+' || post[i]=='-' || post[i]=='*' || post[i]=='/' || post[i]=='^')  
23:   {  
24:    calculator(post[i]);  
25:   }  
26:   }  
27:   printf("\n\nResult :: %d",stack[top]);  
28:   getch();  
29:  }  
30:  void pushstack(int tmp)  
31:  {  
32:   top++;  
33:   stack[top]=(int)(post[tmp]-48);  
34:  }  
35:  void calculator(char c)  
36:  {  
37:   int a,b,ans;  
38:   a=stack[top];  
39:   stack[top]='\0';  
40:   top--;  
41:   b=stack[top];  
42:   stack[top]='\0';  
43:   top--;  
44:   switch(c)  
45:   {  
46:   case '+':  
47:   ans=b+a;  
48:   break;  
49:   case '-':  
50:   ans=b-a;  
51:   break;  
52:   case '*':  
53:   ans=b*a;  
54:   break;  
55:   case '/':  
56:   ans=b/a;  
57:   break;  
58:   case '^':  
59:   ans=b^a;  
60:   break;  
61:   default:  
62:   ans=0;  
63:   }  
64:   top++;  
65:   stack[top]=ans;  
66:  }  

Write a C Program To Convert Infix to Postfix Using Stack

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #define SIZE 100  
4:  int top = -1;  
5:  char stack[SIZE];  
6:  void push(char item);  
7:  char pop();  
8:  int is_operator(char symbol);  
9:  int precedence(char symbol);  
10:  void main()  
11:  {  
12:   int i, j;  
13:   char infix_exp[SIZE], postfix_exp[SIZE];  
14:   char item, x;  
15:   clrscr();  
16:   printf("\nEnter the arithmetic expression in Infix notation enclosed in parentheses: \n");  
17:   gets(infix_exp);  
18:   i=0;  
19:   j=0;  
20:   item=infix_exp[i++];  
21:   while(item != '\0')  
22:   {  
23:   if(item == '(')  
24:   {  
25:    push(item);  
26:   }  
27:   else if((item >= 'A' && item <= 'Z') || (item >= 'a' && item <= 'z'))  
28:   {  
29:    postfix_exp[j++] = item;  
30:   }  
31:   else if(is_operator(item) == 1)  
32:   {  
33:    x=pop();  
34:    while(is_operator(x) == 1 && precedence(x) >= precedence(item))  
35:    {  
36:    postfix_exp[j++] = x;  
37:    x = pop();  
38:    }  
39:    push(x);  
40:    push(item);    
41:   }  
42:   else if(item == ')')  
43:   {  
44:    x = pop();  
45:    while(x != '(')  
46:    {  
47:    postfix_exp[j++] = x;  
48:    x = pop();  
49:    }  
50:   }  
51:   else  
52:   {  
53:    printf("\nInvalid Arithmetic Expression.\n");  
54:    getch();  
55:    exit(0);  
56:   }  
57:   item = infix_exp[i++];  
58:   }  
59:   postfix_exp[j++] = '\0';  
60:   printf("\nArithmetic expression in Postfix notation: ");  
61:   puts(postfix_exp);  
62:   getch();  
63:  }  
64:  void push(char item)  
65:  {  
66:   if(top >= SIZE-1)  
67:   {  
68:   printf("\nStack Overflow. Push not possible.\n");  
69:   }  
70:   else  
71:   {  
72:   top = top+1;  
73:   stack[top] = item;  
74:   }  
75:  }  
76:  char pop()  
77:  {  
78:   char item = NULL;  
79:   if(top <= -1)  
80:   {  
81:   printf("\nStack Underflow. Pop not possible.\n");  
82:   }  
83:   else  
84:   {  
85:   item = stack[top];  
86:   stack[top] = NULL;  
87:   top = top-1;  
88:   }  
89:   return(item);  
90:  }  
91:  int is_operator(char symbol)  
92:  {  
93:   if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol == '-')  
94:   {  
95:   return 1;  
96:   }  
97:   else  
98:   {  
99:   return 0;  
100:   }  
101:  }  
102:  int precedence(char symbol)  
103:  {  
104:   if(symbol == '^')  
105:   {  
106:   return(3);  
107:   }  
108:   else if(symbol == '*' || symbol == '/')  
109:   {  
110:   return(2);  
111:   }  
112:   else if(symbol == '+' || symbol == '-')  
113:   {  
114:   return(1);  
115:   }  
116:   else  
117:   {  
118:   return(0);  
119:   }  
120:  }  

Tuesday 22 September 2015

Write a C program to read data from keyboard, write it to a file named STUDENT again read the same data from STUDENT file and write it into DATA file. Same data should be displayed on the screen.

Write a C program to read data from keyboard, write it to a file named STUDENT again read the same data from STUDENT file and write it into DATA file. Same data should be displayed on the screen.
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<process.h>  
4:  struct stud  
5:  {  
6:        int rno;  
7:        char *nm;  
8:  };  
9:  void main()  
10:  {  
11:        struct stud *s;  
12:        int n,i;  
13:        FILE *fp,*fp1;  
14:        clrscr();  
15:        printf("Enter how many record you want to input : ");  
16:        scanf("%d",&n);  
17:        s=(struct stud *)malloc(n*sizeof(struct stud));  
18:        fp=fopen("STUD.txt","w");  
19:        for(i=0;i<n;i++)  
20:        {  
21:              printf("\n\tInformation for student : %d\n",i+1);  
22:              printf("Enter Roll No : ");  
23:              scanf("%d",&s[i].rno);  
24:              printf("Enter Name : ");  
25:              fflush(stdin);  
26:              gets(s[i].nm);  
27:              fprintf(fp,"%5d %-20s\n",s[i].rno,s[i].nm);  
28:        }  
29:        fclose(fp);  
30:        fp=fopen("STUD.txt","r");  
31:        fp1=fopen("DATA.txt","w");  
32:        printf("\nContent of the STUD.txt file is\n");  
33:        printf("Roll No  Name\n");  
34:        printf("---------------------------\n");  
35:        while(!feof(fp))  
36:        {  
37:              fscanf(fp,"%5d %20s\n",&s[i].rno,s[i].nm);  
38:              fprintf(fp1,"%5d %-20s\n",s[i].rno,s[i].nm);  
39:              printf("%7d  %-20s\n",s[i].rno,s[i].nm);    }  
40:        fcloseall();  
41:        getch();  
42:  }  
43:  Output:  
44:  Enter how many record you want to input : 3  
45:        Information for student : 1  
46:  Enter Roll No : 1  
47:  Enter Name : Kartik  
48:        Information for student : 2  
49:  Enter Roll No : 2  
50:  Enter Name : Umesh  
51:        Information for student : 3  
52:  Enter Roll No : 3  
53:  Enter Name : Mehul  
54:  Content of the STUD.txt file is  
55:  Roll No  Name  
56:  ---------------------------  
57:     1  Kartik  
58:     2  Umesh  
59:     3  Mehul  

Write a C program to create linear linked list interactively and print out the list and total number of items in the list.

Write a C program to create linear linked list interactively and print out the list and total number of items in the list.
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<process.h>  
4:  #include<stdlib.h>  
5:  struct node  
6:  {  
7:        int info;  
8:        struct node *next;  
9:  }*head,*last,*temp,*n;  
10:  void menu();  // to select the choise  
11:  void create(); // insert at last  
12:  void display(); // to print the link list  
13:  int i;  
14:  void main()  
15:  {  
16:        i=0;  
17:        clrscr();  
18:        menu();  
19:        getch();  
20:  }  
21:  void menu()  
22:  {  
23:        int ch;  
24:        printf("1. Create\n");  
25:        printf("2. Display\n");  
26:        printf("3. Exit\n");  
27:        printf("Enter choise : ");  
28:        scanf("%d",&ch);  
29:        switch(ch)  
30:        {  
31:              case 1 : create();  
32:              case 2 : display();  
33:              case 3 : printf("You have choose to exit\n");  
34:                     getch();  exit(0);  
35:              default : printf("Invalid choise\n"); menu();  
36:        }  
37:  }  
38:  void create()  
39:  {  
40:        n= new node;  
41:        printf("Enter the information : ");  
42:        scanf("%d",&n->info);  
43:        if(i==0)  
44:        {  
45:              head=n;  
46:              i++;  
47:        }  
48:        else  
49:        {  
50:              last->next=n;  
51:              i++;  
52:        }  
53:        last=n;  
54:        last->next=NULL;  
55:        menu();  
56:  }  
57:  void display()  
58:  {  
59:        if(head==NULL)  
60:        {  
61:              printf("No information in list\n");  
62:              menu();  
63:        }  
64:        temp=head;  
65:        while(temp!=NULL)  
66:        {  
67:              printf("%d->",temp->info);  
68:              temp=temp->next;  
69:        }  
70:        printf("\n");  
71:        printf("Total number of elements in the link list are : %d\n",i);  
72:        menu();  
73:  }  
74:  Output:  
75:  1. Create  
76:  2. Display  
77:  3. Exit  
78:  Enter choise : 1  
79:  Enter the information : 12  
80:  1. Create  
81:  2. Display  
82:  3. Exit  
83:  Enter choise : 1  
84:  Enter the information : 23  
85:  1. Create  
86:  2. Display  
87:  3. Exit  
88:  Enter choise : 1  
89:  Enter the information : 45  
90:  1. Create  
91:  2. Display  
92:  3. Exit  
93:  Enter choise : 2  
94:  12->23->45->  
95:  Total number of elements in the link list are : 3  
96:  1. Create  
97:  2. Display  
98:  3. Exit  
99:  Enter choise : 3  
100:  You have choose to exit