0% found this document useful (0 votes)
29 views

CN All Programs

Uploaded by

Prabhas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

CN All Programs

Uploaded by

Prabhas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

1.

(a)
#include<stdio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
printf("Enter frame length:");
scanf("%d",&n);
printf("Enter input frame (0's & 1's only):");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
i=0; count=1; j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1;a[k]==1 && k<n && count<5;k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}
i=k;
}}
else
{
b[j]=a[i];
}
i++;
j++;
}
printf("After stuffing the frame is:");
for(i=0;i<j;i++)
printf("%d",b[i]);
}
Out Put:
Enter frame length:6
Enter input frame (0's & 1's only):1
1
1
1
1
1
After stuffing the frame is:1111101

...Program finished with exit code 0


Press ENTER to exit console.

1.(b)

#include<stdio.h>
#include<string.h>
#include<process.h>
void main()
{
int i=0,j=0,n,pos;
char a[20],b[50],ch;
printf("Enter string\n");
scanf("%s",&a);
n=strlen(a);
printf("Enter position\n");
scanf("%d",&pos);
if(pos>n)
{
printf("invalid position, Enter again :");
scanf("%d",&pos);}
printf("Enter the character\n");
ch=getche();
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
b[5]='x';
j=6;
while(i<n)
{
if(i==pos-1)
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]=ch;
b[j+4]='d';
b[j+5]='l';
b[j+6]='e';
j=j+7;
}
if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
j=j+3;
}
b[j]=a[i];
i++;
j++;
}
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='e';
b[j+4]='t';
b[j+5]='x';
b[j+6]='\0';
printf("\nframe after stuffing:\n");
printf("%s",b);
}

Enter string
SVCETA
Enter position
2
Enter the character
frame after stuffing:
dlestxSdldleVCETAdleetx

(2)
#include<stdio.h>
#include<stdlib.h>

int print(int *a,int n)


{
int i;
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf("\n");
return (0);
}

int crc(int *g,int *q,int *r,int ng,int nq)


{
int i,j,k;

k = 0;

for(i=0;i<nq;i++)
{
if(r[i]==0)
{

for(j=i;j<(i+ng);j++)
{
r[j]=r[j]^0;
}

q[i]=0;
}
else
{
for(j=i;j<(i+ng);j++)
{
r[j]=r[j]^g[k];
k++;
}
q[i]=1;
k=0;
}
}

return (0);
}

