Decimal representation of given binary string is divisible by 10 or not | C ++ implementation to check whether decimal representation of given binary number is divisible by 10 or not ; function to check whether decimal representation of given binary number is divisible by 10 or not ; if last digit is '1' , then number is not divisible by 10 ; to accumulate the sum of last digits in perfect powers of 2 ; traverse from the 2 nd last up to 1 st digit in ' bin ' ; if digit in '1' ; calculate digit 's position from  the right ; according to the digit 's position,  obtain the last digit of the applicable  perfect power of 2 ; if last digit is 0 , then divisible by 10 ; not divisible by 10 ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE 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 ; }