Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

convert this C++ code to a C language code(make sure it ruins properly): #include using namespace std; string decToBin(int num) { /* Converts a given

convert this C++ code to a C language code(make sure it ruins properly):
#include
using namespace std;
string decToBin(int num)
{
/*
Converts a given decimal number to a binary number.
*/
if (num == 0) return "0";
if (num == 1) return "1";
if (num % 2 == 0)
return decToBin(num / 2) + "0"; // call then add to combin the letters in reverse.
else
return decToBin(num / 2) + "1"; // call then add to combin the letters in reverse.
}
int modExp(int b, int e, int m) {
/*
Calculates modular exponentiation: b^n mod m
*/
int x, power;
x = 1;
power = b % m;
string binaryNum = decToBin(e); // convert e to a binary string.
for (int i = binaryNum.size() - 1; i >= 0; i--) { // iterate over each char of the bianry number
char c = binaryNum[i];
cout << "Because b" << binaryNum.size() - 1 - i << " is " << string(1, c) << ", we have x = ";
if (c == '1') { // print (x * power) if c == 1
cout << "(" << x << " * " << power << ") ";
}
else {
cout << x;
}
if (i == 0) {
cout << " mod " << m << " ==> "; // if it is the last iteration
}
else {
cout << " and power = " << power << " ^ " << 2 << " mod " << m << " = " << (power * power) % m << " ";
}
if (c == '1')
x = (x * power) % m;
power = (power * power) % m;
}
return x;
}
int main() {
int a, b, m;
cout << "MODULAR EXPONENTIATION CALCULATOR ";
cout << "(a ^ b mod m solver) ";
//ask user for input
cout << "Enter Base (a): ";
cin >> a;
cout << "Enter Exponent (b): ";
cin >> b;
cout << "Enter Modulus (m): ";
cin >> m;
cout << b << " is equivalent to (" << decToBin(b) << ") as binary number... ";
// apply operation
int result = modExp(a, b, m);
//print result
cout << " MODULAR EXPONENTIATION of (" << a << "^" << b << ") mod (" << m << ") = " << result << " ";
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

Timeline for implementation report

Answered: 1 week ago

Question

What is dividend payout ratio ?

Answered: 1 week ago

Question

Explain the factors affecting dividend policy in detail.

Answered: 1 week ago

Question

1. Who will you assemble on the team?

Answered: 1 week ago