JavaScript Program to Count Rotations Divisible by 4


In this tutorial, we will learn to count the total number of rotations divisible by 4 for the given number.

Problem statement − We have given a number value. We need to rotate numbers in clockwise or anti-clockwise directions and count the total number of rotations divisible by 4.

Here, we will learn two different approaches to counting rotations divisible by 4.

Rotate the Number and Check if it is Divisible by 4

In this approach, we will convert the number to a string first. We can make n rotations for the string of length n. We will remove the first character of the string and add it at the last of the string. After that, we can check if the new number generated by rotation is divisible by 4.

Syntax

Users can follow the syntax below to check rotation is divisible by 4 and rotate the string of numbers.

for ( ) {
   if (parseInt(numStr) % 4 == 0) {
      count++;
   }
   numStr = numStr.substring(1, len) + numStr[0];
}

In the above syntax, the parseInt() method is used to convert the string to the number, and the substring() method is used to rotate the string.

Algorithm

  • Step 1 − Use the toString() method and convert the number to the string.

  • Step 2 − Use the for loop to make total ‘n’ rotations for the string of the length ‘n’.

  • Step 3 − Use the parseInt() method to convert the string to a number and check whether the number is divisible by 4. If the number is divisible by 4, increase the value of the count variable by 1.

  • Step 4 − Use the substring() method to get the substring from 1st index. Furthermore, append the string's first character at the end of the substring. This way, we can rotate the string and generate a new number.

Example 1

In the example below, we have defined the countRotations() function, which implements the above algorithm and returns the total number of rotations divisible by 4. In the output, users can observe that total of 2 rotations of the number is divisible by 4.

<html>
<body>
   <h3> Program to find the total number of rotations divisible by 4 </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      // JavaScript program to find the total count of rotations divisible by 4
      let countRotations = (number) => {
         let numStr = number.toString();
         let len = numStr.length;
         let count = 0;
         
         // Loop to traverse the string
         for (let i = 0; i < len; i++) {
         
            // Check if the string is divisible by 4
            if (parseInt(numStr) % 4 == 0) {
               count++;
            }
            
            // Rotate the string
            numStr = numStr.substring(1, len) + numStr[0];
         }
         return count;
      }
      let number = 121342435345;
      output.innerHTML = "Total count of rotations divisible by 4 of " + number + " is " + countRotations(number);
   </script>
</body>
</html>

Check Every Pair of 2 Digits is Divisible by 4

If the last 2 digit of any number is divisible by 4, we can say that number is divisible by 4. While rotating the number, every pair of two digits comes at the end of the number. So, we can check if any pair of two digits is divisible by 4; we can say one rotation related to that pair is divisible by 4.

Syntax

Users can follow the syntax below to extract the pair of two digits from the number and check if it is divisible by 4.

let lastDigit = num % 10;
num = Math.floor(num / 10);
let secondLastDigit = num % 10;
if ((secondLastDigit * 10 + lastDigit) % 4 == 0) {
   count++;
}

In the above syntax, we fetch the last and second last digit from the number. After that, we create a number of two digits using both and check if it is divisible by 4. If yes, we increase the value of the count variable.

Algorithm

  • Step 1 − If the number is of a single digit, check if it is divisible by 4. If yes, return 1; else, return 0.

  • Step 2 − If the number contains two or more digits, initialize the ‘count’ variable with 0.

  • Step 3 − Now, we need to create a single pair using the last digit and first digit of the number. Get the last digit using the modulo operator and the first using the Math.log() method.

  • Step 4 − Multiply the last digit with 10 and the first digit into that. After that, check if the result is divisible by 4. If yes, increase the value of the count by 1.

  • Step 5 − Use the while loop to check for other pairs of two digits. In the while loop, get the last and second last digit using the modulo operator. Create a pair using both digits and check if the pair is divisible by 2. If yes, increase the value of the count by 1.

Example 2

In this example, the countRotations() function counts the number of pairs of two digits divisible by 4. It implements the above algorithm and returns the value of the count once all operations are completed.

<html>
<body>
   <h3> Program to find the total number of rotations divisible by 4 </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      function countRotations(number) {
         //If the length of the number is equal to 1, check if the digit is a multiple of 4
         if (number < 10) {
            return number % 4 == 0 ? 1 : 0;
         } else {
            // Initialize count of rotations divisible by 4
            let count = 0;
            let num = number;
            //Check for the last digit and the first digit
            let lastDigit = number % 10;
            // Get the first digit from the number
            let firstDigit = Math.floor(number / Math.pow(10, Math.floor(Math.log10(number))));
            //If the last digit and first digit are divisible by 4, then add 1 to count
            if ((lastDigit * 10 + firstDigit) % 4 == 0) {
               count++;
            }
            while (num > 0) {
               // get last digit of number
               let lastDigit = num % 10;
               // get second last digit of number
               num = Math.floor(num / 10);
               let secondLastDigit = num % 10;
               if ((secondLastDigit * 10 + lastDigit) % 4 == 0) {
                  count++;
               }
            }
            return count;
         }
      }
      let number = 90645232432;
      output.innerHTML = "Total count of rotations divisible by 4 of " + number + " is " + countRotations(number);
   </script>
</body>
</html>

Users learned to find the total number of rotations of the number divisible by 4. We have seen two different approaches. The first approach converts a number to a string, rotates the string, again converts a string to a number and checks newly generated rotation is divisible by 4.

The second approach counts the total number of pairs of two digits divisible by 4.

Updated on: 20-Apr-2023

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements