- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find (a^b)%m where ‘a’ is very large in C++

In this tutorial, we are going to solve the equation (a^{b})%m where a is a very large number.

The equation (a^{b})%m=(a%m)*(a%m)...b_times. We can solve the problem by finding the value of a%m and then multiplying it b times.

Let's see the steps to solve the problem.

Initialize the numbers a, b, and m.

Write a function to find the a%m.

Initialize the number with 0.

Iterate over the number in string format.

Add the last digits to the number.

Update the number with number modulo them.

Get the value of a%m.

Write a loop that iterates b times.

Multiply the a%m and modulo the result with m.

Print the result.

## Example

Let's see the code.

#include<bits/stdc++.h> using namespace std; unsigned int aModm(string str, unsigned int mod) { unsigned int number = 0; for (unsigned int i = 0; i < str.length(); i++) { number = number * 10 + (str[i] - '0'); number %= mod; } return number; } unsigned int aPowerBmodM(string &a, unsigned int b, unsigned int m) { unsigned int a_mod_m_result = aModm(a, m); unsigned int final_result = 1; for (unsigned int i = 0; i < b; i++) { final_result = (final_result * a_mod_m_result) % m; } return final_result; } int main() { string a = "123456789012345678901234567890123"; unsigned int b = 3, m = 7; cout << aPowerBmodM(a, b, m) << endl; return 0; }

## Output

If you execute the above program, then you will get the following result.

1

## Conclusion

If you have any queries in the tutorial, mention them in the comment section.

- Related Questions & Answers
- K-th digit in ‘a’ raised to power ‘b’ in C++
- Print values of ‘a’ in equation (a+b) <= n and a+b is divisible by x
- Find all the names beginning with the letter 'a' or ‘b’ or ‘c’ using MySQL query?
- Find Last Digit of a^b for Large Numbers in C++
- Count the numbers divisible by ‘M’ in a given range in C++
- Maximum element in a very large array using pthreads in C++
- Convert all substrings of length ‘k’ from base ‘b’ to decimal in C++
- Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time in C++
- Validate input: replace all ‘a’ with ‘@’ and ‘i’ with ‘!’JavaScript
- Recursive sum of digit in n^x, where n and x are very large in C++
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- Print all 3 digit repeating numbers in a very large number in C++
- Program to build DFA that starts and end with ‘a’ from input (a, b) in C++
- In the query [SELECT column1, column2 FROM table_name WHERE condition; ] which clause among ‘SELECT’, ‘WHERE’ and ‘FROM’ is evaluated in the last by the database server and why?
- How to handle very large numbers in Python?