Sei sulla pagina 1di 44

Q1)WAP to implement Quick Sort and Merge Sort.

Ans)

Merge Sort

#include<stdio.h>
void mergesort(int *a,int first,int last);

void merge(int *a,int first,int mid,int last);


int main(void)
{
int i,n;
long a[100];
scanf("%d",&n);
for( i = 0 ; i < n ; i++ )
scanf("%d",&a[i]);
mergesort(a,0,n-1);
for( i = 0 ; i < n ; i++)
printf("%d ",a[i]);
return 0;
}
void mergesort(int *a,int first,int last)
{
if( last > first)
{
int mid = ( first+last )/2;
mergesort(a,first,mid);
mergesort(a,mid+1,last);

merge(a,first,mid,last);
}
}
void merge(int *a,int first,int mid,int last)
{
int pos = 0,i = first,j = mid+1;
int temp[last-first+1];
while( i <= mid && j <= last )
{
if( a[i] > a[j] )
temp[pos++] = a[j++];
else
temp[pos++] = a[i++];
}
while(i <= mid)
temp[pos++] = a[i++];
while(j <= last)
temp[pos++] = a[j++];
for( i = 0 ; i < pos ; i++ )
a[i+first] = temp[i];
}

Quick Sort
#include<stdio.h>
int Partition(int *A,int low,int high)
{
int pivot = A[low],j = low,i;
for(i=low+1;i<=high;i++)
{
if(A[i] <= pivot)
{
j++;
Swap(&A[i],&A[j]);
}
}
Swap(&A[j],&A[low]);
return j;
}

void QuickSort(int *A,int low,int high)


{
if(high > low)
{
int pivot_index = Partition(A,low,high);
QuickSort(A,low,pivot_index-1);
QuickSort(A,pivot_index+1,high);
}
}

int main(void)
{
int N,i;
printf("Enter the number of elements : ");
scanf("%d",&N);
int A[N+1];
printf("Enter the numbers : ");
for(i=0;i<N;i++)
scanf("%d",&A[i]);
QuickSort(A,0,N-1);
for(i=0;i<N;i++)
printf("%d ",A[i]);
return 0;}

Q2)WAP to add and multiply two polynomials.


#include<stdio.h>
#include<stdlib.h>

struct node
{
float coef;
int expo;

struct node *link;


};

struct node *create(struct node *);


struct node *insert_s(struct node *,float,int);
struct node *insert(struct node *,float,int);
void display(struct node *ptr);
void poly_add(struct node *,struct node *);
void poly_sub(struct node *,struct node *);
void poly_mult(struct node *,struct node *);
int main( )
{
int ch;
struct node *start1=NULL,*start2=NULL;

printf("Enter polynomial 1 :\n");


start1=create(start1);

printf("Enter polynomial 2 :\n");


start2=create(start2);

printf("Polynomial 1 is : ");
display(start1);
printf("Polynomial 2 is : ");
display(start2);
printf("\n1.Addition of polynomial\n2.Subtaction of polynomial\n3.Mutiplication of
polynomial\n4.Exit");

scanf("%d",&ch);
while(1)
{
switch(ch)
{
case 1:
poly_add(start1, start2);
break;
case 2:
poly_mult(start1, start2);
break;
case 3:
exit(0);
default:
printf("\nPlease try again");
}
printf("\n1.Addition of polynomial\n2.Mutiplication of polynomial\n3.Exit");
scanf("%d",&ch);
}
return 0;
}/*End of main()*/

struct node *create(struct node *start)


{
int i,n,ex;
float co;

printf("Enter the number of terms : ");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter coeficient for term %d : ",i);
scanf("%f",&co);
printf("Enter exponent for term %d : ",i);
scanf("%d",&ex);
start=insert_s(start,co,ex);
}
return start;
}/*End of create()*/
struct node *insert_s(struct node *start,float co,int ex)
{
struct node *ptr,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->coef=co;
tmp->expo=ex;
/*list empty or exp greater than first one */
if(start==NULL || ex > start->expo)
{
tmp->link=start;
start=tmp;
}
else
{

ptr=start;
while(ptr->link!=NULL && ptr->link->expo >= ex)
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
}
return start;
}/*End of insert()*/

struct node *insert(struct node *start,float co,int ex)


