GBN
1#include <stdio.h>
2
3int main() {
4 int total, win, lost, i, j;
5
6 printf("Total frames: ");
7 scanf("%d", &total);
8
9 printf("Window size: ");
10 scanf("%d", &win);
11
12 printf("Enter lost frame number: ");
13 scanf("%d", &lost);
14
15 for(i = 1; i <= total; i += win) {
16
17 printf("\nSending frames: ");
18 for(j = i; j < i + win && j <= total; j++)
19 printf("%d ", j);
20
21 for(j = i; j < i + win && j <= total; j++) {
22
23 if(j == lost) {
24 printf("\nFrame %d lost! Resending window...\n", j);
25
26 // resend from lost frame
27 for(j = lost; j < i + win && j <= total; j++)
28 printf("Resent frame %d -> ACK\n", j);
29
30 break; // stop checking further ACKs
31 }
32 else {
33 printf("\nACK for frame %d", j);
34 }
35 }
36 }
37
38 printf("\n\nAll frames delivered!\n");
39 return 0;
40}
CLASSFUL IP
1#include <stdio.h>
2
3int main() {
4 int a, b, c, d; // to store 4 octets
5
6 printf("Enter IP address (format: a.b.c.d): ");
7 scanf("%d.%d.%d.%d", &a, &b, &c, &d);
8
9 if(a >= 0 && a <= 127)
10 printf("Class A\nDefault Mask: 255.0.0.0\n");
11 else if(a >= 128 && a <= 191)
12 printf("Class B\nDefault Mask: 255.255.0.0\n");
13 else if(a >= 192 && a <= 223)
14 printf("Class C\nDefault Mask: 255.255.255.0\n");
15 else if(a >= 224 && a <= 239)
16 printf("Class D (Multicast)\n");
17 else if(a >= 240 && a <= 255)
18 printf("Class E (Research)\n");
19 else
20 printf("Invalid IP address!\n");
21
22 return 0;
23}
CLASSLESS
1#include <stdio.h>
2
3int main() {
4 int a, b, c, d;
5
6 printf("Enter IP address (format: a.b.c.d): ");
7 scanf("%d.%d.%d.%d", &a, &b, &c, &d);
8
9 if (a >= 0 && a <= 127)
10 printf("Class A\n");
11 else if (a >= 128 && a <= 191)
12 printf("Class B\n");
13 else if (a >= 192 && a <= 223)
14 printf("Class C\n");
15 else if (a >= 224 && a <= 239)
16 printf("Class D (Multicast)\n");
17 else if (a >= 240 && a <= 255)
18 printf("Class E (Experimental)\n");
19 else
20 printf("Invalid IP\n");
21
22 return 0;
23}
DJIKSTRA'S ALGO
1#include <stdio.h>
2#define INF 9999
3#define V 5 // number of vertices
4
5int main() {
6 int graph[V][V] = {
7 {0, 10, 0, 30, 100},
8 {10, 0, 50, 0, 0},
9 {0, 50, 0, 20, 10},
10 {30, 0, 20, 0, 60},
11 {100,0, 10, 60, 0}
12 };
13
14 int dist[V], visited[V];
15 int i, j, count, u, v;
16
17 // initialize
18 for(i = 0; i < V; i++) {
19 dist[i] = INF;
20 visited[i] = 0;
21 }
22
23 int start = 0; // starting node
24 dist[start] = 0;
25
26 for(count = 0; count < V-1; count++) {
27
28 // find minimum distance unvisited node
29 int min = INF;
30 for(i = 0; i < V; i++)
31 if(!visited[i] && dist[i] < min) {
32 min = dist[i];
33 u = i;
34 }
35
36 visited[u] = 1;
37
38 // relax adjacent vertices
39 for(v = 0; v < V; v++)
40 if(graph[u][v] && !visited[v] &&
41 dist[u] + graph[u][v] < dist[v]) {
42 dist[v] = dist[u] + graph[u][v];
43 }
44 }
45
46 // print results
47 printf("Shortest distances from node %d:\n", start);
48 for(i = 0; i < V; i++)
49 printf("To %d = %d\n", i, dist[i]);
50
51 return 0;
52}
ENCRYPTION / DECRYPTION
1#include <stdio.h>
2
3void encrypt(char *msg, int key) {
4 for (int i = 0; msg[i] != '\0'; i++) {
5 msg[i] = msg[i] + key; // shift forward
6 }
7}
8
9void decrypt(char *msg, int key) {
10 for (int i = 0; msg[i] != '\0'; i++) {
11 msg[i] = msg[i] - key; // shift backward
12 }
13}
14
15int main() {
16 char message[100];
17 int key;
18
19 printf("Enter message: ");
20 fgets(message, sizeof(message), stdin);
21
22 printf("Enter key (number): ");
23 scanf("%d", &key);
24
25 encrypt(message, key);
26 printf("Encrypted: %s\n", message);
27
28 decrypt(message, key);
29 printf("Decrypted: %s\n", message);
30
31 return 0;
32}
link state routing
1#include <stdio.h>
2#define INF 999
3
4int main() {
5 int n, source;
6 int cost[10][10], dist[10], visited[10];
7 int i, j, minDist, nextNode;
8
9 printf("Enter number of routers: ");
10 scanf("%d", &n);
11
12 printf("Enter cost adjacency matrix (use 999 for no direct link):\n");
13 for(i = 0; i < n; i++)
14 for(j = 0; j < n; j++)
15 scanf("%d", &cost[i][j]);
16
17 printf("Enter source router (0 to %d): ", n - 1);
18 scanf("%d", &source);
19
20 // Initialization
21 for(i = 0; i < n; i++) {
22 dist[i] = cost[source][i];
23 visited[i] = 0;
24 }
25
26 dist[source] = 0;
27 visited[source] = 1;
28
29 // Dijkstra / Link State Computation
30 for(i = 1; i < n; i++) {
31 minDist = INF;
32
33 for(j = 0; j < n; j++)
34 if(!visited[j] && dist[j] < minDist) {
35 minDist = dist[j];
36 nextNode = j;
37 }
38
39 visited[nextNode] = 1;
40
41 for(j = 0; j < n; j++)
42 if(!visited[j] && (minDist + cost[nextNode][j] < dist[j]))
43 dist[j] = minDist + cost[nextNode][j];
44 }
45
46 // Output Result
47 printf("\nShortest distance from router %d to others:\n", source);
48 for(i = 0; i < n; i++)
49 printf("Router %d -> %d = %d\n", source, i, dist[i]);
50
51 return 0;
52}