C Programs

Binary Search

A binarysearch program locates the position of an item in a sorted array. Logic: The Binary search starts by testing the element at the middle of the array. Let us consider an array ‘a’ with all elements arranged in ascending order. Let low and high are the lower and upper indices of the array ‘a’, respectively. We want to search an item […]


C program to implement tower of Hanoi problem

To implement tower of Hanoi problem. #include<stdio.h> void hanoi_tower(char,char,char,int); void hanoi_tower(char peg1,char peg2,char pege3,int n) { if(n<=0) printf("\n Illegal Entry"); if(n==1) printf ("\n Move disk from %c to %c", pege1,pege3); else { hanoi_tower(peg1,peg3,peg2,n-1); hanoi_tower(peg1,peg2,peg3,1); hanoi_tower(peg2,peg1,peg3,n-1); } } Void main () { int n; printf("\n Input the number of dise:); scanf("%d", &n); printf("\n Tower of Hanoi […]


C program for sorting a Linked List

C program for sorting a Linked List. #include<stdio.h> #include<dirent.h> #include<malloc.h> #include<string.h>   void main (int argc,char * argv[]) { DIR * directory_pointer; struct dirent* entry; struct File list { char filename[64]; struct File * new; } start,*node,* previous, * new; if((directory_pointer=opendir(argv[1]))==NULL) printf("Error opening %s\n", argv[1]); else { start.next + NULL; while (entry = readdir (directory_pointer)) […]


C program to Implement Morse code to text conversion and vice-versa.

C program to Implement Morse code to text conversion and vice-versa. #include<stdio.h> #include<conio.h> #include<string.h> #include<process.h>   void main() { char str[25],str1[100]; clrscr(); fflush(stdin); printf("enter the String"); gets(str);   int j=0; for(int i=0;i<=strlen(str);++) { switch(toupper(str[i])) { case ‘A’: str1[j++]=’.’; str1[j]=’.’; break;   case ‘b’: str1[j++]=’.’; str1[j++]=’.’; str1[j++]=’.’; str1[j]=’.’; break;   case ‘c’: str1[j++]=’.’; str1[j++]=’.’; str1[j++]=’.’; str1[j]=’.’; […]


C program to calculate the GCD of a number

Write a program to calculate the Greatest Common Divisor(GCD) of a number #include<stdio.h> #include<conio.h> #include<process.h> int gcd(int m,int n) { int rem; while(n!=0) { rem=m%n; m=n; n=rem; } return(m); } main() { int num1,num2,num3,gcd1,gcd2; clrscr(); printf("Enter three positive integers"); scanf("%d%d%d",&num1,&num2,&num3); if(num1==0 && num2==0 && num3==0) { printf("\n Invalid number"); exit(0); } gcd1=gcd(num1,num2); gcd2=gcd(num3,gcd1); printf("\n GCD […]


C program for Syntax Analyzer

C program for Syntax Analyzer #include<stdio.h> #include<conio.h> #include<string.h> #include<ctype.h> void main() { int i,j,k=0,count,inc=0,n; char name[30],open[30],ch,chh,o[30]; char op[20]={’=’,’+’,’-‘,’*’,’/’,’%’,’^’,’&’,’|’}; clrscr(); textcolor(3); cprintf("–Syntax Analyser–"); printf("\n"); printf("\n Enter Syntax"); printf("\n"); scanf("%s",name); n=strlen(name); for(i=0;i<n;i++) { ch=tolower(name[i]); for(j=0;j<9;j++) { if(ch==op[j]) { open[k]=i; o[k]=ch; k++; } } } for(i=0;i<k;i++) { count=open[i]; ch=tolower(name[count-1]); chh=tolower(name[count+1]); if(isalpha(ch)&&isalpha(chh)||isdigit(chh)) ++inc; } if(k==inc) printf("\n %s is a […]


C program for Shortest Job Next

C program for Shortest Job Next #include<stdio.h> #include<conio.h> #include<math.h> void main() { int s,t[20],temp,n,x[20],w[20],i,j,c,d,b[20]; float avgw,avgt; clrscr(); printf("\n Enter no.of job:"); scanf("%d",&n); printf("enter the job:"); for(i=1;i<=n;i++) { scanf("%d",&b[i]); x[i]=i; } for(i=1;i<=n-1;i++) { for(j=i+1;j<=n;j++) { if(b[i]>b[j]) { temp=b[i]; b[i]=b[j]; b[j]=temp; s=x[i]; x[i]=x[j]; x[j]=s; } } } c=0; d=0; for(i=1;i<=n;i++) { w[i]=w[i-1]+b[i-1]; t[i]=b[i]+t[i-1]; w[1]=0; t[1]=b[1]; c+=w[i]; d+=t[i]; […]


C program for Round Robin

C program for Round Robin   #include<stdio.h> #include<conio.h> #include<iostream.h> int t,n,s,bt[10],ct[10],ta[10],w[10],lat[10],wav,taav; int allover() { for(int i=0;i<n;i++) if(ct[i]>0) return 0; return 1; } void select(int p) { w[p]+=t-lat[p]; if(ct[p]>=s) { ct[p]-=s; t+=s; } else { t+=ct[p]; ct[p]=0; } if(ct[p]==0) ta[p]=t; lat[p]=t; } void main() { int p=0;t=0;taav=0;wav=0; clrscr(); printf("Enter the number of process : "); scanf("%d",&n); […]


C program for Priority Scheduling

C program for Priority Scheduling #include<stdio.h> #include<conio.h> #include<iostream.h> void main() { clrscr(); int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i; printf("Enter the number of process : "); scanf("%d",&n); printf("\n Enter process : time priorities \n"); for(i=0;i<n;i++) { printf("\nProcess no %d : ",i+1); scanf("%d %d",&pt[i],&pp[i]); p[i]=i+1; } for(i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) { if(pp[i]<pp[j]) { x=pp[i]; pp[i]=pp[j]; pp[j]=x; x=pt[i]; pt[i]=pt[j]; pt[j]=x; x=p[i]; p[i]=p[j]; […]