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.

- C++ program to reverse a single linked list
- c++ program to remove vowels from a string
- CPP program to check if a number is Wagstaff prime or not
- CPP program to remove punctuation from a given string
- C++ program to find minimum players required to win the game anyhow
- C++ program to print primes smaller than n using Sieve of Sundaram
- C++ program to find count of divisors in n!
- C++ implementation to sort the given matrix
- C++ program to find Majority element in an array
- How to convert a string to integer array in CPP
- C++ Program for Tug of War with the output
- CPP Program of finding nth Delannoy Number
- Iterative C++ program to reverse a linked list
- Dynamic Programming implementation of Box Stacking problem
- How to Write a Program to Implement Miller Rabin Primality Test

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok