Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert the follwing code from C++ to C #include <stdio.h> #include<iostream.h>

ID: 3803959 • Letter: C

Question

Convert the follwing code from C++ to C

#include <stdio.h>
#include<iostream.h>
#include <conio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
main()
{
long int p,g,Sa=0,Sb=0,Ta,Tb,Skey1,Skey2,Skey;
clrscr();
cout<<"Enter a Value for p :";
cin>>p;
cout<<" Enter a Value for g :";
cin>>g;
//======== Generate Sa and Sb Values Randomly between 0 and 20
Sa=random(20);
Sb=random(20);

//======== Calculate Ta and Tb Values
Ta=(pow(g,Sa));
Ta=Ta%p;
Tb=(pow(g,Sb));
Tb=Tb%p;
//======== Calculate Secret Key Values
Skey1=(pow(Tb,Sa));
Skey1=Skey1%p;
Skey2=(pow(Ta,Sb));
Skey2=Skey2%p;
if(Skey1==Skey2)
Skey=Skey1;
//========= Print Secret Key Value
cout<<" The random Number of SA is :"<<Sa;
cout<<" The random Number of SA is :"<<Sb;
cout<<" The Value of Secret Key is :"<<Skey;

getch();
return 0;
}

Explanation / Answer

Here is the C version for you:

#include <stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int main()
{
srand(0);
long int p,g,Sa=0,Sb=0,Ta,Tb,Skey1,Skey2,Skey;
printf("Enter a Value for p : ");
scanf("%ld", &p);
printf(" Enter a Value for g : ");
scanf("%ld", &g);
//======== Generate Sa and Sb Values Randomly between 0 and 20
Sa=rand() % 21;
Sb=rand() % 21;
//======== Calculate Ta and Tb Values
Ta=(pow(g,Sa));
Ta=Ta%p;
Tb=(pow(g,Sb));
Tb=Tb%p;
//======== Calculate Secret Key Values
Skey1=(pow(Tb,Sa));
Skey1=Skey1%p;
Skey2=(pow(Ta,Sb));
Skey2=Skey2%p;
if(Skey1==Skey2)
Skey=Skey1;
//========= Print Secret Key Value
printf(" The random Number of SA is : %ld ", Sa);
printf(" The random Number of SB is : %ld ", Sb);
printf(" The Value of Secret Key is : %ld ", Skey);
return 0;
}