In this program, we are going to share a C++ Program to Implement Modular Exponentiation Algorithm. If you are a beginner and want to start learning the C++ programming, then keep your close attention in this tutorial as I am going to share a program for C++ Program to Implement Modular Exponentiation Algorithm.
Copy the below C++ program and execute it with the help of GCC compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <iostream> #define ll long long using namespace std; /* * Function to calculate modulus of x raised to the power y */ ll modular_pow(ll base, ll exponent, int modulus) { ll result = 1; while (exponent > 0) { if (exponent % 2 == 1) result = (result * base) % modulus; exponent = exponent >> 1; base = (base * base) % modulus; } return result; } /* * Main */ int main() { ll x, y; int mod; cout<<"Enter Base Value: "; cin>>x; cout<<"Enter Exponent: "; cin>>y; cout<<"Enter Modular Value: "; cin>>mod; cout<<modular_pow(x, y , mod); return 0; } |
Enter Base Value: 2
Enter Exponent: 5
Enter Modular Value: 23
9
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.