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

Convert the following code from C++ to C to get the same output below. provide s

ID: 3805150 • Letter: C

Question

Convert the following code from C++ to C to get the same output below. provide screenshot

#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;

}

Enter a Value for p Enter a value for g 17 The random Number of SA is :3 The random Number of SA is :6 The Value of Secret Key is :1-

Explanation / Answer

#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
main()
{
long p,g,Sa=0,Sb=0,Ta,Tb,Skey1,Skey2,Skey;
clrscr();
srand (time(NULL));
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() % 20;
Sb=rand() % 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
printf(" The random Number of SA is : %ld", Sa);
printf(" The random Number of SA is : %ld", Sb);
printf(" The Value of Secret Key is : %ld ", Skey);

getch();
return 0;
}

Output:

g++ -std=c++11 -o main *.cpp                                                                                                                                                                                                     

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter a Value for p :7                                                                                                                                                                                                                                                 

Enter a Value for g :17                                                                                                                                                                                                                                                

                                                                                                                                                                                                                                                                       

The random Number of SA is : 4                                                                                                                                                                                                                                         

The random Number of SA is : 9                                                                                                                                                                                                                                         

The Value of Secret Key is : 1