Decimal Representation of given Binary String is Divisible by 20 or Not


In this article, we take the challenge to determine whether or not the given binary number's decimal form can be divided by 20. The base-2 numeral system, often known as the binary numeral system, is a way of expressing numbers in mathematics that employs just two symbols, commonly "0" (zero) and "1." (one). For instance, the decimal number 4 is represented as 100 in binary form. The binary form of the decimal number 6 is 110. The binary representation is 11100 for the decimal number 28. Now think how the decimal number 1,23,45,687 can be represented as a binary number?

Be cautious because the number might be quite huge and might not even fit in a long long integer. The strategy here is ought to ensure that the number of division and multiplication operations is either zero or as few as possible. Input does not contain any leading zeros.

Approach

We first convert the binary number into its equivalent decimal representational form. In order to change any binary number to a decimal number, we must multiply each digit of the binary number starting at 0 by powers of 2 and then add the results to obtain the decimal number.

After we obtain the binary number's decimal representation, we are able to employ the modulo operator (%) to determine if it is divisible by 20. By applying the modulo operator, we take the remainder. If the remainder is 0, then the number is divisible by 20. Else if the remainder is any number other than 0, the given number is not divisible by 20.

The binary integer is indeed divisible by 20 if the decimal equivalent is divisible by 20.

Problem Statement

Check whether the decimal representation of a given binary string is divisible by 20 or not.

Example 1

Let us take the binary number 101000.

The decimal representation is 40.

Explanation: (32*1) + (16*0) + (8*1) + (4*0) + (2*0) + (1*0) = 32 + 8 which gives 40.

Since 40 is divisible by 20, the binary number 101000 is divisible by 20.

Example 2

Let us take the binary number 11110.

The decimal representation is 30.

Explanation: (16*1) + (8*1) + (4*1) + (2*1) + (1*0) = 16 + 8 + 4 + 2 which gives 30.

Since 30 is not divisible by 20, the binary number 11110 is not divisible by 20.

Algorithm

The algorithm to check whether the decimal representation of a given binary string is divisible by 20 or not is given below.

STEP 1: Start

STEP 2: Input binary number

STEP 3: Define variables dec=0, i and r (r being the reminder)

STEP 4: Convert the given binary number into decimal form.

STEP 5: Check that the decimal number is divisible by 20 by taking modulo operator (%)

STEP 6: Print the output

STEP 7: Stop

Example

C program to check whether the binary number's decimal form can be divided by 20 is given below.

#include <stdio.h>
#include <math.h>
// Checking whether the number is divisible by 20
// and printing the necessary output message
int convert(long long);
int main()
{
   long binNum=101000;
   printf("The decimal representation of 101000 is %d",convert(binNum));
   if (convert(binNum) % 20 == 0) {
      printf("\nIt is divisible by 20");
   } else {
      printf("\nNot divisible by 20");
   }
   return 0;
}
// function to convert binary number into its decimal form
int convert(long long binNum)
{
   int dec = 0, i = 0, r;
   while (binNum!=0) {
      r = binNum % 10;
      binNum /= 10;
      dec += r * pow(2, i);
      ++i;
   }
   return dec;
}

Output

On execution, it will produce the following output:

The decimal representation of 101000 is 40
It is divisible by 20

Conclusion

Likewise, we can determine whether the decimal representation of a given binary string is divisible by 20 or not by inputting the binary number.

The challenge of determining whether the decimal representation of a given binary string is divisible by 20 or not is resolved in this article. Here C programming codes are provided.

Updated on: 23-Aug-2023

38 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements