- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 2^(2^A) % B in C++
In this tutorial, we are going to write a program to evaluate the equation 2^(2^A) % B.
We are going to find the value of the equation using a recursive function. Let's see the steps to solve the problem.
Write a recursive function that takes 2 arguments A and B.
If A is 1, then return 4 % B as 2^(2^1) % B = 4 % B.
Else recursively call the function with A-1 and b.
Return the result^2%B.
Print the solution
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; long long solveTheEquation(long long A, long long B) { // 2^(2^1) % B = 4 % B if (A == 1) { return (4 % B); } else { long long result = solveTheEquation(A - 1, B); return result * result % B; } } int main() { long long A = 37, B = 467; cout << solveTheEquation(A, B) << endl; return 0; }
Output
If you execute the above code, then you will get the following result.
113
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- If ( a+b=5 ) and ( a b=2 ), find the value of(a) ( (a+b)^{2} )(b) ( a^{2}+b^{2} )(c) ( (a-b)^{2} )
- Simplify:$(a + b + c)^2 + (a - b + c)^2 + (a + b - c)^2$
- Factorize:$(a – b + c)^2 + (b – c + a)^2 + 2(a – b + c) (b – c + a)$
- Simplify:$(a + b + c)^2 + (a - b + c)^2$
- Simplify:$(a + b + c)^2 - (a - b + c)^2$
- If $a=2$ and $b=-2$ find the value of $(i)$. $a^2+b^2$$(ii)$. $a^2+ab+b^2$$(iii)$. $a^{2}-b^2$
- Find the following products:( left(frac{-7}{4} a b^{2} c-frac{6}{25} a^{2} c^{2}right)left(-50 a^{2} b^{2} c^{2}right) )
- If $a + b + c = 9$, and $a^2 + b^2 + c^2 = 35$, find the value of $a^3 + b^3 + c^3 - 3abc$.
- If ( a+b=5 ) and ( a b=2 ), find the value of(a) ( (a+b)^{2} )(b) ( a^{2}+b^{2} )
- If $a + b + c = 0$ and $a^2 + b^2 + c^2 = 16$, find the value of $ab + bc + ca$.
- If $a^2 + b^2 + c^2 = 16$ and $ab + bc + ca = 10$, find the value of $a + b + c$.
- If $a + b + c = 9$ and $ab + bc + ca = 23$, find the value of $a^2 + b^2 + c^2$.
- Factorize:$a^2 + 2ab + b^2 – c^2$
- Factorize:$a^2 - b^2 + 2bc - c^2$
- Factorize:( 6 a b-b^{2}+12 a c-2 b c )

Advertisements