
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Modular Exponentiation Algorithm
This is a C++ program to implement Modular Exponentiation Algorithm.
Algorithm
Begin function modular(): // Arguments: base, exp, mod. // Body of the function: initialize res = 1 while (exp > 0) if (exp mod 2 == 1) res= (res * base) % mod exp = exp left shift 1 base = (base * base) % mod return res. End
Example
#include <iostream> using namespace std; long long modular(long long base, long long exp, int mod) { long long res = 1; while (exp > 0) { if (exp % 2 == 1) res= (res * base) % mod; exp = exp >> 1; base = (base * base) % mod; } return res; } int main() { long long b, e; int mod; cout<<"Enter Base : "; cin>>b; cout<<"Enter Exponent: "; cin>>e; cout<<"Enter Modular Value: "; cin>>mod; cout<<modular(b, e , mod); return 0; }
Output
Enter Base : 7 Enter Exponent: 6 Enter Modular Value: 26 25
- Related Articles
- Python program for Modular Exponentiation
- Modular Exponentiation (Power in Modular Arithmetic) in java
- C++ Program to Implement Kadane’s Algorithm
- C++ Program to Implement Johnson’s Algorithm
- C++ Program to Implement the RSA Algorithm
- C++ Program to Implement Coppersmith Freivald’s Algorithm
- C++ Program to Implement Nearest Neighbour Algorithm
- C++ Program to Implement Expression Tree Algorithm
- C++ Program to Implement Interpolation Search Algorithm
- C++ Program to Implement Extended Euclidean Algorithm
- C program to implement Euclid’ s algorithm
- C++ Program to Implement Levenshtein Distance Computing Algorithm
- C++ Program to Implement Dijkstra’s Algorithm Using Set
- C++ Program to Implement The Edmonds-Karp Algorithm
- C++ Program to Implement the Bin Packing Algorithm

Advertisements