{
struct node *ptr,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->coef=co;
tmp->expo=ex;
/*If list is empty*/
if(start==NULL)
{
tmp->link=start;
start=tmp;
}
else

/*Insert at the end of the list*/

{
ptr=start;
while(ptr->link!=NULL)
ptr=ptr->link;

tmp->link=ptr->link;
ptr->link=tmp;
}
return start;
}/*End of insert()*/

void display(struct node *ptr)


{
if(ptr==NULL)
{
printf("Zero polynomial\n");
return;
}
while(ptr!=NULL)
{
printf("(%.1fx^%d)", ptr->coef,ptr->expo);
ptr=ptr->link;
if(ptr!=NULL)
printf(" + ");
else
printf("\n");
}
}/*End of display()*/
void poly_add(struct node *p1,struct node *p2)
{
struct node *start3;

start3=NULL;

while(p1!=NULL && p2!=NULL)


{
if(p1->expo > p2->expo)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
else if(p2->expo > p1->expo)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
else if(p1->expo==p2->expo)
{
start3=insert(start3,p1->coef+p2->coef,p1->expo);
p1=p1->link;
p2=p2->link;
}
}
/*if poly2 has finished and elements left in poly1*/
while(p1!=NULL)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;

}
/*if poly1 has finished and elements left in poly2*/
while(p2!=NULL)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
printf("Added polynomial is : ");
display(start3);
}/*End of poly_add() */

void poly_mult(struct node *p1, struct node *p2)


{
struct node *start3;
struct node *p2_beg = p2;
start3=NULL;
if(p1==NULL || p2==NULL)
{
printf("Multiplied polynomial is zero polynomial\n");
return;
}
while(p1!=NULL)
{
p2=p2_beg;
while(p2!=NULL)
{

start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2->expo);
p2=p2->link;
}
p1=p1->link;
}
printf("Multiplied polynomial is : ");
display(start3);
}/*End of poly_mult()*/

Q3)WAP to implement LCS using dynamic


programming.
#include<stdio.h>
#include<string.h>
int max(int A,int B)
{
return (A > B) ? A : B;
}

int main(void)
{
char a[100],b[100];
int n,m,i,j;
printf("Enter two strings : ");
scanf("%s %s",a,b);
n = strlen(a);
m = strlen(b);
int L[n+1][m+1];
for(i=0;i<=n;i++)
L[i][0] = 0;
for(j=0;j<=m;j++)
L[0][j] = 0;
for(i=1;i<=n;i++)
for(j=1;j<m;j++)
{
if(a[i-1] == b[j-1])
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = max(L[i-1][j],L[i][j-1]);
}
printf("Length : %d",L[n][m]);
return 0;
}

Q4)WAP to implement Huffman coding technique.


#include<bits/stdc++.h>
using namespace std;
#define SENTINAL '\0'
struct node
{
char character;
int freq;
struct node *left;
struct node *right;
node(char chas, int fr){character= chas; freq=fr;left=NULL; right = NULL;};

};
typedef node node;
class nodeCompare {
public:
bool operator() (node *a, node *b)
{
return (a->freq > b->freq);
}
};
//print heap and codes
void print_heap(node *head, std::string path)
{
if(!head)
return;

if(head->character !=SENTINAL)
cout<< head->character << " code is " << path<<"\n";

print_heap(head->left, path+"0");
print_heap(head->right, path+"1");
}

int main()
{
//new queue
std::priority_queue<node *, vector<node *>, nodeCompare> minHeap;

char arr[] = {'a', 'b', 'c', 'd', 'e', 'f'};


int freq[] = {5, 9, 12, 13, 16, 45};
//fill queue
for(int i=0;i<6;i++)
minHeap.push(new node(arr[i],freq[i]));
//iterate till heapsie is 1
while(minHeap.size()>1)
{
//remove 1
node *min1= minHeap.top();
minHeap.pop();
//remove 2
node *min2= minHeap.top();

minHeap.pop();
//merge both
node * new_node= new node(SENTINAL, min1->freq + min2->freq);
new_node->left= min1;
new_node->right= min2;
//push back in minHeap
minHeap.push(new_node);
}
print_heap(minHeap.top(), std::string(""));
return 0;
}

Q5)WAP to implement two integer multiplication


using divide and conquer technique.
#include<bits/stdc++.h>
using namespace std;

int makeEqualLength(string &str1, string &str2)


