Upload
yogeshnikalje89
View
171
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
-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("\n\n*********************** VERTICAL REDUNDANCY CHECK ***********************\n\n");while(1){
printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t\tMAIN MENU");printf("\n--------------------------------------------------------------------------\n\n");printf("Choice No. Choice Name.\n\n");printf(" 1. Sender's Side\n\n 2. Receiver's Side\n\n 3. Exit\n\n\nEnter Your Choice : ");scanf("%d",&ans);switch(ans){
case 1:printf("\n------------------------------------------------------------------------\n");printf("\t\t\t Sender's Side");printf("\n-------------------------------------------------------------------------\n");sender();break;
case 2:printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t Receiver's Side");printf("\n-------------------------------------------------------------------------\n");receiver();
1
break;case 3:
exit(1);default:
printf("\n\nINVALID ENTRY!!!");}
}}
void receiver(){
int sum=0;printf("\nEnter the number of bits you want (Max 20) : ");scanf("%d",&j);printf("\n\nEnter %d bits : ",j);for(i=0;i<j;i++){
printf("\n\n %d bit: ",i+1);scanf("%d",&data[i]);if((data[i]!=0) && (data[i]!=1)){
printf("\nInvalid Bit......Enter only (1's and 0's) ");i--;
}}for(i=0;i<j;i++){
sum=sum+data[i];}printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t\tOutput");printf("\n-------------------------------------------------------------------------");printf("\n\nThe Number of 1's in the message is: %d",sum);if(sum%2==0){
printf("\n\nThere is no ERROR in the message.");}else{
printf("\n\nThere is ERROR in the message.");}
}
void sender(){
int sum=0;
2
printf("\nEnter the number of bits you want (Max 20) : ");scanf("%d",&j);printf("\nEnter %d bits : ",j);for(i=0;i<j;i++){
printf("\n\n %d bit: ",i+1);scanf("%d",&data[i]);if((data[i]!=0) && (data[i]!=1)){
printf("\nInvalid bit......Enter only (1's and 0's) ");i--;
}}for(i=0;i<j;i++){
sum=sum+data[i];}printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t\tOutput");printf("\n-------------------------------------------------------------------------");printf("\n\nThe Message sent was : ");for(i=0;i<j;i++){
printf("%d",data[i]);}if(sum%2!=0){
printf("\n\nParity Bit is : 1");data[j]=1;printf("\n\nThe Message after appending parity bit was : ");for(i=0;i<=j;i++){
printf("%d",data[i]);}
}else{
printf("\n\nParity Bit is : 0");data[j]=0;printf("\n\nThe Message after appending parity bit was : ");for(i=0;i<=j;i++){
printf("%d",data[i]);}
}}
3
OUTPUT:-
****************** VERTICAL REDUNDANCY CHECK *******************
------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Side
2. Receiver's Side
3. Exit
Enter Your Choice : 1
------------------------------------------------------------------------------------------------------------ Sender's Side------------------------------------------------------------------------------------------------------------
Enter the number of bits you want (Max 20) : 5
Enter 5 bits :
1 bit: 1
2 bit: 0
3 bit: 1
4 bit: 0
5 bit: 1
------------------------------------------------------------------------------------------------------------ Output
4
------------------------------------------------------------------------------------------------------------
The Message sent was : 10101
Parity Bit is : 1
The Message after appending parity bit was : 101011------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Side
2. Receiver's Side
3. Exit
Enter Your Choice : 2
------------------------------------------------------------------------------------------------------------ Receiver's Side------------------------------------------------------------------------------------------------------------
Enter the number of bits you want (Max 20) : 4
Enter 4 bits :
1 bit: 1
2 bit: 0
3 bit: 2
Invalid Bit......Enter only (1's and 0's)
3 bit: 0
4 bit: 1
------------------------------------------------------------------------------------------------------------
5
Output------------------------------------------------------------------------------------------------------------
The Number of 1's in the message is: 2There is no ERROR in the message.------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Side
2. Receiver's Side
3. Exit
Enter Your Choice : 3
6
-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("\n\n*********************** LONGITUDINAL REDUNDANCY CHECK ***********************\n\n");while(1){
printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t\tMAIN MENU");printf("\n--------------------------------------------------------------------------\n\n");printf("Choice No. Choice Name.\n\n");printf(" 1. Sender's Site\n\n 2. Receiver's Site\n\n 3. Exit\n\n\nEnter Your Choice : ");scanf("%d",&ans);switch(ans){
case 1:printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t Sender's Site");printf("\n-------------------------------------------------------------------------\n");sender();break;
case 2:
7
printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t Receiver's Site");printf("\n-------------------------------------------------------------------------\n");receiver();break;
case 3:exit(1);default:printf("\n\nINVALID 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("\n\nInvalid Bit......Enter another bit(Only 1's and 0's)!!!");j--;
}}
}for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(a[j][i]==1){
count++;}
}
8
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("%d\t",a[i][j]);}printf("\n\nThe Message after appending : \n");for(i=1;i<=m+1;i++){
for(j=1;j<=n;j++){
printf("%d\t",a[i][j]);}printf("\n\n");
}}
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("\n\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("\n\nInvalid Bit......Enter another bit(Only 1's and 0's)!!!");
9
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
OUTPUT:-
***************** LONGITUDINAL REDUNDANCY CHECK ***************
------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
3. Exit
Enter Your Choice : 1
------------------------------------------------------------------------------------------------------------ Sender's Site------------------------------------------------------------------------------------------------------------Enter the number of rows and columns max(5) : 2 3
Enter 2 rows and 3 columns :
Enter 1 row 3 bits : 1 0 0
Enter 2 row 3 bits : 1 0 1
----------------------------------------------- Output --------------------------------------------------
The parity row is :0 0 1
The Message after appending :1 0 0
11
1 0 1
0 0 1
------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
3. Exit
Enter Your Choice : 2
------------------------------------------------------------------------------------------------------------Receiver's Site
------------------------------------------------------------------------------------------------------------
Enter the number of rows and columns max(5) : 2 3
Enter 2 rows and 3 columns :
Enter 1 row 3 bits : 1 1 0
Enter 2 row 3 bits : 1 0 1
------------------------------------------------ Output -------------------------------------------------
Data is not correctly received...
12
------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
3. Exit
Enter Your Choice : 3
13
-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("\n\n--------------------------------------------------------------------------\n");printf("\t\t\t\tMAIN MENU");printf("\n--------------------------------------------------------------------------\n\n");printf("Choice No. Choice Name.\n\n");printf(" 1. Sender's Site\n\n 2. Receiver's Site\n\n 3. Exit\n\n\nEnter Your Choice : ");scanf("%d",&choice);
switch(choice){
case 1:printf("\n--------------------------- Sender's Site ----------------------------");
14
sender();break;
case 2:printf("\n------------------------- Receiver's Site ---------------");receiver();break;
case 3:exit(1);
}}
}
void sender(){
printf("\n\nEnter total number of data bits : ");scanf("%d",&n);printf("\nEnter %d data bits : \n\n",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 1's & 0's!!!\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 : \n\n");for(j=0;j<g;j++){
printf("\nEnter key bit %d : ",j+1);scanf("%d",&d[j]);
15
if((d[j]!=1)&& (d[j]!=0)){
printf("\nInvalid Entry...Enter only 1's & 0's!!!\n");j--;
}}
}while(d[0]!=1);printf("\n------------------------------------------------------------------\n");printf("\t\tOutput ");printf("\n------------------------------------------------------------------\n");
a1=n+(g-1); printf("\n\nThe 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("\n\nThe Divisor : \t");for(j=0;j<g;j++){
printf("%d",d[j]);}printf("\n\nIntermediate CRC : ");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{
16
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("\n\nThe 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("\n\nEnter total number of data bits: ");scanf("%d",&n);printf("\nEnter %d data bits : \n\n",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 1's & 0's!!!\n");i--;
}}printf("\nEnter size of Key (! >databits): ");scanf("%d",&g);if(g>n){
printf("\nInvalid Entry...Enter value less than databit!!!\n");
17
printf("\nEnter size of Key (! >databits): ");scanf("%d",&g);
}do{
printf("\nEnter Key values : \n\n");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 1's & 0's!!!\n");j--;
}}
}while(d[0]!=1);printf("\n-------------------------------------------------------------------------------\n");printf("\t\tOutput");printf("\n--------------------------------------------------------------------------------\n");
printf("\n\nThe Message : \t");for(i=0;i<n;++i){
p[i]=a[i];}for(i=0;i<n;i++){
printf("%d",p[i]);}printf("\n\nThe Divisor : \t");for(j=0;j<g;j++){
printf("%d",d[j]);}a1=n+(g-1);printf("\n\nCRC's : ");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]);
}
18
}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("\n\nThe 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("\n\nThe Message is received correctly");}else{
printf("\n\nThe Message is not received correctly");}
}
19
Output:-
********************** CYCLIC REDUNCDANCY CHECK ****************
------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
3. Exit
Enter Your Choice : 1
----------------------------------------------- Sender's Site -------------------------------------------
Enter total number of data bits : 5
Enter 5 data bits :
Enter bit 1: 1
Enter bit 2: 0
Enter bit 3: 1
Enter bit 4: 1
Enter bit 5: 0
20
Enter size of Key (! >databits) : 3
Enter Key values :
Enter key bit 1 : 1
Enter key bit 2 : 1
Enter key bit 3 : 0
------------------------------------------------------------------------------------------------------------ Output------------------------------------------------------------------------------------------------------------
The Message : 1011000
The Divisor : 110
Intermediate CRC : 011 001 010 010 010
The Final CRC Bit is : 10
The Message transmitted is : 1011010
------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
3. Exit
Enter Your Choice : 2
---------------------------------------------- Receiver's Site -----------------------------------------
Enter total number of data bits: 5
Enter 5 data bits :
21
Enter bit 1 : 1
Enter bit 2 : 1
Enter bit 3 : 0
Enter bit 4 : 1
Enter bit 5 : 1
Enter size of Key (! >databits): 3
Enter Key values :
Enter key bit 1 : 1
Enter key bit 2 : 0
Enter key bit 3 : 1
------------------------------------------------------------------------------------------------------------ Output------------------------------------------------------------------------------------------------------------
The Message : 11011
The Divisor : 101
CRC's : 011 010 000 000 000
The Final CRC is : 00
The Message is received correctly
------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
3. Exit
22
Enter 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];static int carry=0;int n,i,count=0;void main(){int choice;clrscr();printf("\n*********************************** CHECKSUM *********************************\n");while(1)
{printf("\n\n--------------------------------------------------------------------------\n");printf("\t\t\t\tMAIN MENU");printf("\n--------------------------------------------------------------------------\n\n");printf("Choice No. Choice Name.\n\n");printf(" 1. Sender's Site\n\n 2. Receiver's Site\n\n 3. Exit\n\n\nEnter Your Choice : ");scanf("%d",&choice);
switch(choice){
case 1:printf("------------------------------------------------------------------------\n");
23
printf("\n\t\t Sender's Site ");printf("\n------------------------------------------------------------------------\n");sender();break;
case 2:printf("------------------------------------------------------------------------\n");printf("\n\t\tReceiver's Site ");printf("\n------------------------------------------------------------------------\n");receiver();break;
case 3:exit(1);
default:printf("\n\nINVALID ENTRY");
}}
}
void sender(){
printf("\nEnter the total number of bits : ");scanf("%d",&n);printf("\nEnter %d bits of Data 1 : \n\n",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("\n\nInvalid Bit......Enter another value(only 1's and 0's)!!!!!");i--;
}}printf("\n------------------------------------------------------------------\n");printf("\nEnter %d bits of Data 2 : \n\n",n);for(i=0;i<n;i++){
24
printf("\nEnter bit %d: ",i+1);scanf("%d",&b[i]);if(b[i]!=0 && b[i]!=1){
printf("\n\nInvalid Bit......Enter another value(only 1's and 0's)!!!!!");i--;
}}printf("\n-----------------------------------------------------------------------------\n");printf("\n\t\tOutput");printf("\n-----------------------------------------------------------------------------\n");printf("\n\nData 1:\t\t\t ");for(i=0;i<n;i++){
printf("%d",a[i]);}printf("\n\nData 2:\t\t\t ");for(i=0;i<n;i++){
printf("%d",b[i]);}for(i=n;i>=0;i--){
r[i]=sum(a[i],b[i]);}printf("\n\nResult :\t\t ");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("\n\n1's Compliment of Result :");for(i=0;i<n;i++){
printf("%d",r[i]);
25
}}
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;
}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
26
return(1);}
void receiver(){
printf("\nEnter the total number of bits : ");scanf("%d",&n);printf("\nEnter %d bits of Data 1 : \n\n",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("\n\nInvalid Bit......Enter another value(only 1's and 0's)!!!!!");i--;
}}printf("\n------------------------------------------------------------------\n");printf("\nEnter %d bits of Data 2 : \n\n",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("\n\nInvalid Bit......Enter another value(only 1's and 0's)!!!!!");i--;
}}printf("\n------------------------------------------------------------------\n");printf("\nEnter %d bits of Checksum : \n\n",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("\n\nInvalid Bit......Enter another value(only 1's and 0's)!!!!!");i--;
}}printf("\n----------------------------------------------------------------------------------");
27
printf("\n\t\tOutput");printf("\n----------------------------------------------------------------------------------");printf("\n\nData 1:\t\t\t ");for(i=0;i<n;i++){
printf("%d",a[i]);}printf("\n\nData 2:\t\t\t ");for(i=0;i<n;i++){
printf("%d",b[i]);}printf("\n\nChecksum:\t\t ");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("\n\nResult :\t\t ");for(i=0;i<n;i++){
printf("%d",r1[i]);}for(i=0;i<n;i++){
if (r1[i]==0){
r1[i]=1;}else{
r1[i]=0;}
}printf("\n\n1's Compliment of Result :");for(i=0;i<n;i++){
printf("%d",r1[i]);}for(i=0;i<n;i++){
if(r1[i]==0){
28
count=count+1;}
}if(count==n){
printf("\n\nData is ERROR free, hence ACCEPTED... ");}else{
printf("\n\nData 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);
}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)
29
{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);
}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);
30
}
Output:-
****************************** CHECKSUM *****************************
------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
3. Exit
Enter Your Choice : 1------------------------------------------------------------------------------------------------------------ Sender's Site------------------------------------------------------------------------------------------------------------
Enter the total number of bits : 5
Enter 5 bits of Data 1 :
Enter bit 1: 1
Enter bit 2: 0
Enter bit 3: 1
Enter bit 4: 1
31
Enter bit 5: 0
------------------------------------------------------------------------------------------------------------
Enter 5 bits of Data 2 :
Enter bit 1: 0
Enter bit 2: 1
Enter bit 3: 0Enter bit 4: 0
Enter bit 5: 1
------------------------------------------------------------------------------------------------------------ Output------------------------------------------------------------------------------------------------------------
Data 1: 10110
Data 2: 01001
Result : 11111
1's Compliment of Result :00000
------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
3. Exit
Enter Your Choice : 2
------------------------------------------------------------------------------------------------------------ Receiver's Site------------------------------------------------------------------------------------------------------------
32
Enter the total number of bits : 5
Enter 5 bits of Data 1 :
Enter bit 1: 0
Enter bit 2: 0
Enter bit 3: 0
Enter bit 4: 0
Enter bit 5: 1------------------------------------------------------------------------------------------------------------
Enter 5 bits of Data 2 :
Enter bit 1: 1
Enter bit 2: 2
Invalid Bit......Enter another value(only 1's and 0's)!!!!!
Enter bit 2: 1
Enter bit 3: 1
Enter bit 4: 1
Enter bit 5: 0
------------------------------------------------------------------------------------------------------------
Enter 5 bits of Checksum :
Enter bit 1: 0
Enter bit 2: 0
Enter bit 3: 0
Enter bit 4: 0
33
Enter bit 5: 0
------------------------------------------------------------------------------------------------------------ Output------------------------------------------------------------------------------------------------------------
Data 1: 00001
Data 2: 11110
Checksum: 00000
Result : 11111
1's Compliment of Result :00000
Data is ERROR free, hence ACCEPTED...
------------------------------------------------------------------------------------------------------------ MAIN MENU------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
3. Exit
Enter Your Choice : 3
34
-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("\n\n*********************** HAMMING CODE ***********************\n\n");while(1){
printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t\t\nMAIN MENU");printf("\n--------------------------------------------------------------------------\n\n");printf("Choice No. Choice Name.\n\n");printf(" 1. Sender's Site\n\n 2. Receiver's Site\n\n 3. Exit\n\n\nEnter Your Choice : ");scanf("%d",&ans);switch(ans){
case 1:printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t Sender's Site");
35
printf("\n-------------------------------------------------------------------------\n");sender();break;
case 2:printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t Receiver's Site");printf("\n-------------------------------------------------------------------------\n");receiver();break;
case 3:exit(1);
default:printf("\n\nINVALID 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)
{
36
scanf("%d",&data[i]);if (data[i]!=0&&data[i]!=1){
printf("Invalid Entry!!! Enter only 0's and 1's");i--;
}}
}printf("\nData Is : ");for(i=1;i<=m+r;i++){
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];
37
if(count%2==0)data[a]=0;
elsedata[a]=1;
}}printf("\n\n-----------------------------------------------------------------------------\n");printf("\t\t\tOutput");printf("\n--------------------------------------------------------------------------------\n");printf("\n\nNew Data To Be Sent Is : ");for(i=1;i<=m+r;i++)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 0's and 1's");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;
38
}}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{
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("\n\n-----------------------------------------------------------------------------\n");printf("\t\tOutput\n");printf("\n--------------------------------------------------------------------------------\n");if(count1>0){
if(count1>m){
39
printf("\nError Is At Position %d",count1);printf("\n\nOOPS!!! Position is not available\n");
}else{
printf("\nError Is At Position %d",count1);if(data[count1]==0)
data[count1]=1;else
data[count1]=0;printf("\nCorrect Code Word : ");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]);}
}}
40
Output:-
***************************** HAMMING CODE *************************
------------------------------------------------------------------------------------------------------------MAIN MENU
------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
3. Exit
Enter Your Choice : 1
------------------------------------------------------------------------------------------------------------Sender's Site
------------------------------------------------------------------------------------------------------------
Enter the number of data bits (MAX 11) : 7
The Parity Bits are : 4
Enter 7 data bits : 1 0 0 1 0 1 1
Data Is : 4 4 1 4 0 0 1 4 0 1 1
41
------------------------------------------------------------------------------------------------------------Output
------------------------------------------------------------------------------------------------------------
New Data To Be Sent Is : 1 0 1 1 0 0 1 0 0 1 1
------------------------------------------------------------------------------------------------------------MAIN MENU
------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
3. Exit
Enter Your Choice : 2
------------------------------------------------------------------------------------------------------------Receiver's Site
------------------------------------------------------------------------------------------------------------
Enter the number of data bits : 11
Enter 11 data bits : 1 0 1 1 0 0 1 0 1 1 1
------------------------------------------------------------------------------------------------------------Output
------------------------------------------------------------------------------------------------------------
Error Is At Position 9
Correct Code Word : 1 0 1 1 0 0 1 0 0 1 1
------------------------------------------------------------------------------------------------------------MAIN MENU
------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
42
3. Exit
Enter 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("\n\n--------------------------------------------------------------------------\n");printf("\t\t\t\tMAIN MENU");printf("\n--------------------------------------------------------------------------\n\n");printf("Choice No. Choice Name.\n\n");printf(" 1. Sender's Site\n\n 2. Receiver's Site\n\n 3. Exit\n\n\nEnter Your Choice : ");scanf("%d",&choice);switch(choice){
case 1:
43
printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t Sender's Site");printf("\n-------------------------------------------------------------------------\n");sender();break;
case 2:printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t Receiver's 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("\n\nThe Original Data entered : ");for(i=0;i<n;i++){
printf("%d ",data[i]);}i=0;while(i<n){
if(data[i]==1){
44
i++;count++;if(count==5){
for(j=n+1;j>i;j--){
data[j]=data[j-1];}data[i]=0;
}}else{
i++;count=0;
}}printf("\n\n--------------------------------------------------------------------------\n");printf("\t\t\t Output\n");printf("-------------------------------------------------------------------------------\n");printf("\n\nData 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("\n\nThe Original Data entered : ");for(i=0;i<n;i++){
printf("%d ",data[i]);}i=0;while(i<n){
if(data[i]==1){
45
i++;count++;if(count==5){
for(j=i;j<n+1;j++){
data[j]=data[j+1];
}n--;
}}else{
i++;count=0;
}}printf("\n\n--------------------------------------------------------------------------------");printf("\n\t\t\t Output");printf("\n--------------------------------------------------------------------------------\n");printf("\nData After De-Stuff : ");for(i=0;i<n;i++){
printf("%d ",data[i]);}
}
46
Output:-
****************************** BIT STUFFING **************************
------------------------------------------------------------------------------------------------------------MAIN MENU
------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
3. Exit
Enter Your Choice : 1
------------------------------------------------------------------------------------------------------------Sender's Site
------------------------------------------------------------------------------------------------------------
Enter the length of your code : 11
Enter 1 bit : 0
47
Enter 2 bit : 1
Enter 3 bit : 1
Enter 4 bit : 1
Enter 5 bit : 1
Enter 6 bit : 1
Enter 7 bit : 1
Enter 8 bit : 0
Enter 9 bit : 1
Enter 10 bit : 1
Enter 11 bit : 0
The 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. Sender's Site
2. Receiver's Site
3. Exit
Enter Your Choice : 2
------------------------------------------------------------------------------------------------------------Receiver's Site
------------------------------------------------------------------------------------------------------------
48
Enter the length of your code : 11
Enter 1 bit : 0
Enter 2 bit : 1
Enter 3 bit : 1
Enter 4 bit : 1
Enter 5 bit : 1
Enter 6 bit : 1
Enter 7 bit : 0
Enter 8 bit : 0
Enter 9 bit : 1
Enter 10 bit : 0
Enter 11 bit : 1
The 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
------------------------------------------------------------------------------------------------------------MAIN MENU
------------------------------------------------------------------------------------------------------------
Choice No. Choice Name.
1. Sender's Site
2. Receiver's Site
3. Exit
Enter Your Choice : 3
49
-7- WALSH MATRIX
#include<stdio.h>#include<conio.h>#include<stdlib.h>void stn1();void stn2();void stn4();int n,i,j;void main(){
clrscr();printf("\n***************** CHIPPING SEQUENCE USING WALSH MATRIX *********************\n\n");while(1){
printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t\tMAIN MENU");printf("\n--------------------------------------------------------------------------\n\n");printf("Choice No. Station No.\n\n");printf(" 1. Station 1\n\n 2. Station 2\n\n 3. Station 4\n\n 4. Exit\n\nEnter Your Choice : ");
50
scanf("%d",&n);switch(n){
case 1:printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t Station 1");printf("\n-------------------------------------------------------------------------\n");stn1();break;
case 2:printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t Station 2");printf("\n-------------------------------------------------------------------------\n");stn2();break;
case 3:printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t Station 4");printf("\n-------------------------------------------------------------------------\n");stn4();break;
case 4:exit(1);
default:printf("\n\nInvalid Entry!!!");
}}
}
void stn1(){
int w1;printf("\nYour Code For Station 1 : 1\n");
}
51
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 : \n\n");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;}else if(j==0){
w4[i][j]=1;}else if((i==1)&&(j==1)){
52
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 : \n\n");for(i=0;i<4;i++){
for(j=0;j<4;j++){
printf("%d\t",w4[i][j]);}printf("\n");
}}
53
Output:-
************** CHIPPING SEQUENCE USING WALSH MATRIX ***********
------------------------------------------------------------------------------------------------------------MAIN MENU
------------------------------------------------------------------------------------------------------------
Choice No. Station No.
1. Station 1
2. Station 2
3. Station 4
4. Exit
Enter Your Choice : 1
54
------------------------------------------------------------------------------------------------------------Station 1
------------------------------------------------------------------------------------------------------------
Your Code For Station 1 : 1
------------------------------------------------------------------------------------------------------------MAIN MENU
------------------------------------------------------------------------------------------------------------
Choice No. Station No.
1. Station 1
2. Station 2
3. Station 4
4. Exit
Enter Your Choice : 5
Invalid Entry!!!
------------------------------------------------------------------------------------------------------------MAIN MENU
------------------------------------------------------------------------------------------------------------
Choice No. Station No.
1. Station 1
2. Station 2
3. Station 4
4. Exit
Enter Your Choice : 2
------------------------------------------------------------------------------------------------------------Station 2
------------------------------------------------------------------------------------------------------------
55
Your Code For Station 2 :
1 11 -1
------------------------------------------------------------------------------------------------------------MAIN MENU
------------------------------------------------------------------------------------------------------------
Choice No. Station No.
1. Station 1
2. Station 2
3. Station 4
4. Exit
Enter Your Choice : 3
------------------------------------------------------------------------------------------------------------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
56
3. Station 4
4. Exit
Enter Your Choice : 4
-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("\n\nEnter the length of Data ( <= 20 ) : ");scanf("%d",&n);printf("\n\nEnter %d Bits : \n",n);for(i=1;i<=n;i++){
printf("\n\nEnter Bit %d:",i);scanf("%d",&a[i]);
}printf("\n\nOriginal Data : ");
57
for(i=1;i<=n;i++){
printf("%d ",a[i]);}cnt1=0;cnt2=0;printf("\n\n-----------------------------------------------------------------------------\n");printf("\t\t\t Output");printf("\n-------------------------------------------------------------------------------\n");printf("\nThe Data Contains : \n");for(i=1;i<=n;i++){
if(a[i]==1){
if(cnt2!=0){
printf("\n\n 0's : %d",cnt2);cnt2=0;
}cnt1++;
}if(a[i]==0){
if(cnt1!=0){
printf("\n\n 1's : %d",cnt1);cnt1=0;
}cnt2++;
}}if(cnt1==0)
printf("\n\n 0's : %d",cnt2);if(cnt2==0)
printf("\n\n 1's : %d",cnt1);getch();
}
58
Output:-
************************** BIT COMPRESSION **************************
Enter the length of Data : 11
Enter 11 Bits :
Enter Bit 1 : 1
Enter Bit 2 : 1
Enter Bit 3 : 0
Enter Bit 4 : 0
Enter Bit 5 : 0
Enter Bit 6 : 0
59
Enter Bit 7 : 1
Enter Bit 8 : 1
Enter Bit 9 : 0
Enter Bit 10 : 1
Enter Bit 11 :1
Original Data : 1 1 0 0 0 0 1 1 0 1 1
------------------------------------------------------------------------------------------------------------Output
------------------------------------------------------------------------------------------------------------The Data Contains :
1's : 2
0's : 4
1's : 2
0's : 1
1's : 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("\n\n--------------------------------------------------------------------------\n");
60
printf("\t\t\t\tMAIN MENU");printf("\n--------------------------------------------------------------------------\n\n");printf("Choice No. Choice Name.\n\n");printf(" 1. Sender's Site\n\n 2. Receiver's Site\n\n 3. Exit\n\n\nEnter Your Choice : ");scanf("%d",&choice);switch(choice){
case 1:printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t Sender's Site");printf("\n-------------------------------------------------------------------------\n");sender();break;
case 2:printf("\n-------------------------------------------------------------------------\n");printf("\t\t\t Receiver's Site");printf("\n-------------------------------------------------------------------------\n");receiver();break;
case 3:exit(1);
default:printf("\n\nInvalid 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);
61
printf("\n-----------------------------------------------------------------------\n\n");printf("\nPlain Text : ");printf("%s",str);printf("\n\nCipher 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);
}
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-----------------------------------------------------------------------\n\n");printf("\nCipher Text : ");printf("%s",ptr);printf("\n\nPlain 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
62
{str1[i]=ptr[i];
}}str1[i]='\0';printf("%s",str1);
}
Output:-
********************* MONOALPHABETIC ENCRYPTION **************** ------------------------------------------------------------------------------------------------------------
MAIN MENU------------------------------------------------------------------------------------------------------------ Choice No. Choice Name. 1. Sender's Site 2. Receiver's Site 3. Exit
63
Enter Your Choice : 1 ------------------------------------------------------------------------------------------------------------
Sender's Site------------------------------------------------------------------------------------------------------------ Enter the Plain Text : TARANNUM Enter the Charter difference : 5 ------------------------------------------------------------------------------------------------------------ Plain Text : TARANNUM Cipher Text After Encryption : YFWFSSZR ------------------------------------------------------------------------------------------------------------
MAIN MENU------------------------------------------------------------------------------------------------------------ Choice No. Choice Name. 1. Sender's Site 2. Receiver's Site 3. Exit
Enter Your Choice : 2 ------------------------------------------------------------------------------------------------------------
Receiver's Site------------------------------------------------------------------------------------------------------------ Enter the Cipher text : yfwfsszr Enter the Charter difference : 5 ------------------------------------------------------------------------------------------------------------ Cipher Text : yfwfsszr Plain text After Decryption : tarannum
64
------------------------------------------------------------------------------------------------------------ MAIN MENU
------------------------------------------------------------------------------------------------------------ Choice No. Choice Name. 1. Sender's Site 2. Receiver's Site 3. Exit
Enter 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("\n\t\t\t\t VIGENERE TABLE \n\n");printf("------------------------------------------------------------------------------\n\n");
65
while (j <= 26){
for (i=j; i<j+26; i++){
if (k >90)k = k - 26;
printf("%c ",k );k++;
}printf("\n");j++;k++;
}printf("\n----------------------------------------------------------------------------\n\n");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("\n\nPlain Text : ");printf("%s",input);printf("\n\nCipher 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();
66
}
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 Z B C D E F G H I J K L M N O P Q R S T U V W X Y Z A C D E F G H I J K L M N O P Q R S T U V W X Y Z A B D E F G H I J K L M N O P Q R S T U V W X Y Z A B C E F G H I J K L M N O P Q R S T U V W X Y Z A B C D F G H I J K L M N O P Q R S T U V W X Y Z A B C D E G H I J K L M N O P Q R S T U V W X Y Z A B C D E F
67
H I J K L M N O P Q R S T U V W X Y Z A B C D E F G I J K L M N O P Q R S T U V W X Y Z A B C D E F G H J K L M N O P Q R S T U V W X Y Z A B C D E F G H I K L M N O P Q R S T U V W X Y Z A B C D E F G H I J L M N O P Q R S T U V W X Y Z A B C D E F G H I J K M N O P Q R S T U V W X Y Z A B C D E F G H I J K L N O P Q R S T U V W X Y Z A B C D E F G H I J K L M O P Q R S T U V W X Y Z A B C D E F G H I J K L M N P 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 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 P R S T U V W X Y Z A B C D E F G H I J K L M N O P Q S T U V W X Y Z A B C D E F G H I J K L M N O P Q R T U V W X Y Z A B C D E F G H I J K L M N O P Q R S U V W X Y Z A B C D E F G H I J K L M N O P Q R S T V W X Y Z A B C D E F G H I J K L M N O P Q R S T U W X Y Z A B C D E F G H I J K L M N O P Q R S T U V X Y Z A B C D E F G H I J K L M N O P Q R S T U V W Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Z 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 : HIWORLD Enter Encryption Key : ME ------------------------------------------------------------------------------------------------------------
KEY : MEMEMEM Plain Text : HIWORLD Cipher Text : TMISDPP
68
-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;
}
69
flg = 0;}
void encrypt(){
int i;c = 1;for(i=0;i<e;i++)
c=c*m%n;c=c%n;
printf("\nEncrypted Plain Text : %d",c);printf("\n\n----------------------------------------------------------------------\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 *****************************\n\n");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("\n\nEnter value for e : ");scanf("%d",&e);check();
}while(flg==1);d = 1;do{
s = (d*e)%phi;d++;
70
}while(s!=1);d = d-1;printf("\n-----------------------------------------------------------------------------\n\n");printf("\nPublic Key\t: {%d,%d}",e,n);printf("\n\nPrivate Key\t: {%d,%d}",d,phi);printf("\n\n---------------------------------------------------------------------------\n\n");printf("\nEnter The Plain Text : ");scanf("%d",&m);encrypt();printf("\n\nEnter the Cipher text : ");scanf("%d",&c);decrypt();getch();
}
Output:-
**************************** RSA ALGORITHM ************************* Enter Two Relative Prime Numbers : 7 11 F(n) = 60 Enter value for e : 13
------------------------------------------------------------------------------------------------------------ Public Key : {13,77} Private Key : {37,60}
71
------------------------------------------------------------------------------------------------------------ Enter The Plain Text : 7 Encrypted Plain Text : 35 ------------------------------------------------------------------------------------------------------------ Enter the Cipher text : 35 Decrypted 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 9999
struct node{
int predecessor;int dist;int status;
};
72
int adj[MAX][MAX];int n;void main(){
int i,j;int source,dest;int path[MAX];int shortdist,count;clrscr();printf("************************ DIJKSTRA'S 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 : %d\n", 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 destination node\n");
}getch();
}
create_graph(){
int i,max_edges,origin,destin,wt;
73
printf("Enter number of vertices : ");scanf("%d",&n);max_edges=n*(n-1);
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;
74
state[s].dist = 0;state[s].status = PERM;current=s;while(current!=d){
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);
}
75
Output:-
********************** DIJKSTRA'S SHORTEST PATH *******************
Enter number of vertices : 6
Enter edge 1(0 0 to quit) : 1 2
Enter weight for this edge : 6
Enter edge 2(0 0 to quit) : 2 5
Enter weight for this edge : 3
Enter edge 3(0 0 to quit) : 2 6
76
Enter weight for this edge : 2
Enter edge 4(0 0 to quit) : 3 4
Enter weight for this edge : 2
Enter edge 5(0 0 to quit) : 3 6
Enter weight for this edge : 3
Enter edge 6(0 0 to quit) : 4 6
Enter weight for this edge : 1
Enter edge 7(0 0 to quit) : 5 4
Enter weight for this edge : 4
Enter edge 8(0 0 to quit) : 6 5
Enter weight for this edge : 3
Enter edge 9(0 0 to quit) : 0 0
The 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 0
Enter source node(0 to quit) : 1
Enter destination node(0 to quit) : 6
Shortest distance is : 8
Shortest Path is : 1->2->6
77
Enter source node(0 to quit) : 2
Enter destination node(0 to quit) :4
Shortest distance is : 7
Shortest Path is : 2->5->4
Enter source node(0 to quit) : 0
Enter 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 9999
struct node{
78
int predecessor;int dist;int status;
};
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 PRIM'S ALGORITHM ********\n");create_graph();printf("\nAdjacency matrix is : \n\n");display();
count = maketree(tree,&wt_tree);
printf("\nWeight of spanning tree is : %d\n", wt_tree);printf("\nEdges to be included in spanning tree are : \n\n");for(i=1;i<=count;i++){
printf("%d->",tree[i].u);printf("%d\n",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;
79
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;adj[destin][origin]=wt;
}}if(i<n-1){
printf("\nSpanning tree is not possible\n");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++)
80
{state[i].predecessor=0;state[i].dist = infinity;state[i].status = TEMP;
}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] )
81
{int i;for(i=1;i<=n;i++) if( state[i].status == TEMP ) return FALSE;return TRUE;
}
Output:-
******** MINIMUM SPANNING TREE FROM PRIM'S ALGORITHM ********
Enter number of vertices : 7
Enter edge 1(0 0 to quit) : 1 2
Enter weight for this edge : 3
Enter edge 2(0 0 to quit) : 1 3
Enter weight for this edge : 10
Enter edge 3(0 0 to quit) : 1 4
82
Enter weight for this edge : 2
Enter edge 4(0 0 to quit) : 1 5
Enter weight for this edge : 4
Enter edge 5(0 0 to quit) : 2 5
Enter weight for this edge : 7
Enter edge 6(0 0 to quit) : 3 4
Enter weight for this edge : 12
Enter edge 7(0 0 to quit) : 3 6
Enter weight for this edge : 15
Enter edge 8(0 0 to quit) : 4 5
Enter weight for this edge : 6
Enter edge 9(0 0 to quit) : 4 6
Enter weight for this edge : 4
Enter edge 10(0 0 to quit) : 5 6
Enter weight for this edge : 5
Enter edge 11(0 0 to quit) : 5 7
Enter weight for this edge : 2
Enter edge 12(0 0 to quit) : 6 7
Enter weight for this edge : 3
Enter edge 13(0 0 to quit) : 0 0
Adjacency matrix is :
0 3 10 2 4 0 0 3 0 0 0 7 0 010 0 0 12 0 15 0
83
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
Weight of spanning tree is : 24
Edges to be included in spanning tree are :
1->41->21->55->77->61->3
84