main()
{
int *gx,*tx,*q,*r;
int i,j,nt,ng,nq,n,flag=1;

printf("\n Enter no. of bits in message to be transmitted:");


scanf("%d",&nt);
printf("\n Enter no. of bits in G(x) :");
scanf("%d",&ng);

n=nt+ng-1;
nq=nt;
gx=malloc(sizeof(int)*ng);
tx=malloc(sizeof(int)*n);
r=malloc(sizeof(int)*n);
q=calloc(nq,sizeof(int));

printf("\n Enter message :");


for(i=0;i<nt;i++)
{
scanf("%d",&tx[i]);
r[i]=tx[i];
}
for(;i<n;i++)
{
r[i]=0;
}

printf("\n Enter G(x) :");


for(i=0;i<ng;i++)
{
scanf("%d",&gx[i]);
}

/* *****AT TRANSMITTER***** */
printf("\n CRC at transmitter :");
printf("\n Message to be transmitted :");
print(tx,nt);
printf("\n G(x)=");
print(gx,ng);
printf("\n Message with '0' appended :");
print(r,n);

crc(gx,q,r,ng,nq);

printf("\n Quotient at transmitter:");


print(q,nq);
printf("\n Remainder at transmitter :");
print(r,n);

for(i=0;i<nt;i++)
{
r[i]=tx[i];
}

printf("\n Transmitted message :");


print(r,n);

/* ***** AT RECEIVER***** */
printf("\n CRC at receiver :");
printf("\n Message received :");
print(r,n);
printf("\n G(x)=");
print(gx,ng);

crc(gx,q,r,ng,nq);

printf("\n Quotient at receiver :");


print(q,nq);
printf("\n Remainder at receiver :");
print(r,n);

for(i=0;i<n;i++)
{
if(r[i]!=0)
{
flag=0;
break;
}
}

if(flag)
printf("\n No error detected -->CRC algorithm implemented
successfully.");
else
printf("\n Error detected.");

getch();

Sample Input and Output

Enter no. of bits in message to be transmitted:8

Enter no. of bits in G(x) :5

Enter message :0 1 1 0 1 1 0 1

Enter G(x) :1 0 1 0 1

CRC at transmitter :
Message to be transmitted :01101101
G(x)=10101

Message with '0' appended :011011010000

Quotient at transmitter:01110111

Remainder at transmitter :000000001011

Transmitted message :011011011011

CRC at receiver :
Message received :011011011011

G(x)=10101

Quotient at receiver :01110111

Remainder at receiver :000000000000

No error detected -->CRC algorithm implemented successfully.

(3)
#include<stdio.h>

int main()
{

int w,i,f,frames[50];

printf("Enter window size: ");

scanf("%d",&w);

printf("\nEnter number of frames to transmit: ");

scanf("%d",&f);

printf("\nEnter %d frames: ",f);

for(i=1;i<=f;i++)

scanf("%d",&frames[i]);
printf("\nWith sliding window protocol the frames will be sent in the following manner (assuming
no corruption of frames)\n\n");

printf("After sending %d frames at each stage sender waits for acknowledgement sent by the
receiver\n\n",w);

for(i=1;i<=f;i++)

if(i%w==0)

printf("%d\n",frames[i]);

printf("Acknowledgement of above frames sent is received by sender\n\n");

else

printf("%d ",frames[i]);

if(f%w!=0)

printf("\nAcknowledgement of above frames sent is received by sender\n");

return 0;

Output:

Enter window size: 3


Enter number of frames to transmit: 5
Enter 5 frames: 5 6 7 8 9
With sliding window protocol the frames will be sent in the following manner
(assuming no corruption of frames)
After sending 3 frames at each stage sender waits for acknowledgement sent by the
receiver
567
Acknowledgement of above frames sent is received by sender
89
Acknowledgement of above frames sent is received by sender

(4)

#include<conio.h>
#include<stdio.h>
static int dsp[10][10],nodes,perm,tem;
struct
{
char src;
char dest;
int length;
} stemp,permanent[10]={' ',' ',0},temp[10]={' ',' ',-1};
void sort()
{
int i,j,k;
for(i=0;i<=tem;i++)
{
k=1;
for(j=0;j<=tem;j++)
{
if((temp[j].length<=temp[j+1].length))
{
stemp=temp[j];
temp[j]=temp[j+1];
temp[j+1]=stemp;
k=0;
}
}
if(k)
break;
}
permanent[perm++]=temp[tem-1];
temp[tem-1].src=' ';
temp[tem-1].dest=' ';
temp[tem-1].length=-1;
tem--;
}
void main()
{
int i,j,k,l,m,n=0,point;
char initial,dest,path[10]={' '};
clrscr();
printf("\t\t Shortest path dijkstra's algothrim");
printf("\n **************************************\n");
printf("Enter the number of nodes:\n");
scanf("%d",&nodes);
printf("\nEnter the adjacency matrix");
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
scanf("%d",&dsp[i][j]);
}
fflush(stdin);
printf("\n Enter the source node");
scanf("%c",&initial);
fflush(stdin);
printf("\n Enter the destination node\n");
scanf("%c",&dest);
permanent[perm].src=initial;
permanent[perm].dest=initial;
permanent[perm++].length=0;
i=permanent[perm-1].dest-97;
for(j=0;j<nodes;j++)
{
if(i!=j)
{
if(dsp[i][j]>0)
{
temp[tem].src=permanent[perm-1].src;
temp[tem].dest=j+97;
temp[tem++].length=dsp[i][j];
}
}
}
sort();
while(tem>=0)
{
j=permanent[perm-1].dest-97;
for(i=0;i<nodes;i++)
{
if(i!=initial-97)
{
if(dsp[j][i]>0)
{
l=-1;
for(k=0;k<perm;k++)
{
if(permanent[k].dest==(i+97))
l=k;
}

for(k=0;k<=tem;k++)
{
if(temp[k].dest==(i+97))
l=k;
}
if(l<0)
{
temp[tem].src=j+97;
temp[tem].dest=i+97;
for(m=0;m<perm;m++)
{
if(permanent[m].dest==temp[tem].src)
n=permanent[m].length;
}
temp[tem++].length=dsp[j][i]+n;
}
else
{
for(m=0;m<perm;m++)
{
if(permanent[m].dest==j+97)
{
n=permanent[m].length+dsp[j][i];
break;
}
else
n=dsp[j][i];
}
if((n<temp[l].length))
{
temp[l].length=n;
temp[l].src=j+97;
tpaemp[l].dest=i+97;
} } } } }
sort();
}
printf("\n shortest path :\n\n");
printf("from %c to %c is:",initial,dest);
for(i=0;i<perm-1;i++)
{
if(permanent[i].dest==dest)
{
point=i;
n=i;
break;
}
}
i=0;
for(j=perm;j>0;j--)
{
if(permanent[j-1].dest==permanent[point].src)
{
path[i++]=permanent[point].dest;
point=j-1;
} }
path[i]=initial;
for(j=i;j>=0;j--)
{
printf("%c",path[j]);
if(j>0)
printf("----->"); }
printf("\t length=%d",permanent[m].length);
getch();
}

Output :

(5)

#include<stdio.h>
main()
{
int n,a[10][10],i,j,k;

printf("\n ENTER THE NO.OF NODES: ");


scanf("%d",&n);
printf("\n ENTER THE MATRIX ELEMENTS: ");
for(i=0;i<n;i++)
{
printf("\nENTER THE DISTANCE FOR NODE:%d\n",i+1);
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
printf("THE LINK STATE STATE PACKETS FOR NODE:%d\n",i+1);
printf("\n NODE\tDISTANCE\n");
for(j=0;j<n;j++)
{
if(a[i][j]!=0&&a[i][j]!=-1)
{
printf("%d\t%d\n",j+1,a[i][j]);
}
}
printf("\n\n");
}

}
Output :

(6)

#include"stdio.h"

#include"conio.h"

void main()

int i=0,j=0,nei,n,ah[20];

int v[50][50],r[20],min=10000,si;

char s[50],dest,line[50];

for(i=0;i<50;i++)
s[i]= NULL;

clrscr();

printf("\nEnter the no of nodes:\n");

scanf("%d",&n);

printf("\nEnter the destination:\n");

scanf("%s",&dest);

printf("\nEnter the no.of neighbours:\n");

scanf("%d",&nei);

printf("\nEnter the neighbours & slash(0) at end:\n");

for(i=0;i<=nei;i++)

scanf("%s",s[i]);

for(j=0;j<nei;)

printf("\nThe distance from %c to %s :",dest,s[j]);

scanf("%d",&ah[j]);

j++; }

printf("\nEnter the table of entries:");

for(i=0;i<n;i++)

for(j=0;j<nei;j++)

scanf("%d",&v[i][j]);

for(i=0;i<n;i++)

{
min=10000;

for(j=0;j<nei;j++)

if(v[i][j]+ah[j]<min)

{
min=v[i][j]+ah[j];

line[i]=s[j];

}r[i]=min;

if(dest-97==i)

{ r[i]=0;

line[i]=' ';

printf("\n new estimated delays from %c |",dest);

printf(" Line\n____________________________

_________________\n\n");

for(i=0;i<n;i++)

printf("\n \t%d\t\t\t| %s",r[i],line[i]);

getch();

OUTPUT:

(7)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int i,ch,lp;
char cipher[50],plain[50];
char key[50];
clrscr();
while(1)
{
printf("\n-----MENU-----");
printf("\n1:Data Encryption\t2:Data Decryption\t3:Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nData Encryption");
printf("\nEnter the plain text:");
fflush(stdin);
gets(plain);
printf("\nEnter the encryption key:");
gets(key);
lp=strlen(key);
for(i=0;plain[i]!='\0';i++)
cipher[i]=plain[i]^lp;
cipher[i]='\0';
printf("\nThe encrypted text is:");
puts(cipher);
break;

case 2: printf("\nData decryption");


for(i=0;cipher[i]!='\0';i++)
plain[i]=cipher[i]^lp;
printf("\nDecrypted text is:");
puts(plain);
break;

case 3: exit(0);
}
}

Output:

-------Menu--------

1. Data Encryption
2. Data Decryption
3. Exit

Enter your Choice: 1

Data Encryption

Enter the plain text: rupesh


Enter the encryption key: hsepur
The encrypted text is: tsvcun

-------Menu--------

1. Data Encryption
2. Data Decryption
3. Exit

Enter your Choice: 2

Data Decryption

Decrypted text is: rupesh

-------Menu--------

1. Data Encryption
2. Data Decryption
3. Exit

Enter your Choice: 3

7(b)

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char pwd[20];
char alpha[26]="abcdefghijklmnopqrstuvwxyz";
int num[20],i,n,key;
clrscr();
printf("\nEnter the password:");
scanf("%s",&pwd);
n=strlen(pwd);
for(i=0;i<n;i++)
num[i]=toascii(tolower(pwd[i]))-'a';
printf("\nEnter the key:");
scanf("%d",&key);
for(i=0;i<n;i++)
num[i]=(num[i]+key)%26;
for(i=0;i<n;i++)
pwd[i]=alpha[num[i]];
printf("\nThe key is:%d",key);
printf("\nEncrypted text is:%s",pwd);
for(i=0;i<n;i++)
{
num[i]=(num[i]-key)%26;
if(num[i]<0)
num[i]=26+num[i];
pwd[i]=alpha[num[i]];
}
printf("\nDecrypted text is:%s",pwd);
getch();
}

Output:

Input:

Enter the password: rupesh


Enter the key:eeeee

Output:

The key is : 26522


Encrypted text is : twrguj
Dencrypted text is : rupes

(8)

#include< stdio.h>
#include< conio.h>

int phi,M,n,e,d,C,FLAG;

void check()
{
int i;
for(i=3;e%i==0 && phi%i==0;i+2)
{
FLAG = 1;
return;
}
FLAG = 0;
}
void encrypt()
{
int i;
C = 1;
for(i=0;i< e;i++)
C=C*M%n;
C = C%n;
printf("\n\tEncrypted keyword : %d",C);
}

void decrypt()
{
int i;
M = 1;
for(i=0;i< d;i++)
M=M*C%n;
M = M%n;
printf("\n\tDecrypted keyword : %d",M);
}

void main()
{
int p,q,s;
clrscr();
printf("Enter Two Relatively Prime Numbers\t: ");
scanf("%d%d",&p,&q);
n = p*q;
phi=(p-1)*(q-1);
printf("\n\tF(n)\t= %d",phi);
do
{
printf("\n\nEnter e\t: ");
scanf("%d",&e);
check();
}while(FLAG==1);
d = 1;
do
{
s = (d*e)%phi;
d++;
}while(s!=1);
d = d-1;
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
scanf("%d",&M);
encrypt();
printf("\n\nEnter the Cipher text\t: ");
scanf("%d",&C);
decrypt();
getch();
}

OUTPUT :

Enter Two Relatively Prime Numbers : 7 17

F(n) = 96

Enter e : 5

Public Key : {5,119}


Private Key : {77,119}

Enter The Plain Text : 19

Encrypted keyword : 66

Enter the Cipher text : 66

(9)

Apparatus (Software): Command Prompt And Packet Tracer.

Procedure: To do this EXPERIMENT- follows these steps:


In this EXPERIMENT- students have to understand basic networking commands e.g
ping, tracert etc.

All commands related to Network configuration which includes how to switch to


privilege mode and normal mode and how to configure router interface and how to save this
configuration to flash memory or permanent memory.

This commands includes

• Configuring the Router commands


• General Commands to configure network
• Privileged Mode commands of a router
• Router Processes & Statistics
• IP Commands
• Other IP Commands e.g. show ip route etc.

1.ping: ping(8) sends an ICMP ECHO_REQUEST packet to the specified host. If the
host responds, you get an ICMP packet back. Sound strange? Well, you can “ping” an IP
address to see if a machine is alive. If there is no response, you know something is wrong.
2.Traceroute:

Tracert is a command which can show you the path a packet of information takes from
your computer to one you specify. It will list all the routers it passes through until it reaches its
destination, or fails to and is discarded. In addition to this, it will tell you how long each 'hop'
from router to router takes.
3.nslookup:

Displays information from Domain Name System (DNS) name servers.


NOTE If you write the command as above it shows as default your pc's server name
firstly.

C:\Users\Thyagarajan>nslookup
Default Server: dns.google
Address: 2001:4860:4860::8888

> nslookup www.svcetedu.org


Server: svcetedu.org
Address: 43.225.55.220
Aliases: www.svcetedu.org

*** www.svcetedu.org can't find nslookup: Query refused


> nslookup www.gmail.com
Server: googlemail.l.google.com
Addresses: 2404:6800:4002:80d::2005
172.217.167.197
Aliases: www.gmail.com
mail.google.com

DNS request timed out.


timeout was 2 seconds.
DNS request timed out.
timeout was 2 seconds.
*** Request to www.gmail.com timed-out
4.pathping:

A better version of tracert that gives you statics about packet lost and latency.
(10)
Aim:
To Configure IP Address in a system in LAN (TCP/IP Configuration) and Configure DNS to establish
interconnection between systems
Principle: Following is required to be study under this practical.
• Classification of IP address
Class A 1.0.0.1 to 126.255.255.254 Supports 16 million hosts on each of 127 networks.
Class B 128.1.0.1 to 191.255.255.254 Supports 65,000 hosts on each of 16,000 networks.
Class C 192.0.1.1 to 223.255.254.254 Supports 254 hosts on each of 2 million networks.
Class D 224.0.0.0 to 239.255.255.255 Reserved for multicast groups.
Class E 240.0.0.0 to 254.255.255.254 Reserved.
• Sub netting
• Subnet ting is dividing the network into two or more networks is called subnet ting.
Procedure:
(a) Steps to configure IP address, Subnet mask and Default Gateway:
1. Click on the Start button and select Control Panel then Network and Internet Connections.
2. Click Network and Internet Connections.
3. Right click on the Local Area Connection icon and select Properties.
4. Select Internet Protocol (TCP/IP).
5. Click on the Properties button.
6. Uncheck that Obtain an IP address automatically and Obtain DNS server address automatically and
put
IP, Subnet mask & Default Gateways.
7. Click on the Advanced button and select the DNS tab in the Advanced TCP/IP Settings window.
8. Ensure that Register this connection's addresses in DNS is not selected.
9. Click OK, OK, then Close to close all boxes.

Result :
Configuration of IP Address in a system in LAN (TCP/IP Configuration) and Configuration to establish
interconnection between systems have been done successfully

You might also like