{
int len1 = str1.size();
int len2 = str2.size();
if (len1 < len2)

{
for (int i = 0 ; i < len2 - len1 ; i++)
str1 = '0' + str1;
return len2;
}
else if (len1 > len2)
{
for (int i = 0 ; i < len1 - len2 ; i++)
str2 = '0' + str2;
}
return len1; // If len1 >= len2
}
string addBitStrings( string first, string second )
{
string result;
int length = makeEqualLength(first, second);
int carry = 0;

for (int i = length-1 ; i >= 0 ; i--)


{
int firstBit = first.at(i) - '0';
int secondBit = second.at(i) - '0';
int sum = (firstBit ^ secondBit ^ carry)+'0';
result = (char)sum + result;
carry = (firstBit&secondBit) | (secondBit&carry) | (firstBit&carry);
}

if (carry) result = '1' + result;


return result;
}
int multiplyiSingleBit(string a, string b)
{ return (a[0] - '0')*(b[0] - '0'); }
long int multiply(string X, string Y)
{
int n = makeEqualLength(X, Y);
if (n == 0) return 0;
if (n == 1) return multiplyiSingleBit(X, Y);

int fh = n/2;
int sh = (n-fh);
string Xl = X.substr(0, fh);
string Xr = X.substr(fh, sh);
string Yl = Y.substr(0, fh);
string Yr = Y.substr(fh, sh);
long int P1 = multiply(Xl, Yl);
long int P2 = multiply(Xr, Yr);
long int P3 = multiply(addBitStrings(Xl, Xr), addBitStrings(Yl, Yr));
return P1*(1<<(2*sh)) + (P3 - P1 - P2)*(1<<sh) + P2;
}
int main(void)
{
int num1,num2;
scanf("%d %d",&num1,&num2);

string S1,S2;
S1 = bitset<32>(num1).to_string();
S2 = bitset<32>(num2).to_string();
printf("%ld\n",multiply(S1,S2));
return 0;
}

Q6) WAP to implement matrix multiplication using


divide and conquer.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define M 2
#define N (1<<M)
typedef double datatype;
typedef datatype mat[N][N]; // mat[2**M,2**M] for divide and conquer mult.
typedef struct
{
int ra, rb, ca, cb;
} corners;
void identity(mat A, corners a)
{
int i, j;
for (i = a.ra; i < a.rb; i++)

for (j = a.ca; j < a.cb; j++)


A[i][j] = (datatype) (i == j);
}

// set A[a] = k
void set(mat A, corners a, datatype k)
{
int i, j;
for (i = a.ra; i < a.rb; i++)
for (j = a.ca; j < a.cb; j++)
A[i][j] = k;
}

// set A[a] = [random(l..h)].


void randk(mat A, corners a, double l, double h)
{
int i, j;
for (i = a.ra; i < a.rb; i++)
for (j = a.ca; j < a.cb; j++)
A[i][j] = (datatype) (l + (h - l) * (rand() / (double) RAND_MAX));
}

// Print A[a]
void print(mat A, corners a, char *name)
{
int i, j;

printf("%s = {\n", name);


for (i = a.ra; i < a.rb; i++)
{
for (j = a.ca; j < a.cb; j++)
printf("%.2f ", A[i][j]);
printf("\n");
}
printf("}\n");
}

// C[c] = A[a] + B[b]


void add(mat A, mat B, mat C, corners a, corners b, corners c)
{
int rd = a.rb - a.ra;
int cd = a.cb - a.ca;
int i, j;
for (i = 0; i < rd; i++)
{
for (j = 0; j < cd; j++)
{
C[i + c.ra][j + c.ca] = A[i + a.ra][j + a.ca] + B[i + b.ra][j
+ b.ca];
}
}
}

// C[c] = A[a] - B[b]


void sub(mat A, mat B, mat C, corners a, corners b, corners c)
{
int rd = a.rb - a.ra;
int cd = a.cb - a.ca;
int i, j;
for (i = 0; i < rd; i++)
{
for (j = 0; j < cd; j++)
{
C[i + c.ra][j + c.ca] = A[i + a.ra][j + a.ca] - B[i + b.ra][j
+ b.ca];
}
}
}

// Return 1/4 of the matrix: top/bottom , left/right.


void find_corner(corners a, int i, int j, corners *b)
{
int rm = a.ra + (a.rb - a.ra) / 2;
int cm = a.ca + (a.cb - a.ca) / 2;
*b = a;
if (i == 0)
b->rb = rm; // top rows
else
b->ra = rm; // bot rows

if (j == 0)
b->cb = cm; // left cols
else
b->ca = cm; // right cols
}

