Check if a large number is divisible by a number which is a power of 2 | C # program for the above approach ; Function to check divisibility ; Calculate the number of digits in num ; Check if the length of the string is less than the powerOf2 then return false ; Check if the powerOf2 is 0 that means the given number is 1 and as every number is divisible by 1 so return true ; Find the number which is formed by the last n digits of the string where n = powerOf2 ; Check if the number formed is divisible by input num or not ; Driver Code ; Given number ; Function call
using System ; class GFG { static bool checkIfDivisible ( String str , long num ) { long powerOf2 = ( int ) ( Math . Log ( num ) / Math . Log ( 2 ) ) ; if ( str . Length < powerOf2 ) return false ; if ( powerOf2 == 0 ) return true ; long i , number = 0 ; int len = str . Length ; for ( i = len - powerOf2 ; i < len ; i ++ ) { number += ( long ) ( ( str [ ( int ) i ] - '0' ) * Math . Pow ( 10 , powerOf2 - 1 ) ) ; powerOf2 -- ; } if ( number % num != 0 ) return false ; else return true ; } public static void Main ( String [ ] args ) { String str = "213467756564" ; long num = 4 ; if ( checkIfDivisible ( str , num ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }