Dcn code and output
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Dcn code and output

on

  • 283 views

 

Statistics

Views

Total Views
283
Views on SlideShare
283
Embed Views
0

Actions

Likes
0
Downloads
5
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Dcn code and output Document Transcript

  • 1. -1- VERTICAL REDUNDANCY CHECK#include<stdio.h>#include<conio.h>#include<stdlib.h>void receiver();void sender();int i,j,data[20];void main(){ int ans; clrscr(); printf("nn*********************** VERTICAL REDUNDANCY CHECK ***********************nn"); while(1) { printf("n--------------------------------------------------------------------------n "); printf("ttttMAIN MENU"); printf("n--------------------------------------------------------------------------n n"); printf("Choice No. Choice Name.nn"); printf(" 1. Senders Sidenn 2. Receivers Sidenn 3. ExitnnnEnter Your Choice : "); scanf("%d",&ans); switch(ans) { case 1: printf("n----------------------------------------------------------- -------------n"); printf("ttt Senders Side"); printf("n----------------------------------------------------------- --------------n"); sender(); break; case 2: printf("n----------------------------------------------------------- --------------n"); printf("ttt Receivers Side"); printf("n----------------------------------------------------------- --------------n"); receiver(); break; case 3: exit(1); default: 1
  • 2. printf("nnINVALID ENTRY!!!"); } }}void receiver(){ int sum=0; printf("nEnter the number of bits you want (Max 20) : "); scanf("%d",&j); printf("nnEnter %d bits : ",j); for(i=0;i<j;i++) { printf("nn %d bit: ",i+1); scanf("%d",&data[i]); if((data[i]!=0) && (data[i]!=1)) { printf("nInvalid Bit......Enter only (1s and 0s) "); i--; } } for(i=0;i<j;i++) { sum=sum+data[i]; } printf("n-------------------------------------------------------------------------n"); printf("ttttOutput"); printf("n-------------------------------------------------------------------------"); printf("nnThe Number of 1s in the message is: %d",sum); if(sum%2==0) { printf("nnThere is no ERROR in the message."); } else { printf("nnThere is ERROR in the message."); }}void sender(){ int sum=0; printf("nEnter the number of bits you want (Max 20) : "); scanf("%d",&j); printf("nEnter %d bits : ",j); for(i=0;i<j;i++) 2
  • 3. { printf("nn %d bit: ",i+1); scanf("%d",&data[i]); if((data[i]!=0) && (data[i]!=1)) { printf("nInvalid bit......Enter only (1s and 0s) "); i--; } } for(i=0;i<j;i++) { sum=sum+data[i]; } printf("n-------------------------------------------------------------------------n"); printf("ttttOutput"); printf("n-------------------------------------------------------------------------"); printf("nnThe Message sent was : "); for(i=0;i<j;i++) { printf("%d",data[i]); } if(sum%2!=0) { printf("nnParity Bit is : 1"); data[j]=1; printf("nnThe Message after appending parity bit was : "); for(i=0;i<=j;i++) { printf("%d",data[i]); } } else { printf("nnParity Bit is : 0"); data[j]=0; printf("nnThe Message after appending parity bit was : "); for(i=0;i<=j;i++) { printf("%d",data[i]); } }} 3
  • 4. OUTPUT:-****************** VERTICAL REDUNDANCY CHECK *******************------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Side 2. Receivers Side 3. ExitEnter Your Choice : 1------------------------------------------------------------------------------------------------------------ Senders Side------------------------------------------------------------------------------------------------------------Enter the number of bits you want (Max 20) : 5Enter 5 bits :1 bit: 12 bit: 03 bit: 14 bit: 05 bit: 1------------------------------------------------------------------------------------------------------------ 4
  • 5. Output------------------------------------------------------------------------------------------------------------The Message sent was : 10101Parity Bit is : 1The Message after appending parity bit was : 101011------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Side 2. Receivers Side 3. ExitEnter Your Choice : 2------------------------------------------------------------------------------------------------------------ Receivers Side------------------------------------------------------------------------------------------------------------Enter the number of bits you want (Max 20) : 4Enter 4 bits :1 bit: 12 bit: 03 bit: 2Invalid Bit......Enter only (1s and 0s) 5
  • 6. 3 bit: 04 bit: 1------------------------------------------------------------------------------------------------------------ Output------------------------------------------------------------------------------------------------------------The Number of 1s in the message is: 2There is no ERROR in the message.------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Side 2. Receivers Side 3. ExitEnter Your Choice : 3 6
  • 7. -2- LONGITUDINAL REDUNDANCY CHECK#include<stdio.h>#include<stdlib.h>#include<conio.h>int a[10][10];int count,j,i,n,m;void sender();void receiver();void main(){ int ans; clrscr(); printf("nn*********************** LONGITUDINAL REDUNDANCY CHECK ***********************nn"); while(1) { printf("n--------------------------------------------------------------------------n "); printf("ttttMAIN MENU"); printf("n--------------------------------------------------------------------------n n"); printf("Choice No. Choice Name.nn"); printf(" 1. Senders Sitenn 2. Receivers Sitenn 3. ExitnnnEnter Your Choice : "); scanf("%d",&ans); switch(ans) { case 1: printf("n----------------------------------------------------------- --------------n"); 7
  • 8. printf("ttt Senders Site"); printf("n----------------------------------------------------------- --------------n"); sender(); break; case 2: printf("n----------------------------------------------------------- --------------n"); printf("ttt Receivers Site"); printf("n----------------------------------------------------------- --------------n"); receiver(); break; case 3: exit(1); default: printf("nnINVALID ENTRY!!!"); } }}void sender(){ printf("Enter the number of rows and columns max(5) : "); scanf("%d%d",&m,&n); printf("nEnter %d rows and %d columns : n",m,n); for(i=1;i<=m;i++) { printf("nEnter %d row %d bits : ",i,n); for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); if((a[i][j]!=0)&&(a[i][j]!=1)) { printf("nnInvalid Bit......Enter another bit(Only 1s and 0s)!!!"); j--; } } } for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(a[j][i]==1) 8
  • 9. { count++; } } if(count%2==0) { a[j][i]=0; count=0; } else { a[j][i]=1; count=0; } } printf("n--------------------------------------------- Output -----------------------------------n"); printf("nThe parity row is : n"); i=m+1; for(j=1;j<=n;j++) { printf("%dt",a[i][j]); } printf("nnThe Message after appending : n"); for(i=1;i<=m+1;i++) { for(j=1;j<=n;j++) { printf("%dt",a[i][j]); } printf("nn"); }}void receiver(){ int count1=0; printf("Enter the number of rows and columns max(5) : "); scanf("%d%d",&m,&n); printf("nEnter %d rows and %d columns : ",m,n); for(i=1;i<=m;i++) { printf("nnEnter %d row %d bits : ",i,n); for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); 9
  • 10. if((a[i][j]!=0)&&(a[i][j]!=1)) { printf("nnInvalid Bit......Enter another bit(Only 1s and 0s)!!!"); j--; } } } printf("n-------------------------------------------- Output ----------------------------------------n"); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(a[j][i]==1) { count++; } } if(count%2==0) { count1++; count=0; } else { count=0; } } if(count1==n) printf("nData is correctly received...n"); else printf("nData is not correctly received...n");} 10
  • 11. OUTPUT:-***************** LONGITUDINAL REDUNDANCY CHECK ***************------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 1------------------------------------------------------------------------------------------------------------ Senders Site------------------------------------------------------------------------------------------------------------Enter the number of rows and columns max(5) : 2 3Enter 2 rows and 3 columns :Enter 1 row 3 bits : 1 0 0Enter 2 row 3 bits : 1 0 1 11
  • 12. ----------------------------------------------- Output --------------------------------------------------The parity row is :0 0 1The Message after appending :1 0 01 0 10 0 1------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 2------------------------------------------------------------------------------------------------------------ Receivers Site------------------------------------------------------------------------------------------------------------Enter the number of rows and columns max(5) : 2 3Enter 2 rows and 3 columns :Enter 1 row 3 bits : 1 1 0 12
  • 13. Enter 2 row 3 bits : 1 0 1------------------------------------------------ Output -------------------------------------------------Data is not correctly received...------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 3 13
  • 14. -3- CYCLIC REDUNDANCY CHECK#include<stdlib.h>#include<conio.h>#include<stdio.h>void receiver();void sender();int i,j,n,g,a1,a[20]={0},d[20]={0},b[20],p[20],s,cnt;void main(){ int choice; clrscr(); printf("*********************** CYCLIC REDUNCDANCY CHECK **********************n"); while(1) { printf("nn-------------------------------------------------------------------------- n"); 14
  • 15. printf("ttttMAIN MENU"); printf("n--------------------------------------------------------------------------n n"); printf("Choice No. Choice Name.nn"); printf(" 1. Senders Sitenn 2. Receivers Sitenn 3. ExitnnnEnter Your Choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("n--------------------------- Senders Site ----------------------------"); sender(); break; case 2: printf("n------------------------- Receivers Site ---------------"); receiver(); break; case 3: exit(1); } }}void sender(){ printf("nnEnter total number of data bits : "); scanf("%d",&n); printf("nEnter %d data bits : nn",n); for(i=0;i<n;i++) { printf("nEnter bit %d: ",i+1); scanf("%d",&a[i]); if((a[i]!=1)&& (a[i]!=0)) { printf("nInvalid Entry...Enter only 1s & 0s!!!n"); i--; } } printf("nEnter size of Key (! >databits) : "); scanf("%d",&g); 15
  • 16. if(g>n){ printf("nInvalid Entry...Enter value less than databit!!!n"); printf("nEnter size of Key (! >databits): "); scanf("%d",&g);}do{ printf("nEnter Key values : nn"); for(j=0;j<g;j++) { printf("nEnter key bit %d : ",j+1); scanf("%d",&d[j]); if((d[j]!=1)&& (d[j]!=0)) { printf("nInvalid Entry...Enter only 1s & 0s!!!n"); j--; } }}while(d[0]!=1);printf("n------------------------------------------------------------------n");printf("ttOutput ");printf("n------------------------------------------------------------------n");a1=n+(g-1);printf("nnThe Message : t");for(i=0;i<a1;++i){ if(i<a1) { p[i]=a[i]; } else { p[i]=0; }}for(i=0;i<a1;++i){ printf("%d",p[i]);}printf("nnThe Divisor : t");for(j=0;j<g;j++){ printf("%d",d[j]);}printf("nnIntermediate CRC : "); 16
  • 17. for(i=0;i<n;++i) { if(a[i]==0) { printf(" "); for(j=i;j<g+i;++j) { a[j] = a[j]^0; printf("%d",a[j]); } } else { printf(" "); a[i] = a[i]^d[0]; a[i+1]=a[i+1]^d[1]; a[i+2]=a[i+2]^d[2]; a[i+3]=a[i+3]^d[3]; printf("%d%d%d",a[i],a[i+1],a[i+2]); printf(" "); } } printf("nnThe Final CRC Bit is : t"); for(i=n;i<a1;++i) { printf("%d",a[i]); } s=n+a1; for(i=n;i<s;i++) { p[i]=a[i]; } printf("n"); printf("nThe Message transmitted is : "); for(i=0;i<a1;i++) { printf("%d",p[i]); }}void receiver(){ printf("nnEnter total number of data bits: "); scanf("%d",&n); printf("nEnter %d data bits : nn",n); for(i=0;i<n;i++) { 17
  • 18. printf("nEnter bit %d : ",i+1); scanf("%d",&a[i]); if((a[i]!=1)&& (a[i]!=0)) { printf("nInvalid Entry...Enter only 1s & 0s!!!n"); i--; }}printf("nEnter size of Key (! >databits): ");scanf("%d",&g);if(g>n){ printf("nInvalid Entry...Enter value less than databit!!!n"); printf("nEnter size of Key (! >databits): "); scanf("%d",&g);}do{ printf("nEnter Key values : nn"); for(j=0;j<g;j++) { printf("nEnter key bit %d : ",j+1); scanf("%d",&d[j]); if((d[j]!=1)&& (d[j]!=0)) { printf("nInvalid Entry...Enter only 1s & 0s!!!n"); j--; } }}while(d[0]!=1);printf("n-------------------------------------------------------------------------------n");printf("ttOutput");printf("n--------------------------------------------------------------------------------n");printf("nnThe Message : t");for(i=0;i<n;++i){ p[i]=a[i];}for(i=0;i<n;i++){ printf("%d",p[i]);}printf("nnThe Divisor : t");for(j=0;j<g;j++){ printf("%d",d[j]); 18
  • 19. }a1=n+(g-1);printf("nnCRCs : ");for(i=0;i<n;i++){ if(a[i]==0) { printf(" "); for(j=i;j<g+i;j++) { a[j] = a[j]^0; printf("%d",a[j]); } } else { printf(" "); a[i] = a[i]^d[0]; a[i+1]=a[i+1]^d[1]; a[i+2]=a[i+2]^d[2]; a[i+3]=a[i+3]^d[3]; printf("%d%d%d",a[i],a[i+1],a[i+2]); printf(" "); }}printf("nnThe Final CRC is : ");for(i=n;i<a1;++i){ printf("%d",a[i]);}s=n+a1;cnt=0;for(i=n;i<s;i++){ p[i]=a[i]; if(p[i]==1) { cnt++; }}printf("n");if(cnt==0){ printf("nnThe Message is received correctly");}else 19
  • 20. { printf("nnThe Message is not received correctly"); }}Output:-********************** CYCLIC REDUNCDANCY CHECK ****************------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 1----------------------------------------------- Senders Site------------------------------------------- 20
  • 21. Enter total number of data bits : 5Enter 5 data bits :Enter bit 1: 1Enter bit 2: 0Enter bit 3: 1Enter bit 4: 1Enter bit 5: 0Enter size of Key (! >databits) : 3Enter Key values :Enter key bit 1 : 1Enter key bit 2 : 1Enter key bit 3 : 0------------------------------------------------------------------------------------------------------------ Output------------------------------------------------------------------------------------------------------------The Message : 1011000The Divisor : 110Intermediate CRC : 011 001 010 010 010The Final CRC Bit is : 10The Message transmitted is : 1011010------------------------------------------------------------------------------------------------------------ 21
  • 22. MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 2---------------------------------------------- Receivers Site -----------------------------------------Enter total number of data bits: 5Enter 5 data bits :Enter bit 1 : 1Enter bit 2 : 1Enter bit 3 : 0Enter bit 4 : 1Enter bit 5 : 1Enter size of Key (! >databits): 3Enter Key values :Enter key bit 1 : 1Enter key bit 2 : 0Enter key bit 3 : 1------------------------------------------------------------------------------------------------------------ Output------------------------------------------------------------------------------------------------------------ 22
  • 23. The Message : 11011The Divisor : 101CRCs : 011 010 000 000 000The Final CRC is : 00The Message is received correctly------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 3 -4- CHECKSUM#include<stdio.h>#include<conio.h>#include<stdlib.h>int sum(int,int);int sum1(int,int,int);void sender();void receiver();int a[8],b[8],r[8],c[8],r1[8]; 23
  • 24. static int carry=0;int n,i,count=0;void main(){int choice;clrscr();printf("n*********************************** CHECKSUM*********************************n");while(1) { printf("nn-------------------------------------------------------------------------- n"); printf("ttttMAIN MENU"); printf("n--------------------------------------------------------------------------n n"); printf("Choice No. Choice Name.nn"); printf(" 1. Senders Sitenn 2. Receivers Sitenn 3. ExitnnnEnter Your Choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("------------------------------------------------------------- -----------n"); printf("ntt Senders Site "); printf("n----------------------------------------------------------- -------------n"); sender(); break; case 2: printf("------------------------------------------------------------- -----------n"); printf("nttReceivers Site "); printf("n----------------------------------------------------------- -------------n"); receiver(); break; case 3: exit(1); default: printf("nnINVALID ENTRY"); } } 24
  • 25. }void sender(){ printf("nEnter the total number of bits : "); scanf("%d",&n); printf("nEnter %d bits of Data 1 : nn",n); for(i=0;i<n;i++) { printf("nEnter bit %d: ",i+1); scanf("%d",&a[i]); if(a[i]!=0 && a[i]!=1) { printf("nnInvalid Bit......Enter another value(only 1s and 0s)!!!!!"); i--; } } printf("n------------------------------------------------------------------n"); printf("nEnter %d bits of Data 2 : nn",n); for(i=0;i<n;i++) { printf("nEnter bit %d: ",i+1); scanf("%d",&b[i]); if(b[i]!=0 && b[i]!=1) { printf("nnInvalid Bit......Enter another value(only 1s and 0s)!!!!!"); i--; } } printf("n-----------------------------------------------------------------------------n"); printf("nttOutput"); printf("n-----------------------------------------------------------------------------n"); printf("nnData 1:ttt "); for(i=0;i<n;i++) { printf("%d",a[i]); } printf("nnData 2:ttt "); for(i=0;i<n;i++) { printf("%d",b[i]); } for(i=n;i>=0;i--) { 25
  • 26. r[i]=sum(a[i],b[i]); } printf("nnResult :tt "); for(i=0;i<n;i++) { printf("%d",r[i]); } for(i=0;i<n;i++) { if (r[i]==0) { r[i]=1; } else { r[i]=0; } } printf("nn1s Compliment of Result :"); for(i=0;i<n;i++) { printf("%d",r[i]); }}int sum(int x,int y){ if(x==0 && y==0 && carry==0) { carry=0; return 0; } else if(x==0 && y==0 && carry==1) { carry=0; return 1; } else if(x==0 && y==1 && carry==0) { carry=0; return 1; } else if(x==0 && y==1 && carry==1) { carry=1; return 0; 26
  • 27. } else if(x==1 && y==0 && carry==0) { carry=0; return 1; } else if(x==1 && y==0 && carry==1) { carry=1; return 0; } else if(x==1 && y==1 && carry==0) { carry=1; return 0; } else if(x==1 && y==1 && carry==1) { carry=1; return 1; } else return(1);}void receiver(){ printf("nEnter the total number of bits : "); scanf("%d",&n); printf("nEnter %d bits of Data 1 : nn",n); for(i=0;i<n;i++) { printf("nEnter bit %d: ",i+1); scanf("%d",&a[i]); if(a[i]!=0 && a[i]!=1) { printf("nnInvalid Bit......Enter another value(only 1s and 0s)!!!!!"); i--; } } printf("n------------------------------------------------------------------n"); printf("nEnter %d bits of Data 2 : nn",n); for(i=0;i<n;i++) { printf("nEnter bit %d: ",i+1); 27
  • 28. scanf("%d",&b[i]); if(b[i]!=0 && b[i]!=1) { printf("nnInvalid Bit......Enter another value(only 1s and 0s)!!!!!"); i--; }}printf("n------------------------------------------------------------------n");printf("nEnter %d bits of Checksum : nn",n);for(i=0;i<n;i++){ printf("nEnter bit %d: ",i+1); scanf("%d",&c[i]); if(c[i]!=0 && c[i]!=1) { printf("nnInvalid Bit......Enter another value(only 1s and 0s)!!!!!"); i--; }}printf("n----------------------------------------------------------------------------------");printf("nttOutput");printf("n----------------------------------------------------------------------------------");printf("nnData 1:ttt ");for(i=0;i<n;i++){ printf("%d",a[i]);}printf("nnData 2:ttt ");for(i=0;i<n;i++){ printf("%d",b[i]);}printf("nnChecksum:tt ");for(i=0;i<n;i++){ printf("%d",c[i]);}for(i=n;i>=0;i--){ r1[i]=sum1(a[i],b[i],c[i]);}printf("nnResult :tt ");for(i=0;i<n;i++){ 28
  • 29. printf("%d",r1[i]); } for(i=0;i<n;i++) { if (r1[i]==0) { r1[i]=1; } else { r1[i]=0; } } printf("nn1s Compliment of Result :"); for(i=0;i<n;i++) { printf("%d",r1[i]); } for(i=0;i<n;i++) { if(r1[i]==0) { count=count+1; } } if(count==n) { printf("nnData is ERROR free, hence ACCEPTED... "); } else { printf("nnData is NOT ERROR free, hence NOT ACCEPTED..."); }}int sum1(int x,int y,int z){ if(x==0 && y==0 && z==0 && carry==0) { carry=0; return(0); } else if(x==1 && y==0 && z==0 && carry==0) { carry=0; return(1); 29
  • 30. }else if(x==0 && y==1 && z==0 && carry==0){ carry=0; return(1);}else if(x==1 && y==1 && z==0 && carry==0){ carry=1; return(0);}else if(x==0 && y==0 && z==1 && carry==0){ carry=0; return(1);}else if(x==1 && y==0 && z==1 && carry==0){ carry=1; return(0);}else if(x==0 && y==1 && z==1 && carry==0){ carry=1; return(0);}else if(x==1 && y==1 && z==1 && c==0){ carry=1; return(1);}else if(x==0 && y==0 && z==0 && carry==1){ carry=0; return(1);}else if(x==1 && y==0 && z==0 && carry==1){ carry=1; return(0);}else if(x==0 && y==1 && z==0 && carry==1){ carry=1; return(0);} 30
  • 31. else if(x==1 && y==1 && z==0 && carry==1) { carry=1; return(1); } else if(x==0 && y==0 && z==1 && carry==1) { carry=1; return(0); } else if(x==1 && y==0 && z==1 && carry==1) { carry=1; return(1); } else if(x==0 && y==1 && z==1 && carry==1) { carry=1; return(1); } else return(1);}Output:-****************************** CHECKSUM *****************************------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 31
  • 32. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 1------------------------------------------------------------------------------------------------------------ Senders Site------------------------------------------------------------------------------------------------------------Enter the total number of bits : 5Enter 5 bits of Data 1 :Enter bit 1: 1Enter bit 2: 0Enter bit 3: 1Enter bit 4: 1Enter bit 5: 0------------------------------------------------------------------------------------------------------------Enter 5 bits of Data 2 :Enter bit 1: 0Enter bit 2: 1Enter bit 3: 0Enter bit 4: 0Enter bit 5: 1------------------------------------------------------------------------------------------------------------ 32
  • 33. Output------------------------------------------------------------------------------------------------------------Data 1: 10110Data 2: 01001Result : 111111s Compliment of Result :00000------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 2------------------------------------------------------------------------------------------------------------ Receivers Site------------------------------------------------------------------------------------------------------------Enter the total number of bits : 5Enter 5 bits of Data 1 :Enter bit 1: 0Enter bit 2: 0Enter bit 3: 0Enter bit 4: 0 33
  • 34. Enter bit 5: 1------------------------------------------------------------------------------------------------------------Enter 5 bits of Data 2 :Enter bit 1: 1Enter bit 2: 2Invalid Bit......Enter another value(only 1s and 0s)!!!!!Enter bit 2: 1Enter bit 3: 1Enter bit 4: 1Enter bit 5: 0------------------------------------------------------------------------------------------------------------Enter 5 bits of Checksum :Enter bit 1: 0Enter bit 2: 0Enter bit 3: 0Enter bit 4: 0Enter bit 5: 0------------------------------------------------------------------------------------------------------------ Output------------------------------------------------------------------------------------------------------------Data 1: 00001 34
  • 35. Data 2: 11110Checksum: 00000Result : 111111s Compliment of Result :00000Data is ERROR free, hence ACCEPTED...------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 3 35
  • 36. -5- HAMMING CODE#include<stdio.h>#include<conio.h>#include<math.h>void sender();void receiver();int a,m,r,i,count=0,n,count1=0;void main(){ int ans; clrscr(); printf("nn*********************** HAMMING CODE ***********************nn"); while(1) { printf("n--------------------------------------------------------------------------n "); printf("ttttnMAIN MENU"); printf("n--------------------------------------------------------------------------n n"); printf("Choice No. Choice Name.nn"); printf(" 1. Senders Sitenn 2. Receivers Sitenn 3. ExitnnnEnter Your Choice : "); scanf("%d",&ans); switch(ans) { case 1: printf("n----------------------------------------------------------- --------------n"); printf("ttt Senders Site"); printf("n----------------------------------------------------------- --------------n"); sender(); break; case 2: printf("n----------------------------------------------------------- --------------n"); printf("ttt Receivers Site"); 36
  • 37. printf("n----------------------------------------------------------- --------------n"); receiver(); break; case 3: exit(1); default: printf("nnINVALID ENTRY!!!"); } }}void sender(){ int data[20]; printf("Enter the number of data bits (MAX 11) : "); scanf("%d",&m); for(i=0;i<20;i++) data[i]=0; while(pow(2,r)-r<m+1) r++; printf("nThe Parity Bits are : %d n",r); for(i=1;i<9;i++) { if(i==1||i==2||i==4||i==8) { data[i]=4; } } printf("nEnter %d data bits : ",m); for(i=1;i<=m+r;i++) { if(i==3||i==5||i==6||i==7||i==9||i==10||i==11||i==12||i==13||i==14||i==15||i==16) { scanf("%d",&data[i]); if (data[i]!=0&&data[i]!=1) { printf("Invalid Entry!!! Enter only 0s and 1s"); i--; } } } printf("nData Is : "); for(i=1;i<=m+r;i++) { 37
  • 38. printf("%d ",data[i]);}for(i=1;i<=m+r;i++){ if(data[i]==4) { a=i; } if(a==1) { count=data[3]+data[5]+data[7]+data[9]+data[11]; if(count%2==0) data[a]=0; else data[a]=1; } if(a==2) { count=data[3]+data[6]+data[7]+data[10]+data[11]; if(count%2==0) data[a]=0; else data[a]=1; } if(a==4) { count=data[5]+data[6]+data[7]; if(count%2==0) data[a]=0; else data[a]=1; } if(a==8) { count=data[9]+data[10]+data[11]; if(count%2==0) data[a]=0; else data[a]=1; }}printf("nn-----------------------------------------------------------------------------n");printf("tttOutput");printf("n--------------------------------------------------------------------------------n");printf("nnNew Data To Be Sent Is : ");for(i=1;i<=m+r;i++) 38
  • 39. printf("%d ",data[i]);}void receiver(){ int data[20],temp; printf("nEnter the number of data bits : "); scanf("%d",&m); for(i=0;i<20;i++) data[i]=0; printf("nEnter %d data bits : ",m); for(i=1;i<=m;i++) { scanf("%d",&data[i]); if (data[i]!=0&&data[i]!=1) { printf("Invalid Entry!!! Enter only 0s and 1s"); i--; } } for(i=1;i<=m;i++) { if(i==1) { count=data[1]+data[3]+data[5]+data[7]+data[9]+data[11]; if(count%2==0) { data[i]=data[i]; } else { count1=count1+i; } } if(i==2) { count=data[2]+data[3]+data[6]+data[7]+data[10]+data[11]; if(count%2==0) { data[i]=data[i]; } else { 39
  • 40. count1=count1+i; } } if(i==4) { count=data[4]+data[5]+data[6]+data[7]; if(count%2==0) { data[i]=data[i]; } else { count1=count1+i; } } if(i==8) { count=data[8]+data[9]+data[10]+data[11]; if(count%2==0) { data[i]=data[i]; } else { count1=count1+i; } }}printf("nn-----------------------------------------------------------------------------n");printf("ttOutputn");printf("n--------------------------------------------------------------------------------n");if(count1>0){ if(count1>m) { printf("nError Is At Position %d",count1); printf("nnOOPS!!! Position is not availablen"); } else { printf("nError Is At Position %d",count1); if(data[count1]==0) data[count1]=1; else data[count1]=0; printf("nCorrect Code Word : "); 40
  • 41. for(i=1;i<=m;i++) { printf("%d ",data[i]); } } } else { printf("nReceived Data Is ERROR FREE... n"); printf("nCorrect Code Word : "); for(i=1;i<=m;i++) { printf("%d ",data[i]); } }} 41
  • 42. Output:-***************************** HAMMING CODE *************************------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 1------------------------------------------------------------------------------------------------------------ Senders Site------------------------------------------------------------------------------------------------------------Enter the number of data bits (MAX 11) : 7The Parity Bits are : 4Enter 7 data bits : 1 0 0 1 0 1 1Data Is : 4 4 1 4 0 0 1 4 0 1 1------------------------------------------------------------------------------------------------------------ Output------------------------------------------------------------------------------------------------------------ 42
  • 43. New Data To Be Sent Is : 1 0 1 1 0 0 1 0 0 1 1------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 2------------------------------------------------------------------------------------------------------------ Receivers Site------------------------------------------------------------------------------------------------------------Enter the number of data bits : 11Enter 11 data bits : 1 0 1 1 0 0 1 0 1 1 1------------------------------------------------------------------------------------------------------------ Output------------------------------------------------------------------------------------------------------------Error Is At Position 9Correct Code Word : 1 0 1 1 0 0 1 0 0 1 1------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 43
  • 44. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 3 -6- BIT STUFFING#include<stdio.h>#include<conio.h>#include<stdlib.h>void sender();void receiver();int data[20],i,j,n,count=0;void main(){ int choice; clrscr(); printf("********************************** BIT STUFFING ********************************"); while(1) { printf("nn-------------------------------------------------------------------------- n"); printf("ttttMAIN MENU"); printf("n--------------------------------------------------------------------------n n"); printf("Choice No. Choice Name.nn"); printf(" 1. Senders Sitenn 2. Receivers Sitenn 3. ExitnnnEnter Your Choice : "); scanf("%d",&choice); 44
  • 45. switch(choice) { case 1: printf("n----------------------------------------------------------- --------------n"); printf("ttt Senders Site"); printf("n----------------------------------------------------------- --------------n"); sender(); break; case 2: printf("n----------------------------------------------------------- --------------n"); printf("ttt Receivers Site"); printf("n----------------------------------------------------------- --------------n"); receiver(); break; case 3: exit(1); default: printf("nInvalid Choice!!!"); } }}void sender(){ printf("nEnter the length of your code : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("nEnter %d bit : ",i+1); scanf("%d",&data[i]); } printf("nnThe Original Data entered : "); for(i=0;i<n;i++) { printf("%d ",data[i]); } i=0; while(i<n) { if(data[i]==1) { i++; 45
  • 46. count++; if(count==5) { for(j=n+1;j>i;j--) { data[j]=data[j-1]; } data[i]=0; } } else { i++; count=0; } } printf("nn--------------------------------------------------------------------------n"); printf("ttt Outputn"); printf("-------------------------------------------------------------------------------n"); printf("nnData After Stuffing : "); for(i=0;i<n+1;i++) { printf("%d ",data[i]); }}void receiver(){ printf("nEnter the length of your code : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("nEnter %d bit : ",i+1); scanf("%d",&data[i]); } printf("nnThe Original Data entered : "); for(i=0;i<n;i++) { printf("%d ",data[i]); } i=0; while(i<n) { if(data[i]==1) { i++; 46
  • 47. count++; if(count==5) { for(j=i;j<n+1;j++) { data[j]=data[j+1]; } n--; } } else { i++; count=0; } } printf("nn--------------------------------------------------------------------------------"); printf("nttt Output"); printf("n--------------------------------------------------------------------------------n"); printf("nData After De-Stuff : "); for(i=0;i<n;i++) { printf("%d ",data[i]); }} 47
  • 48. Output:-****************************** BIT STUFFING **************************------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 1------------------------------------------------------------------------------------------------------------ Senders Site------------------------------------------------------------------------------------------------------------ 48
  • 49. Enter the length of your code : 11Enter 1 bit : 0Enter 2 bit : 1Enter 3 bit : 1Enter 4 bit : 1Enter 5 bit : 1Enter 6 bit : 1Enter 7 bit : 1Enter 8 bit : 0Enter 9 bit : 1Enter 10 bit : 1Enter 11 bit : 0The Original Data entered : 0 1 1 1 1 1 1 0 1 1 0------------------------------------------------------------------------------------------------------------ Output------------------------------------------------------------------------------------------------------------Data After Stuffing : 0 1 1 1 1 1 0 1 0 1 1 0------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 49
  • 50. 3. ExitEnter Your Choice : 2------------------------------------------------------------------------------------------------------------ Receivers Site------------------------------------------------------------------------------------------------------------Enter the length of your code : 11Enter 1 bit : 0Enter 2 bit : 1Enter 3 bit : 1Enter 4 bit : 1Enter 5 bit : 1Enter 6 bit : 1Enter 7 bit : 0Enter 8 bit : 0Enter 9 bit : 1Enter 10 bit : 0Enter 11 bit : 1The Original Data entered : 0 1 1 1 1 1 0 0 1 0 1------------------------------------------------------------------------------------------------------------ Output------------------------------------------------------------------------------------------------------------Data After De-Stuff : 0 1 1 1 1 1 0 1 0 1------------------------------------------------------------------------------------------------------------ 50
  • 51. MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 3 -7- WALSH MATRIX#include<stdio.h>#include<conio.h>#include<stdlib.h>void stn1();void stn2();void stn4();int n,i,j;void main(){ 51
  • 52. clrscr();printf("n***************** CHIPPING SEQUENCE USING WALSHMATRIX *********************nn");while(1){ printf("n--------------------------------------------------------------------------n "); printf("ttttMAIN MENU"); printf("n--------------------------------------------------------------------------n n"); printf("Choice No. Station No.nn"); printf(" 1. Station 1nn 2. Station 2nn 3. Station 4nn 4. ExitnnEnter Your Choice : "); scanf("%d",&n); switch(n) { case 1: printf("n----------------------------------------------------------- --------------n"); printf("ttt Station 1"); printf("n----------------------------------------------------------- --------------n"); stn1(); break; case 2: printf("n----------------------------------------------------------- --------------n"); printf("ttt Station 2"); printf("n----------------------------------------------------------- --------------n"); stn2(); break; case 3: printf("n----------------------------------------------------------- --------------n"); printf("ttt Station 4"); printf("n----------------------------------------------------------- --------------n"); stn4(); break; case 4: exit(1); default: printf("nnInvalid Entry!!!"); }} 52
  • 53. }void stn1(){ int w1; printf("nYour Code For Station 1 : 1n");}void stn2(){ int w2[2][2]; for(i=0;i<2;i++) { for(j=0;j<2;j++) { if ((i==1) && (j==1)) { w2[i][j]=-1; } else { w2[i][j]=1; } } } printf("nYour Code For Station 2 : nn"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("%d ",w2[i][j]); } printf("n"); }}void stn4(){ int w4[4][4]; for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(i==0) { w4[i][j]=1; 53
  • 54. } else if(j==0) { w4[i][j]=1; } else if((i==1)&&(j==1)) { w4[i][j]=-1; } else if((i==1)&&(j==3)) { w4[i][j]=-1; } else if((i==2)&&(j==2)) { w4[i][j]=-1; } else if((i==2)&&(j==3)) { w4[i][j]=-1; } else if((i==3)&&(j==1)) { w4[i][j]=-1; } else if((i==3)&&(j==2)) { w4[i][j]=-1; } else { w4[i][j]=1; } } } printf("nYour Code For Station 4 : nn"); for(i=0;i<4;i++) { for(j=0;j<4;j++) { printf("%dt",w4[i][j]); } printf("n"); }} 54
  • 55. Output:-************** CHIPPING SEQUENCE USING WALSH MATRIX ***********------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Station No. 1. Station 1 55
  • 56. 2. Station 2 3. Station 4 4. ExitEnter Your Choice : 1------------------------------------------------------------------------------------------------------------ Station 1------------------------------------------------------------------------------------------------------------Your Code For Station 1 : 1------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Station No. 1. Station 1 2. Station 2 3. Station 4 4. ExitEnter Your Choice : 5Invalid Entry!!!------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Station No. 56
  • 57. 1. Station 1 2. Station 2 3. Station 4 4. ExitEnter Your Choice : 2------------------------------------------------------------------------------------------------------------ Station 2------------------------------------------------------------------------------------------------------------Your Code For Station 2 :1 11 -1------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Station No. 1. Station 1 2. Station 2 3. Station 4 4. ExitEnter Your Choice : 3 57
  • 58. ------------------------------------------------------------------------------------------------------------ Station 4------------------------------------------------------------------------------------------------------------Your Code For Station 4 :1 1 1 11 -1 1 -11 1 -1 -11 -1 -1 1------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Station No. 1. Station 1 2. Station 2 3. Station 4 4. ExitEnter Your Choice : 4 58
  • 59. -8- BIT COMPRESSION#include<stdio.h>#include<conio.h>void main(){ int i,j,a[20],cnt1,cnt2,n; clrscr(); printf("n************************** BIT COMPRESSION ************************************n"); printf("nnEnter the length of Data ( <= 20 ) : "); scanf("%d",&n); printf("nnEnter %d Bits : n",n); for(i=1;i<=n;i++) { printf("nnEnter Bit %d:",i); scanf("%d",&a[i]); } printf("nnOriginal Data : "); for(i=1;i<=n;i++) { printf("%d ",a[i]); } cnt1=0; cnt2=0; printf("nn-----------------------------------------------------------------------------n"); printf("ttt Output"); printf("n-------------------------------------------------------------------------------n"); printf("nThe Data Contains : n"); for(i=1;i<=n;i++) { if(a[i]==1) { if(cnt2!=0) { printf("nn 0s : %d",cnt2); cnt2=0; } cnt1++; } 59
  • 60. if(a[i]==0) { if(cnt1!=0) { printf("nn 1s : %d",cnt1); cnt1=0; } cnt2++; } } if(cnt1==0) printf("nn 0s : %d",cnt2); if(cnt2==0) printf("nn 1s : %d",cnt1); getch();} 60
  • 61. Output:-************************** BIT COMPRESSION **************************Enter the length of Data : 11Enter 11 Bits :Enter Bit 1 : 1Enter Bit 2 : 1Enter Bit 3 : 0Enter Bit 4 : 0Enter Bit 5 : 0Enter Bit 6 : 0Enter Bit 7 : 1Enter Bit 8 : 1Enter Bit 9 : 0Enter Bit 10 : 1Enter Bit 11 :1Original Data : 1 1 0 0 0 0 1 1 0 1 1------------------------------------------------------------------------------------------------------------ Output------------------------------------------------------------------------------------------------------------The Data Contains :1s : 20s : 4 61
  • 62. 1s : 20s : 11s : 2 -9- MONOALPHABETIC ENCRYPTION#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<ctype.h>void sender();void receiver();void main(){ int choice; clrscr(); printf("n************************** MONOALPHABETIC ENCRYPTION ***********************n"); while(1) { printf("nn-------------------------------------------------------------------------- n"); printf("ttttMAIN MENU"); printf("n--------------------------------------------------------------------------n n"); printf("Choice No. Choice Name.nn"); printf(" 1. Senders Sitenn 2. Receivers Sitenn 3. ExitnnnEnter Your Choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("n----------------------------------------------------------- --------------n"); printf("ttt Senders Site"); printf("n----------------------------------------------------------- --------------n"); sender(); break; case 2: printf("n----------------------------------------------------------- --------------n"); 62
  • 63. printf("ttt Receivers Site"); printf("n----------------------------------------------------------- --------------n"); receiver(); break; case 3: exit(1); default: printf("nnInvalid Entry"); } }}void sender(){ int i,n; char *ptr,*str,*str1; printf("nEnter the Plain Text : "); scanf("%s",str); printf("nEnter the Charter difference : "); scanf("%d",&n); printf("n-----------------------------------------------------------------------nn"); printf("nPlain Text : "); printf("%s",str); printf("nnCipher Text After Encryption : "); for(i=0;str[i]!=0;i++) { if((str[i]>=65 && str[i]<=90-n) || (str[i]>=97 && str[i]<=122-n)) { ptr[i]=str[i]+n; } else if((str[i]>90-n && str[i]<=90)||(str[i]>122-n && str[i]<=122)) { ptr[i]=str[i]-26+n; } else { ptr[i]=str[i]; } } ptr[i]=0; printf("%s",ptr);} 63
  • 64. void receiver(){ int i,n; char *ptr,*str,*str1; printf("nEnter the Cipher text : "); scanf("%s",ptr); printf("nEnter the Charter difference : "); scanf("%d",&n); printf("n-----------------------------------------------------------------------nn"); printf("nCipher Text : "); printf("%s",ptr); printf("nnPlain text After Decryption : "); for(i=0;ptr[i]!=0;i++) { if((ptr[i]>=65+n && ptr[i]<=90) || (ptr[i]>=97+n && ptr[i]<=122)) { str1[i]=ptr[i]-n; } else if((ptr[i]>=65 && ptr[i]<=65+n)||(ptr[i]>=97 && ptr[i]<=97+n)) { str1[i]=ptr[i]+26-n; } else { str1[i]=ptr[i]; } } str1[i]=0; printf("%s",str1);} 64
  • 65. Output:-********************* MONOALPHABETIC ENCRYPTION ****************------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 1------------------------------------------------------------------------------------------------------------ Senders Site------------------------------------------------------------------------------------------------------------Enter the Plain Text : TARANNUMEnter the Charter difference : 5------------------------------------------------------------------------------------------------------------Plain Text : TARANNUMCipher Text After Encryption : YFWFSSZR 65
  • 66. ------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 3. ExitEnter Your Choice : 2------------------------------------------------------------------------------------------------------------ Receivers Site------------------------------------------------------------------------------------------------------------Enter the Cipher text : yfwfsszrEnter the Charter difference : 5------------------------------------------------------------------------------------------------------------Cipher Text : yfwfsszrPlain text After Decryption : tarannum------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------Choice No. Choice Name. 1. Senders Site 2. Receivers Site 66
  • 67. 3. ExitEnter Your Choice : 3 -10- POLYALPHABETIC ENCRYPTION#include <stdio.h>#include <string.h>void main(){ unsigned int i,j=1,k; char input[257],key[33],l[257]; clrscr(); k=65; printf("n************************** POLYALPHABETIC ENCRYPTION ***************************n"); printf("ntttt VIGENERE TABLE nn"); printf("------------------------------------------------------------------------------nn"); while (j <= 26) { for (i=j; i<j+26; i++) { if (k >90) k = k - 26; printf("%c ",k ); k++; } printf("n"); 67
  • 68. j++; k++; } printf("n----------------------------------------------------------------------------nn"); printf("nEnter Plain Text : "); gets(input); printf("nEnter Encryption Key : "); gets(key); printf("n----------------------------------------------------------------------------n"); printf("nKEY : t "); for(i=0,j=0;i<strlen(input);i++,j++) { if(j>=strlen(key)) { j=0; } l[i]=key[j]; } for(i=0;i<strlen(input);i++) { printf("%c",l[i]); } printf("nnPlain Text : "); printf("%s",input); printf("nnCipher Text : "); for(i=0,j=0;i<strlen(input);i++,j++) { if(j>=strlen(key)) { j=0; } printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j])-65)) %26)); } getch();} 68
  • 69. Output:-********************* POLYALPHABETIC ENCRYPTION ***************** VIGENERE TABLE------------------------------------------------------------------------------------------------------------A B C D E F G H I J K L M N O P Q R S T U V W X Y ZB C D E F G H I J K L M N O P Q R S T U V W X Y Z AC D E F G H I J K L M N O P Q R S T U V W X Y Z A BD E F G H I J K L M N O P Q R S T U V W X Y Z A B CE F G H I J K L M N O P Q R S T U V W X Y Z A B C DF G H I J K L M N O P Q R S T U V W X Y Z A B C D EG H I J K L M N O P Q R S T U V W X Y Z A B C D E FH I J K L M N O P Q R S T U V W X Y Z A B C D E F GI J K L M N O P Q R S T U V W X Y Z A B C D E F G HJ K L M N O P Q R S T U V W X Y Z A B C D E F G H IK L M N O P Q R S T U V W X Y Z A B C D E F G H I JL M N O P Q R S T U V W X Y Z A B C D E F G H I J KM N O P Q R S T U V W X Y Z A B C D E F G H I J K LN O P Q R S T U V W X Y Z A B C D E F G H I J K L MO P Q R S T U V W X Y Z A B C D E F G H I J K L M NP Q R S T U V W X Y Z A B C D E F G H I J K L M N O 69
  • 70. Q R S T U V W X Y Z A B C D E F G H I J K L M N O PR S T U V W X Y Z A B C D E F G H I J K L M N O P QS T U V W X Y Z A B C D E F G H I J K L M N O P Q RT U V W X Y Z A B C D E F G H I J K L M N O P Q R SU V W X Y Z A B C D E F G H I J K L M N O P Q R S TV W X Y Z A B C D E F G H I J K L M N O P Q R S T UW X Y Z A B C D E F G H I J K L M N O P Q R S T U VX Y Z A B C D E F G H I J K L M N O P Q R S T U V WY Z A B C D E F G H I J K L M N O P Q R S T U V W XZ A B C D E F G H I J K L M N O P Q R S T U V W X Y------------------------------------------------------------------------------------------------------------Enter Plain Text : HIWORLDEnter Encryption Key : ME------------------------------------------------------------------------------------------------------------KEY : MEMEMEMPlain Text : HIWORLDCipher Text : TMISDPP 70
  • 71. -11- RSA ALGORITHM#include<stdio.h>#include<conio.h>#include<math.h>int phi,m,n,e,d,c,flg;void check(){ int i; for(i=3;e%i==0 && phi%i==0;i+2) { flg = 1; return; } flg = 0; }void encrypt(){ int i; c = 1; 71
  • 72. for(i=0;i<e;i++) c=c*m%n; c=c%n; printf("nEncrypted Plain Text : %d",c); printf("nn----------------------------------------------------------------------n");}void decrypt(){ int i; m = 1; for(i=0;i< d;i++) m=m*c%n; m = m%n; printf("nDecrypted Cipher Text : %d",m);}void main(){ int p,q,s; clrscr(); printf("n************************* RSA ALGORITHM *****************************nn"); printf("nEnter Two Relative Prime Numbers : "); scanf("%d%d",&p,&q); n = p*q; phi=(p-1)*(q-1); printf("nF(n)t = %d",phi); do { printf("nnEnter value for e : "); scanf("%d",&e); check(); }while(flg==1); d = 1; do { s = (d*e)%phi; d++; }while(s!=1); d = d-1; printf("n-----------------------------------------------------------------------------nn"); printf("nPublic Keyt: {%d,%d}",e,n); printf("nnPrivate Keyt: {%d,%d}",d,phi); printf("nn---------------------------------------------------------------------------nn"); printf("nEnter The Plain Text : "); 72
  • 73. scanf("%d",&m); encrypt(); printf("nnEnter the Cipher text : "); scanf("%d",&c); decrypt(); getch();}Output:-**************************** RSA ALGORITHM *************************Enter Two Relative Prime Numbers : 7 11F(n) = 60Enter value for e : 13------------------------------------------------------------------------------------------------------------Public Key : {13,77}Private Key : {37,60}------------------------------------------------------------------------------------------------------------Enter The Plain Text : 7Encrypted Plain Text : 35 73
  • 74. ------------------------------------------------------------------------------------------------------------Enter the Cipher text : 35Decrypted Cipher Text : 7 -12-DIJKSTRA’S SHORTEST PATH#include<stdio.h>#include<conio.h>#define MAX 10#define TEMP 0#define PERM 1#define infinity 9999struct node{ int predecessor; int dist; int status;};int adj[MAX][MAX];int n;void main() 74
  • 75. { int i,j; int source,dest; int path[MAX]; int shortdist,count; clrscr(); printf("************************ DIJKSTRAS SHORTEST PATH **********************"); create_graph(); printf("The adjacency matrix is :n"); display(); while(1) { printf("nEnter source node(0 to quit) : "); scanf("%d",&source); printf("nEnter destination node(0 to quit) : "); scanf("%d",&dest); if(source==0 || dest==0) exit(1); count = findpath(source,dest,path,&shortdist); if(shortdist!=0) { printf("nShortest distance is : %dn", shortdist); printf("nShortest Path is : "); for(i=count;i>1;i--) printf("%d->",path[i]); printf("%d",path[i]); printf("n"); } else printf("nThere is no path from source to destinationnoden"); } getch();}create_graph(){ int i,max_edges,origin,destin,wt; printf("Enter number of vertices : "); scanf("%d",&n); max_edges=n*(n-1); 75
  • 76. for(i=1;i<=max_edges;i++) { printf("nEnter edge %d(0 0 to quit) : ",i); scanf("%d %d",&origin,&destin); if((origin==0) && (destin==0)) break; printf("nEnter weight for this edge : "); scanf("%d",&wt); if( origin > n || destin > n || origin<=0 || destin<=0) { printf("nInvalid edge!n"); i--; } else adj[origin][destin]=wt; }}display(){ int i,j; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf("%3d",adj[i][j]); printf("n"); }}int findpath(int s,int d,int path[MAX],int *sdist){ struct node state[MAX]; int i,min,count=0,current,newdist,u,v; *sdist=0; for(i=1;i<=n;i++) { state[i].predecessor=0; state[i].dist = infinity; state[i].status = TEMP; } state[s].predecessor=0; state[s].dist = 0; state[s].status = PERM; current=s; while(current!=d) 76
  • 77. { for(i=1;i<=n;i++) { if ( adj[current][i] > 0 && state[i].status == TEMP ) { newdist=state[current].dist + adj[current][i]; if( newdist < state[i].dist ) { state[i].predecessor = current; state[i].dist = newdist; } } } min=infinity; current=0; for(i=1;i<=n;i++) { if(state[i].status == TEMP && state[i].dist < min) { min = state[i].dist; current=i; } } if(current==0) return 0; state[current].status=PERM; } while( current!=0 ) { count++; path[count]=current; current=state[current].predecessor; } for(i=count;i>1;i--) { u=path[i]; v=path[i-1]; *sdist+= adj[u][v]; } return (count);} 77
  • 78. Output:-********************** DIJKSTRAS SHORTEST PATH *******************Enter number of vertices : 6Enter edge 1(0 0 to quit) : 1 2Enter weight for this edge : 6Enter edge 2(0 0 to quit) : 2 5Enter weight for this edge : 3Enter edge 3(0 0 to quit) : 2 6Enter weight for this edge : 2Enter edge 4(0 0 to quit) : 3 4 78
  • 79. Enter weight for this edge : 2Enter edge 5(0 0 to quit) : 3 6Enter weight for this edge : 3Enter edge 6(0 0 to quit) : 4 6Enter weight for this edge : 1Enter edge 7(0 0 to quit) : 5 4Enter weight for this edge : 4Enter edge 8(0 0 to quit) : 6 5Enter weight for this edge : 3Enter edge 9(0 0 to quit) : 0 0The adjacency matrix is : 0 6 0 0 0 0 0 0 0 0 3 2 0 0 0 2 0 3 0 0 0 0 0 1 0 0 0 4 0 0 0 0 0 0 3 0Enter source node(0 to quit) : 1Enter destination node(0 to quit) : 6Shortest distance is : 8Shortest Path is : 1->2->6Enter source node(0 to quit) : 2Enter destination node(0 to quit) :4 79
  • 80. Shortest distance is : 7Shortest Path is : 2->5->4Enter source node(0 to quit) : 0Enter destination node(0 to quit) : 0 -13-PRIM’S ALGORITHM#include<stdio.h>#include<conio.h>#define MAX 10#define TEMP 0#define PERM 1#define FALSE 0#define TRUE 1#define infinity 9999struct node{ int predecessor; int dist; int status;}; 80
  • 81. struct edge{ int u; int v;};int adj[MAX][MAX];int n;main(){ int i,j; int path[MAX]; int wt_tree,count; struct edge tree[MAX]; clrscr(); printf("********* MINIMUM SPANNING TREE FROM PRIMS ALGORITHM ********n"); create_graph(); printf("nAdjacency matrix is : nn"); display(); count = maketree(tree,&wt_tree); printf("nWeight of spanning tree is : %dn", wt_tree); printf("nEdges to be included in spanning tree are : nn"); for(i=1;i<=count;i++) { printf("%d->",tree[i].u); printf("%dn",tree[i].v); } getch();}create_graph(){ int i,max_edges,origin,destin,wt; printf("nEnter number of vertices : "); scanf("%d",&n); max_edges=n*(n-1)/2; for(i=1;i<=max_edges;i++) { printf("nEnter edge %d(0 0 to quit) : ",i); 81
  • 82. scanf("%d %d",&origin,&destin); if((origin==0) && (destin==0)) break; printf("nEnter weight for this edge : "); scanf("%d",&wt); if( origin > n || destin > n || origin<=0 || destin<=0) { printf("nInvalid edge!n"); i--; } else { adj[origin][destin]=wt; adj[destin][origin]=wt; } } if(i<n-1) { printf("nSpanning tree is not possiblen"); exit(1); }}display(){ int i,j; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf("%3d",adj[i][j]); printf("n"); }}int maketree(struct edge tree[MAX],int *weight){ struct node state[MAX]; int i,k,min,count,current,newdist; int m; int u1,v1; *weight=0; for(i=1;i<=n;i++) { state[i].predecessor=0; state[i].dist = infinity; state[i].status = TEMP; 82
  • 83. } state[1].predecessor=0; state[1].dist = 0; state[1].status = PERM; current=1; count=0; while( all_perm(state) != TRUE ) { for(i=1;i<=n;i++) { if ( adj[current][i] > 0 && state[i].status == TEMP ) { if( adj[current][i] < state[i].dist ) { state[i].predecessor = current; state[i].dist = adj[current][i]; } } } min=infinity; for(i=1;i<=n;i++) { if(state[i].status == TEMP && state[i].dist < min) { min = state[i].dist; current=i; } } state[current].status=PERM; u1=state[current].predecessor; v1=current; count++; tree[count].u=u1; tree[count].v=v1; *weight=*weight+adj[u1][v1]; } return (count);}int all_perm(struct node state[MAX] ){ int i; for(i=1;i<=n;i++) if( state[i].status == TEMP ) 83
  • 84. return FALSE; return TRUE;}Output:-******** MINIMUM SPANNING TREE FROM PRIMS ALGORITHM ********Enter number of vertices : 7Enter edge 1(0 0 to quit) : 1 2Enter weight for this edge : 3Enter edge 2(0 0 to quit) : 1 3Enter weight for this edge : 10Enter edge 3(0 0 to quit) : 1 4Enter weight for this edge : 2Enter edge 4(0 0 to quit) : 1 5 84
  • 85. Enter weight for this edge : 4Enter edge 5(0 0 to quit) : 2 5Enter weight for this edge : 7Enter edge 6(0 0 to quit) : 3 4Enter weight for this edge : 12Enter edge 7(0 0 to quit) : 3 6Enter weight for this edge : 15Enter edge 8(0 0 to quit) : 4 5Enter weight for this edge : 6Enter edge 9(0 0 to quit) : 4 6Enter weight for this edge : 4Enter edge 10(0 0 to quit) : 5 6Enter weight for this edge : 5Enter edge 11(0 0 to quit) : 5 7Enter weight for this edge : 2Enter edge 12(0 0 to quit) : 6 7Enter weight for this edge : 3Enter edge 13(0 0 to quit) : 0 0Adjacency matrix is : 0 3 10 2 4 0 0 3 0 0 0 7 0 010 0 0 12 0 15 0 2 0 12 0 6 4 0 4 7 0 6 0 5 2 0 0 15 4 5 0 3 0 0 0 0 2 3 0 85
  • 86. Weight of spanning tree is : 24Edges to be included in spanning tree are :1->41->21->55->77->61->3 86