// Multiply: A[a] * B[b] => C[c], recursively.


void mul(mat A, mat B, mat C, corners a, corners b, corners c)
{
corners aii[2][2], bii[2][2], cii[2][2], p;
mat P[7], S, T;
int i, j, m, n, k;

// Check: A[m n] * B[n k] = C[m k]


m = a.rb - a.ra;
assert(m==(c.rb-c.ra));
n = a.cb - a.ca;
assert(n==(b.rb-b.ra));
k = b.cb - b.ca;
assert(k==(c.cb-c.ca));
assert(m>0);

if (n == 1)
{
C[c.ra][c.ca] += A[a.ra][a.ca] * B[b.ra][b.ca];
return;

// Create the 12 smaller matrix indexes:


// A00 A01 B00 B01 C00 C01
// A10 A11 B10 B11 C10 C11
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
find_corner(a, i, j, &aii[i][j]);
find_corner(b, i, j, &bii[i][j]);
find_corner(c, i, j, &cii[i][j]);
}
}

p.ra = p.ca = 0;
p.rb = p.cb = m / 2;

#define LEN(A) (sizeof(A)/sizeof(A[0]))


for (i = 0; i < LEN(P); i++)
set(P[i], p, 0);

#define ST0 set(S,p,0); set(T,p,0)

// (A00 + A11) * (B00+B11) = S * T = P0


ST0;

add(A, A, S, aii[0][0], aii[1][1], p);


add(B, B, T, bii[0][0], bii[1][1], p);
mul(S, T, P[0], p, p, p);

// (A10 + A11) * B00 = S * B00 = P1


ST0;
add(A, A, S, aii[1][0], aii[1][1], p);
mul(S, B, P[1], p, bii[0][0], p);

// A00 * (B01 - B11) = A00 * T = P2


ST0;
sub(B, B, T, bii[0][1], bii[1][1], p);
mul(A, T, P[2], aii[0][0], p, p);

// A11 * (B10 - B00) = A11 * T = P3


ST0;
sub(B, B, T, bii[1][0], bii[0][0], p);
mul(A, T, P[3], aii[1][1], p, p);

// (A00 + A01) * B11 = S * B11 = P4


ST0;
add(A, A, S, aii[0][0], aii[0][1], p);
mul(S, B, P[4], p, bii[1][1], p);

// (A10 - A00) * (B00 + B01) = S * T = P5


ST0;

sub(A, A, S, aii[1][0], aii[0][0], p);


add(B, B, T, bii[0][0], bii[0][1], p);
mul(S, T, P[5], p, p, p);

// (A01 - A11) * (B10 + B11) = S * T = P6


ST0;
sub(A, A, S, aii[0][1], aii[1][1], p);
add(B, B, T, bii[1][0], bii[1][1], p);
mul(S, T, P[6], p, p, p);

// P0 + P3 - P4 + P6 = S - P4 + P6 = T + P6 = C00
add(P[0], P[3], S, p, p, p);
sub(S, P[4], T, p, p, p);
add(T, P[6], C, p, p, cii[0][0]);

// P2 + P4 = C01
add(P[2], P[4], C, p, p, cii[0][1]);

// P1 + P3 = C10
add(P[1], P[3], C, p, p, cii[1][0]);

// P0 + P2 - P1 + P5 = S - P1 + P5 = T + P5 = C11
add(P[0], P[2], S, p, p, p);
sub(S, P[1], T, p, p, p);
add(T, P[5], C, p, p, cii[1][1]);

}
int main()
{
mat A, B, C;
corners ai = { 0, N, 0, N };
corners bi = { 0, N, 0, N };
corners ci = { 0, N, 0, N };
// identity(A,bi); identity(B,bi);
// set(A,ai,2); set(B,bi,2);
randk(A, ai, 0, 2);
randk(B, bi, 0, 2);
print(A, ai, "A");
print(B, bi, "B");
set(C, ci, 0);
// add(A,B,C, ai, bi, ci);
mul(A, B, C, ai, bi, ci);
print(C, ci, "C");
return 0;
}

Q7)WAP to implement DFS and BFS.


