The problem is to check whether the decimal representation of the given binary number is divisible by 10 or not. Take care, the number could be very large and may not fit even in long long int. The approach should be such that there are zero or minimum number of multiplication and division operations. No leading 0’s are there in the input.
Examples:
Input : 101000
Output : Yes
(101000)2 = (40)10
and 40 is divisible by 10.
Input : 11000111001110
Output : Yes
Approach: First of all we need to know that last digit of pow(2, i) = 2, 4, 8, 6 if i % 4 is equal to 1, 2, 3, 0 respectively, where i is greater than equal to 1. So, in the binary representation we need to know the position of digit ‘1’ from the right, so as to know the perfect power of 2 with which it is going to be multiplied. This will help us to obtain the last digit of the required perfect power’s of 2. We can add these digits and then check whether the last digit of the sum is 0 or not which implies that the number is divisible by 10 or not. Note that if the last digit in the binary representation is ‘1’ then it represents an odd number, and thus not divisible by 10.
C++
#include <bits/stdc++.h>
using namespace std;
bool isDivisibleBy10(string bin)
{
int n = bin.size();
if (bin[n-1] == '1' )
return false ;
int sum = 0;
for ( int i=n-2; i>=0; i--)
{
if (bin[i] == '1' )
{
int posFromRight = n - i - 1;
if (posFromRight % 4 == 1)
sum = sum + 2;
else if (posFromRight % 4 == 2)
sum = sum + 4;
else if (posFromRight % 4 == 3)
sum = sum + 8;
else if (posFromRight % 4 == 0)
sum = sum + 6;
}
}
if (sum % 10 == 0)
return true ;
return false ;
}
int main()
{
string bin = "11000111001110" ;
if (isDivisibleBy10(bin))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static boolean isDivisibleBy10(String bin)
{
int n = bin.length();
if (bin.charAt(n - 1 ) == '1' )
return false ;
int sum = 0 ;
for ( int i = n - 2 ; i >= 0 ; i--)
{
if (bin.charAt(i) == '1' )
{
int posFromRight = n - i - 1 ;
if (posFromRight % 4 == 1 )
sum = sum + 2 ;
else if (posFromRight % 4 == 2 )
sum = sum + 4 ;
else if (posFromRight % 4 == 3 )
sum = sum + 8 ;
else if (posFromRight % 4 == 0 )
sum = sum + 6 ;
}
}
if (sum % 10 == 0 )
return true ;
return false ;
}
public static void main(String[] args)
{
String bin = "11000111001110" ;
if (isDivisibleBy10(bin))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python
def isDivisibleBy10( bin ) :
n = len ( bin )
if ( bin [n - 1 ] = = '1' ) :
return False
sum = 0
i = n - 2
while i > = 0 :
if ( bin [i] = = '1' ) :
posFromRight = n - i - 1
if (posFromRight % 4 = = 1 ) :
sum = sum + 2
else if (posFromRight % 4 = = 2 ) :
sum = sum + 4
else if (posFromRight % 4 = = 3 ) :
sum = sum + 8
else if (posFromRight % 4 = = 0 ) :
sum = sum + 6
i = i - 1
if ( sum % 10 = = 0 ) :
return True
return False
bin = "11000111001110"
if (isDivisibleBy10( bin ) = = True ) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool isDivisibleBy10(String bin)
{
int n = bin.Length;
if (bin[n - 1] == '1' )
return false ;
int sum = 0;
for ( int i = n - 2; i >= 0; i--) {
if (bin[i] == '1' ) {
int posFromRight = n - i - 1;
if (posFromRight % 4 == 1)
sum = sum + 2;
else if (posFromRight % 4 == 2)
sum = sum + 4;
else if (posFromRight % 4 == 3)
sum = sum + 8;
else if (posFromRight % 4 == 0)
sum = sum + 6;
}
}
if (sum % 10 == 0)
return true ;
return false ;
}
public static void Main()
{
String bin = "11000111001110" ;
if (isDivisibleBy10(bin))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function isDivisibleBy10( $bin )
{
$n = strlen ( $bin );
if ( $bin [ $n - 1] == '1' )
return false;
$sum = 0;
for ( $i = $n - 2; $i >= 0; $i --)
{
if ( $bin [ $i ] == '1' )
{
$posFromRight = $n - $i - 1;
if ( $posFromRight % 4 == 1)
$sum = $sum + 2;
else if ( $posFromRight % 4 == 2)
$sum = $sum + 4;
else if ( $posFromRight % 4 == 3)
$sum = $sum + 8;
else if ( $posFromRight % 4 == 0)
$sum = $sum + 6;
}
}
if ( $sum % 10 == 0)
return true;
return false;
}
$bin = "11000111001110" ;
if (isDivisibleBy10( $bin ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isDivisibleBy10(bin)
{
let n = bin.length;
if (bin[n - 1] == '1' )
return false ;
let sum = 0;
for (let i = n - 2; i >= 0; i--)
{
if (bin[i] == '1' )
{
let posFromRight = n - i - 1;
if (posFromRight % 4 == 1)
sum = sum + 2;
else if (posFromRight % 4 == 2)
sum = sum + 4;
else if (posFromRight % 4 == 3)
sum = sum + 8;
else if (posFromRight % 4 == 0)
sum = sum + 6;
}
}
if (sum % 10 == 0)
return true ;
return false ;
}
let bin = "11000111001110" ;
if (isDivisibleBy10(bin))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output:
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)
Method: Convert the given binary string in to decimal using int function then check if it is divisible by 10 or not using modulo division.
C++
#include <iostream>
using namespace std;
int main()
{
char s[] = "1010" ;
int n = stoi(s, 0, 2);
if (n % 10 == 0) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Python3
str1 = "101000"
decnum = int (str1, 2 )
if decnum % 10 = = 0 :
print ( "Yes" )
else :
print ( "No" )
|
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
String s= "1010" ;
int n=Integer.parseInt(s, 2 );
if (n% 10 == 0 )
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Javascript
<script>
str1 = "101000"
decnum = Number.parseInt(str1, 2)
if (decnum % 10 == 0)
document.write( "Yes" )
else
document.write( "No" )
</script>
|
C#
using System;
public class GFG{
static public void Main (){
string s= "1010" ;
int n=Convert.ToInt32(s,2);
if (n%10==0)
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Time Complexity: O(n), where n is the number of digits in the binary number.
Auxiliary Space: O(1)
Method 3: Using the concept of modular arithmetic
We can also solve this problem without converting the binary string to decimal by using the concept of modular arithmetic. The idea is to iterate through the binary string from right to left, and keep track of the remainder after dividing the current number by 10. If the final remainder is 0, then the binary string is divisible by 10.
Python3
def is_divisible_by_10(binary_str):
remainder = 0
for digit in binary_str:
remainder = (remainder * 2 + int (digit)) % 10
return remainder = = 0
binary_str = '101010'
print (is_divisible_by_10(binary_str))
|
Javascript
function is_divisible_by_10(binary_str) {
let remainder = 0;
for (let i = 0; i < binary_str.length; i++) {
remainder = (remainder * 2 + parseInt(binary_str[i])) % 10;
}
return remainder == 0;
}
let binary_str = '101010' ;
console.log(is_divisible_by_10(binary_str));
|
Java
public class GFG {
public static boolean
is_divisible_by_10(String binary_str)
{
int remainder = 0 ;
for ( int i = 0 ; i < binary_str.length(); i++) {
char digit = binary_str.charAt(i);
int num = Character.getNumericValue(digit);
remainder = (remainder * 2 + num) % 10 ;
}
return remainder == 0 ;
}
public static void main(String[] args)
{
String binary_str = "101010" ;
System.out.println(is_divisible_by_10(binary_str));
}
}
|
C++
#include <iostream>
#include <string>
using namespace std;
bool is_divisible_by_10(string binary_str) {
int remainder = 0;
for ( int i = 0; i < binary_str.length(); i++) {
char digit = binary_str[i];
int num = digit - '0' ;
remainder = (remainder * 2 + num) % 10;
}
return remainder == 0;
}
int main() {
string binary_str = "101010" ;
cout << is_divisible_by_10(binary_str) << endl;
return 0;
}
|
C#
using System;
class Program
{
static bool IsDivisibleBy10( string binaryStr)
{
int remainder = 0;
foreach ( char digit in binaryStr)
{
remainder = (remainder * 2 + int .Parse(digit.ToString())) % 10;
}
return remainder == 0;
}
static void Main( string [] args)
{
string binaryStr = "101010" ;
Console.WriteLine(IsDivisibleBy10(binaryStr));
}
}
|
Time complexity: O(n)
Auxiliary Space: O(1)
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.