- 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
Program to find last two digits of 2^n in C++
In this problem, we are given a number N. Our task is to create a Program to find last two digits of 2^n in C++.
Problem Description
To find the last two digits. We will use only the product of the last two digits. And leave other things to make the calculation small.
Let’s take an example to understand the problem,
Input: N = 12
Output: 96
Explanation
2^12 = 4096
Solution Approach
To solve the problem, a direct approach could be finding the value of 2^N and then finding the remainder when it is divided by 100.
Example
#include <iostream> using namespace std; int findLastDigit(int N){ int powerVal = 1; for(int i = 0; i < N; i++){ powerVal *= 2; } return powerVal%100; } int main() { int N = 14; cout<<"The last two digits of 2^"<<N<<" is "<<findLastDigit(N); return 0; }
Output
The last two digits of 2^14 is 84
This approach is not effective, as for large values of N the program will overflow.
A better approach is by only considering 2 digits from the values. And multiply it by two for every power.
For each in the case of 2^14, the last two digits are 84. We will multiply 84 by two instead of the whole number which will save calculations. So, (84*2)%100 = 68.
Example
#include <iostream> using namespace std; int findLastDigit(int N){ int powerVal = 1; for(int i = 0; i < N; i++){ powerVal = (powerVal * 2)%100; } return powerVal; } int main() { int N = 15; cout<<"The last two digits of 2^"<<N<<" is "<<findLastDigit(N); return 0; }
Output
The last two digits of 2^15 is 68
- Related Articles
- Find last two digits of sum of N factorials using C++.
- Program to find last two digits of Nth Fibonacci number in C++
- Program to find multiple of n with only two digits in Python
- Program to print last N lines in c++
- Number of digits in 2 raised to power n in C++
- Golang Program to extract the last two digits from the given year
- Sum of the digits of a number N written in all bases from 2 to N/2 in C++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Program to Find Out Integers Less than n Containing Multiple Similar Digits in C++
- C/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2
- C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
- Splitting last n digits of each value in the array in JavaScript
- Find last k digits in product of an array numbers in C++
- How to split last n digits of each value in the array with JavaScript?
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++
