Program to find last two digits of 2^n
Given a number n, we need to find the last two digits of 2n.
Examples:
Input : n = 7
Output : 28
Input : n = 72
Output : 96
2^72 = 4722366482869645213696
A Naive Approach is to find the value of 2^n iteratively or using pow function. Once the value of 2^n is calculated, find the last two digits and print it.
Note: This approach will only work for 2n within a certain range, as overflow occurs.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int LastTwoDigit( long long int num)
{
int one = num % 10;
num /= 10;
int tens = num % 10;
tens *= 10;
num = tens + one;
return num;
}
int main()
{
int n = 10;
long long int num = 1;
num = pow (2, n);
cout << "Last " << 2;
cout << " digits of " << 2;
cout << "^" << n << " = " ;
cout << LastTwoDigit(num) << endl;
return 0;
}
|
Java
class Geeks {
static long LastTwoDigit( long num)
{
long one = num % 10 ;
num /= 10 ;
long tens = num % 10 ;
tens *= 10 ;
num = tens + one;
return num;
}
public static void main(String args[])
{
int n = 10 ;
long num = 1 ;
num = ( long )Math.pow( 2 , n);
System.out.println( "Last 2 digits of 2^10 = "
+LastTwoDigit(num));
}
}
|
Python3
def LastTwoDigit(num):
one = num % 10
num / / = 10
tens = num % 10
tens * = 10
num = tens + one
return num
if __name__ = = "__main__" :
n = 10
num = 1
num = pow ( 2 , n);
print ( "Last " + str ( 2 ) + " digits of " +
str ( 2 ) + "^" + str (n) +
" = " , end = "")
print (LastTwoDigit(num))
|
C#
using System;
class GFG
{
static long LastTwoDigit( long num)
{
long one = num % 10;
num /= 10;
long tens = num % 10;
tens *= 10;
num = tens + one;
return num;
}
public static void Main(String []args)
{
int n = 10;
long num = 1;
num = ( long )Math.Pow(2, n);
Console.WriteLine( "Last 2 digits of 2^10 = " +
LastTwoDigit(num));
}
}
|
PHP
<?php
function LastTwoDigit( $num )
{
$one = $num % 10;
$num /= 10;
$tens = $num % 10;
$tens *= 10;
$num = $tens + $one ;
return $num ;
}
$n = 10;
$num = 1;
$num = pow(2, $n );
echo ( "Last " . 2);
echo ( " digits of " . 2);
echo ( "^" . $n . " = " );
echo ( LastTwoDigit( $num )) ;
?>
|
Javascript
<script>
function LastTwoDigit(num)
{
let one = num % 10;
num = Math.floor(num/10);
let tens = num % 10;
tens *= 10;
num = tens + one;
return num;
}
let n = 10;
let num = 1;
num = Math.pow(2, n);
document.write( "Last " + 2);
document.write( " digits of " + 2);
document.write( "^" + n + " = " );
document.write(LastTwoDigit(num) + "<br>" );
</script>
|
Output:
Last 2 digits of 2^10 = 24
Time Complexity: O(log n), due to the pow() function
Auxiliary Space: O(1)
Efficient approach: The efficient way is to keep only 2 digits after every multiplication. This idea is very similar to the one discussed in Modular exponentiation where a general way is discussed to find (a^b)%c, here in this case c is 10^2 as the last two digits are only needed.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
int power( long long int x, long long int y, long long int p)
{
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int numberOfDigits( int x)
{
int i = 0;
while (x) {
x /= 10;
i++;
}
return i;
}
void LastTwoDigit( int n)
{
cout << "Last " << 2;
cout << " digits of " << 2;
cout << "^" << n << " = " ;
int temp = 1;
for ( int i = 1; i <= 2; i++)
temp *= 10;
temp = power(2, n, temp);
for ( int i = 0; i < 2 - numberOfDigits(temp); i++)
cout << 0;
if (temp)
cout << temp;
}
int main()
{
int n = 72;
LastTwoDigit(n);
return 0;
}
|
Java
class GFG
{
static int power( long x, long y,
long p)
{
int res = 1 ;
x = x % p;
while (y > 0 )
{
long r = y & 1 ;
if (r == 1 )
res = (res * ( int )x) % ( int )p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static int numberOfDigits( int x)
{
int i = 0 ;
while (x != 0 )
{
x /= 10 ;
i++;
}
return i;
}
static void LastTwoDigit( int n)
{
System.out.print( "Last " + 2 +
" digits of " + 2 + "^" );
System.out.print(n + " = " );
int temp = 1 ;
for ( int i = 1 ; i <= 2 ; i++)
temp *= 10 ;
temp = power( 2 , n, temp);
for ( int i = 0 ;
i < ( 2 - numberOfDigits(temp)); i++)
System.out.print( 0 + " " );
if (temp != 0 )
System.out.println(temp);
}
public static void main(String[] args)
{
int n = 72 ;
LastTwoDigit(n);
}
}
|
Python3
def power(x, y, p):
res = 1
x = x % p
while (y > 0 ):
if (y & 1 ):
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def numberOfDigits(x):
i = 0
while (x):
x / / = 10
i + = 1
return i
def LastTwoDigit(n):
print ( "Last " + str ( 2 ) +
" digits of " + str ( 2 ), end = "")
print ( "^" + str (n) + " = " , end = "")
temp = 1
for i in range ( 1 , 3 ):
temp * = 10
temp = power( 2 , n, temp)
for i in range ( 2 - numberOfDigits(temp)):
print ( 0 , end = "")
if temp:
print (temp)
if __name__ = = "__main__" :
n = 72
LastTwoDigit(n)
|
C#
using System;
class GFG
{
static int power( long x, long y,
long p)
{
int res = 1;
x = x % p;
while (y > 0)
{
long r = y & 1;
if (r == 1)
res = (res * ( int )x) % ( int )p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static int numberOfDigits( int x)
{
int i = 0;
while (x != 0)
{
x /= 10;
i++;
}
return i;
}
static void LastTwoDigit( int n)
{
Console.Write( "Last " + 2 +
" digits of " + 2 + "^" );
Console.Write(n + " = " );
int temp = 1;
for ( int i = 1; i <= 2; i++)
temp *= 10;
temp = power(2, n, temp);
for ( int i = 0;
i < ( 2 - numberOfDigits(temp)); i++)
Console.Write(0 + " " );
if (temp != 0)
Console.Write(temp);
}
public static void Main()
{
int n = 72;
LastTwoDigit(n);
}
}
|
PHP
<?php
function power( $x , $y , $p )
{
$res = 1;
$x = $x % $p ;
while ( $y > 0)
{
if ( $y & 1)
$res = ( $res * $x ) % $p ;
$y = $y >> 1;
$x = ( $x * $x ) % $p ;
}
return $res ;
}
function numberOfDigits( $x )
{
$i = 0;
while ( $x )
{
$x /= 10;
$i ++;
}
return $i ;
}
function LastTwoDigit( $n )
{
echo ( "Last " . 2);
echo ( " digits of " . 2);
echo ( "^" . $n . " = " );
$temp = 1;
for ( $i = 1; $i <= 2; $i ++)
$temp *= 10;
$temp = power(2, $n , $temp );
for ( $i = 0;
$i < 2 - numberOfDigits( $temp ); $i ++)
echo (0);
if ( $temp )
echo ( $temp );
}
$n = 72;
LastTwoDigit( $n );
?>
|
Javascript
<script>
function power(x, y, p)
{
let res = 1;
x = x % p;
while (y > 0)
{
let r = y & 1;
if (r == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function numberOfDigits(x)
{
let i = 0;
while (x != 0)
{
x /= 10;
i++;
}
return i;
}
function LastTwoDigit(n)
{
document.write( "Last " + 2 +
" digits of " + 2 + "^" );
document.write(n + " = " );
let temp = 1;
for (let i = 1; i <= 2; i++)
temp *= 10;
temp = power(2, n, temp);
for (let i = 0;
i < ( 2 - numberOfDigits(temp)); i++)
document.write(0 + " " );
if (temp != 0)
document.write(temp);
}
let n = 72;
LastTwoDigit(n);
</script>
|
Output:
Last 2 digits of 2^72 = 96
Time Complexity: O(log n)
Auxiliary Space: O(1)
Last Updated :
26 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...