Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Programs

381 views

Published on

lab programs

Published in: Business, Technology
  • Be the first to comment

Programs

  1. 1. cn and ct lab programs k.murali gudlavalleru engineering college1.bit stuffingProgram:#include<stdio.h>#include<conio.h>int main(){ int a[50],b[50],n,i,j,count,k; printf("enter the size of the bits"); scanf("%d",&n); printf("enter the bits to stuff"); for(i=0;i<n;i++) scanf("%d",&a[i]); i=0;j=0; while(i<n) { if(a[i]==1) { b[j]=a[i]; count=1; for(k=i+1;k<n&&count<5&&a[k]==1;k++) { j++; b[j]=a[i]; count++; if(count==5) {
  2. 2. j++; b[j]=0; } i=k; } } else b[j]=a[i]; i++; j++; }printf("after stuffing");printf("01111110 ");for(i=0;i<j;i++)printf("%d",b[i]);printf(" 01111110");return 0;}2)character stuffing using dleProgram:#include<stdio.h>#include<conio.h>#include<string.h>int main(){ char msg[50],m[40]; int i,j,k,n,te; printf("enter the string"); scanf("%s",&msg); n=strlen(msg); printf("msg length %d n",n);
  3. 3. for(i=0;i<n;i++) { if(msg[i]==d&&msg[i+1]==l&&msg[i+2]==e) { for(k=0;k<i;k++) m[k]=msg[k]; m[k++]=d; m[k++]=l; m[k++]=e; for(te=i;te<n;te++) m[k++]=msg[te]; strcpy(msg,m); n=n+3; i=i+5; } } msg[i]=0; printf("n dlestx %s dleetx n",msg); return 0;}3)crc encoding:Program:#include<stdio.h>#include<conio.h>#include<string.h>int main(){ static int p[30],g[30],i,j,k; char cp[50],cg[50]; printf("enter the frame string"); gets(cp); printf("enter the gerearator"); gets(cg); for(i=0;i<strlen(cg)-1;i++)
  4. 4. strcat(cp,"0"); for(i=0;i<strlen(cp);i++) p[i]=cp[i]-0; for(i=0;i<strlen(cg);i++) g[i]=cg[i]-0; printf("n"); i=0; while(1) { j=0; while(p[j]!=1) j++; if(j>=strlen(cp)-(strlen(cg)-1)) break; k=0; for(i=j;i<j+strlen(cg);i++,k++) p[i]=p[i]^g[k]; } printf("data transmitted is: "); for(i=strlen(cp)-1;i>=strlen(cp)-(strlen(cg)-1);i--) cp[i]=p[i]+0; printf("%s",cp); return 0;}4) crc decoding:Program:#include<stdio.h>#include<conio.h>#include<string.h>int main(){ static int p[30],g[30],i,j,k;
  5. 5. char cp[50],cg[50]; printf("enter the frame string"); gets(cp); printf("enter the gerearator"); gets(cg); for(i=0;i<strlen(cg)-1;i++) strcat(cp,"0"); for(i=0;i<strlen(cp);i++) p[i]=cp[i]-0;for(i=0;i<strlen(cg);i++)g[i]=cg[i]-0;printf("n");i=0;while(1){ j=0; while(p[j]!=1) j++; if(j>=strlen(cp)-(strlen(cg)-1)) break; k=0; for(i=j;i<j+strlen(cg);i++,k++) p[i]=p[i]^g[k];}i=0;for(i=0;i<strlen(cp);i++)if(p[i]!=0)break;if(i==strlen(cp)){ printf("n no error n data received is: "); for(i=0;i<strlen(cp)-(strlen(cg)-1);i++) printf("%d",cp[i]-0);
  6. 6. } else printf("error has occured "); return 0;}5)RSA key generation:Program:#include<stdio.h>int prime(int n);int gcd(int a,int b);int main(){ int p,q,j,i,n,eq,e,d,m1,m2; int c,c1; printf("enter two prime numbers"); scanf("%d%d",&p,&q); if(prime(p)||prime(q)) { printf("numbers are not prime"); } else { n=p*q; eq=(p-1)*(q-1); printf("euler values are"); for(i=2;i<eq;i++) if(gcd(eq,i)==1) printf("%d,",i); printf("choose e value"); scanf("%d",&e); for(i=0;i<eq;i++) { d=((i*eq)+1)%e;
  7. 7. if(d==0) { d=((i*eq)+1)/e; break; } } printf("d is :%d",d); printf("public key is {%d,%d}",e,n); printf("public key is {%d,%d}",d,n); } return 0;}int gcd(int a,int b){ if(b!=0) return gcd(b,a%b); else return a;}int prime(int n){ int i,x=0; for(i=2;i<n;i++) { if(n%i==0) x++; } if(x==0) return 0; else return 1;}6)RSA encryption:#include<stdio.h>
  8. 8. #include<math.h>int prime(int n);int gcd(int a,int b);int main(){ int p,q,j,i,n,eq,e,d,m1,m2; int c,c1; printf("enter two prime numbers"); scanf("%d%d",&p,&q); if(prime(p)||prime(q)) { printf("numbers are not prime"); } else { n=p*q; eq=(p-1)*(q-1); printf("euler values are"); for(i=2;i<eq;i++) if(gcd(eq,i)==1) printf("%d,",i); printf("choose e value"); scanf("%d",&e); for(i=0;i<eq;i++) { d=((i*eq)+1)%e; if(d==0) { d=((i*eq)+1)/e; break; } } printf("d is :%d",d); printf("public key is {%d,%d}",e,n);
  9. 9. printf("public key is {%d,%d}",d,n); printf("enter the plain text"); scanf("%d",&m1); c=fmod((pow(m1,e)),n); printf("cipher text is %d",c); } return 0;}int gcd(int a,int b){ if(b!=0) return gcd(b,a%b); else return a;}int prime(int n){ int i,x=0; for(i=2;i<n;i++) { if(n%i==0) x++; } if(x==0) return 0; else return 1;}7)hirerchical:Porgram:#include<stdio.h>#include<string.h>struct full{char line[10],dest[10];int hops;
  10. 10. }f[20];int main(){ int nv,min,minver,i,no; char sv[2],temp; printf("enter no of vertices"); scanf("%d",&no); printf("enter source vertex"); scanf("%s",sv); printf("Enter full table for source vertex %s",sv); for(i=0;i<no;i++) scanf("%s%s%d",f[i].dest,f[i].line,&f[i].hops); printf("n HIERARCHICAL TABLE n"); for(i=0;i<no;) { if(sv[0]==f[i].dest[0]) { printf("n%s%s%d",f[i].dest,f[i].line,f[i].hops); i++; } else { temp=f[i].dest[0]; min=1000; minver=0; while(temp==f[i].dest[0]) { if(min>f[i].hops) { min=f[i].hops; minver=i; }
  11. 11. i++; } printf("n %c%s%d",temp,f[minver].line,f[minver].hops); } } return 0;}8)Distance Vector routing algorithmProgram:#include<stdio.h>#include<string.h>void main(){int mat[10][10];int n=8,sour,dest,small=1000,vnod[10]={1};int v=0,count=0,visit=0,i,j,k;printf("enter no of nodes");scanf("%d",&n);printf("enter the distances");for(i=0;i<n;i++){for(j=0;j<n;j++){scanf("%d",&mat[i][j]);}}printf("enterc distance");for(i=0;i<n;i++){for(j=0;j<n;j++){printf("enter source mode");scanf("%d",&sour);vnod[v++]=sour;
  12. 12. printf("enter distance node");scanf("%d",&dest);do{count++;visit=0;small=1000;for(j=0;j<n;j++,visit=0)if(mat[sour][j]<small&&mat[sour][j]!=0){for(k=0;k<v-1;k++)if(j==vnod[n])visit=1;if(visit!=1){small=mat[sour][j];sour=j;vnod[v++]=sour;if(sour==dest)break;}}}while(sour==dest||count==n);printf("path is n");for(i=0;i<v;i++)printf("%c",vnod[1]+65);}}}9) shortest distance vector routing:Program:#include<stdio.h>
  13. 13. #include<conio.h>#include<stdlib.h>void main(){int i,j,k,nv,sv,noadj,edel[20],tdel[20][20],min;char sn,adver[20],ch;clrscr();printf("enter no.of vertices");scanf("%d",&nv);printf("enter the source vertex no. and name");scanf("%d %c",&sv,&sn);printf("enter no.og adjacent vertices to vertex node %c ",sn);scanf("%d",&noadj);for(i=0;i<noadj;i++){ printf("enter time delay adjacent node %d and node name of adjcent node %d",i+1,i+1); scanf("%d %c",&edel[i],&adver[i]);}for(i=0;i<noadj;i++){ printf("enter the line dealy from %c to all nodes",adver[i]); for(j=0;j<nv;j++) { scanf("%d",&tdel[i][j]); }}printf("n destination adjacent node shortestdelay");for(i=0;i<nv;i++){ min=1000; ch=0; for(j=0;j<noadj;j++) if(min>tdel[j][i]+edel[j]) {
  14. 14. min=tdel[i][j]+edel[j]; ch=adver[j];}if(i!=sv-1)printf("n %d t %c t %d",i+1,ch,min);elseprintf("n %d t 0 t",i+1);}}

×