DFS
#include<bits/stdc++.h>
using namespace std;
struct AL
{
int data;
struct AL *next;
};
int V[10] = {0};
void addEdge(vector<struct AL *> &G,int start,int end)
{
struct AL *temp = (struct AL *)malloc(sizeof(struct AL));
temp->data = end;
temp->next = G[start]; //G[start] initially contains NULL
G[start] = temp;
}

//Now G[start] contains value at temp

void dfs(vector<struct AL *> &G,int i)


{
V[i] = 1;
printf("%d ",i);
struct AL *temp = G[i];
while(temp)
{
if(!V[temp->data])
dfs(G,temp->data);
temp = temp->next;
}
}
void bfs(vector<struct AL *> &G,int i)
{
queue<int> Q;
Q.push(i);

//V[i] = 1;
while(!Q.empty())
{
int j = Q.front();
printf("%d ",j);
V[j] = 1;
Q.pop();
struct AL *temp = G[j];
while(temp)

{
if(!V[temp->data])
{
Q.push(temp->data);
// V[temp->data] = 1;
}
temp = temp->next;
}
}
}
int main(void)
{
vector<struct AL *> Graph;
int v = 4,i;
for(int i=0;i<v;i++)
Graph.push_back(NULL);
addEdge(Graph, 0, 1);
addEdge(Graph, 0, 2);
addEdge(Graph, 1, 2);
addEdge(Graph, 2, 0);
addEdge(Graph, 2, 3);
addEdge(Graph, 3, 3);
dfs(Graph,0);
memset(V,0,sizeof(V));
bfs(Graph,0);
return 0;

Q8)WAP to implement Dijktras algorithm to find


the shortest path.
#include<bits/stdc++.h>
using namespace std;
struct AL
{
int data;
int weight;
struct AL *next;
};
#define MAX 100000
typedef pair<int,int> P;

void addEdge(vector<struct AL *> &G,int start,int end,int weight)


{
struct AL *temp = (struct AL *)malloc(sizeof(struct AL));
temp->weight = weight;
temp->data = end;
temp->next = G[start]; //G[start] initially contains NULL
G[start] = temp;
}

//Now G[start] contains value at temp

void dijkstra(vector<struct AL *> &G,int start,int N)


{
int distance[N+1],v[N+1],i;
for(i=0;i<N;i++)
{
distance[i] = MAX;
v[i] = 0;
}
distance[start] = 0;
priority_queue<P> PQ;
PQ.push(make_pair(start,0));
P A;
while(!PQ.empty())
{
A = PQ.top();
PQ.pop();
int u = A.first;
int d = A.second;
if(v[u] == 1)
continue;
v[u] = 1;
struct AL *temp = G[u];
while(temp)
{
if(distance[temp->data] > distance[u] + temp->weight)
distance[temp->data] = distance[u] + temp->weight;

PQ.push(make_pair(temp->data,distance[temp->data]));
temp = temp->next;
}
}
for(i=0;i<N;i++)
printf("%d ",distance[i]);
}

int main(void)
{
vector<struct AL *> Graph;
int v = 5,i;
for(int i=0;i<v;i++)
Graph.push_back(NULL);
addEdge(Graph, 0, 1,10);
addEdge(Graph, 0, 2,3);
addEdge(Graph, 1, 2,1);
addEdge(Graph, 2, 1,4);
addEdge(Graph, 2, 3,8);
addEdge(Graph, 1, 3,2);
addEdge(Graph, 2, 4,2);
addEdge(Graph, 3, 4,7);
addEdge(Graph, 4, 3,9);
dijkstra(Graph,0,v);
return 0;
}

Q9)WAP to implement Bellman Ford algorithm.


#include<bits/stdc++.h>
using namespace std;
struct AL
{
int data,weight;
struct AL *next;
};
void addEdge(vector<struct AL *> &G,int start,int end,int weight)
{
struct AL *temp = (struct AL *)malloc(sizeof(struct AL));
temp->data = end;
temp->next = G[start]; //G[start] initially contains NULL
temp->weight = weight;
G[start] = temp;

//Now G[start] contains value at temp

}
void BellmanFord(vector<struct AL *> G,int start,int N)
{
int dist[N+1];
for(int i=0;i<N;i++)

dist[i] = INT_MAX;
dist[start] = 0;
for(int i=0;i<N-1;i++)
{
for(int j=0;j<N;j++)
{
struct AL *temp = G[j];
while(temp)
{
if(dist[j] != INT_MAX && (dist[temp->data] > dist[j] +
temp->weight))
dist[temp->data] = dist[j] + temp->weight;
temp = temp->next;
}
}
}
printf("Vertex Distance\n");
for(int i=0;i<N;i++)
{
printf("%d\t%2d\n",i,dist[i]);
}
}
int main(void)
{
vector<struct AL *> Graph;
int v = 5;
for(int i=0;i<v;i++)

Graph.push_back(NULL);
addEdge(Graph, 0, 1,-1);
addEdge(Graph, 0, 2,4);
addEdge(Graph, 1, 2,3);
addEdge(Graph, 1, 3,2);
addEdge(Graph, 1, 4,2);
addEdge(Graph, 3, 1,1);
addEdge(Graph, 3, 2,5);
addEdge(Graph, 4, 3,-3);
BellmanFord(Graph,0,v);
return 0;
}

Q10) WAP to implement KMP algorithm for string


matching.
#include<stdio.h>
#include<string.h>
int F[100];
void PrefixTable(char *P, int len)
{
int i,j=0;
F[0] = 0;

for(i=1;i<len;)
{
if(P[i] == P[j])
F[i++] = ++j;
else if(j > 0)
j = F[j-1];
else
F[i++] = 0;
}
}
int KMP(char *T,char *P,int N,int M)
{
PrefixTable(P,M);
int i,j=0;
for(i=0;i<N;)
{
if(T[i] == P[j])
{
if(j == M-1)
return (i-j);
else
i++,j++;
}
else if(j > 0)
j = F[j-1];
else

i++;
}
return -1;
}
int main(void)
{
char T[1000],P[100];
int flag = 0;
printf("Enter the text : ");
scanf("%s",T);
printf("Enter the pattern : ");
scanf("%s",P);
int k = KMP(T,P,strlen(T),strlen(P));
if(k == -1)
printf("Pattern not found\n");
else
printf("Pattern found at index %d\n",k+1);
return 0;
}

Q11) WAP to implement RSA algorithm.


#include<stdio.h>
#include<stdlib.h>

#include<math.h>
#include<string.h>
long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;
char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();
void main()
{
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d",&p);
flag=prime(p);
if(flag==0)
{
printf("\nWRONG INPUT\n");
getch();
exit(1);
}
printf("\nENTER ANOTHER PRIME NUMBER\n");
scanf("%d",&q);
flag=prime(q);
if(flag==0||p==q)
{
printf("\nWRONG INPUT\n");

getch();
exit(1);
}
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%s",msg);
for(i=0;msg[i]!=NULL;i++)
m[i]=msg[i];
n=p*q;
t=(p-1)*(q-1);
ce();

encrypt();
decrypt();
getch();
}
int prime(long int pr)
{
int i;
j=sqrt(pr);
for(i=2;i<=j;i++)
{
if(pr%i==0)
return 0;
}
return 1;

}
void ce()
{
int k;
k=0;
for(i=2;i<t;i++)
{
if(t%i==0)
continue;
flag=prime(i);
if(flag==1&&i!=p&&i!=q)
{
e[k]=i;
flag=cd(e[k]);
if(flag>0)
{
d[k]=flag;
k++;
}
if(k==99)
break;
}
}
}
long int cd(long int x)
{

long int k=1;


while(1)
{
k=k+t;
if(k%x==0)
return(k/x);
}
}
void encrypt()
{
long int pt,ct,key=e[0],k,len;
i=0;
len=strlen(msg);
while(i!=len)
{
pt=m[i];
pt=pt-96;
k=1;
for(j=0;j<key;j++)
{
k=k*pt;
k=k%n;
}
temp[i]=k;
ct=k+96;
en[i]=ct;

i++;
}
en[i]=-1;
printf("\nTHE ENCRYPTED MESSAGE IS\n");
for(i=0;en[i]!=-1;i++)
printf("%c",en[i]);
}
void decrypt()
{
long int pt,ct,key=d[0],k;
i=0;
while(en[i]!=-1)
{
ct=temp[i];
k=1;
for(j=0;j<key;j++)
{
k=k*ct;
k=k%n;
}
pt=k+96;
m[i]=pt;
i++;
}
m[i]=-1;
printf("\nTHE DECRYPTED MESSAGE IS\n");

for(i=0;m[i]!=-1;i++)
printf("%c",m[i]);
}

Potrebbero piacerti anche