Count rotations divisible by 8 in C++

We are given a large number. The goal is to count the rotations of num that are divisible by 8.

As the rotations can not be done again and again. We will use the divisible by 8 property. If the last three digits are divisible by 8then the number is divisible by 8. If the number is 1800 then it’s rotations will be 1800, 0180, 0018, 8001 out of 1800 is divisible by 8.

Let us understand with examples.

Input − num=15320

Output − Count of rotations divisible by 4 are: 1

Explanation − Rotations are −

15320, 01532, 20153, 32015, 53201
Out of these, only 15320 is divisible by 8.

Input − num=848484

Output − Count of rotations divisible by 4 are: 3

Explanation − Rotations are −

848484, 484848, 848484, 484848, 848484, 484848
Out of this all 484848’s are divisible by 8.

The approach used in the below program is as follows

We will convert the number into a string and traverse the number using a for a loop. For each pair of three digits convert them into an integer and check divisibility by 8. If divisible then increment the count.

• Take the number as long long num.

• Function Rotation_8(long long num) takes the number num and returns the count of rotations of num that are divisible by 8.

• Convert the num to string str=to_string(num).

• The number of digits in num will be length=str.length().

• Take temporary variable digit=0 for storing integer values of three digits.

• Take the initial count as 0.

• If length is 1 then only a single digit is present. Convert it to integer, digit=(str.at(0)-’0’). Check divisibility by 8 and return the result as 1 or 0.

• If length is 2 then only two digits are present. Convert them to integers, part_1=(str.at(0)- ’0’) and part_2 = (str[1] - '0') * 10 + (str[0] - '0'). Check divisibility by 8 and return the result as 1 or 0.

• Otherwise for length more than or equal to three digits, traverse string using for loop from i=0 to i=length-1 and convert the three characters to integer value by digit = (str[i] - '0') * 100 + (str[i + 1] - '0') * 10 + (str[i + 2] - '0'); . If the value of the digit is divisible by 8 increment count.

• Do same process as above for pair formed by last digit and first two digits using digit = (str[length - 1] - '0') * 100 + (str[0] - '0') * 10 + (str[1] - '0');

• Check divisibility by 8 and update count.

• At the end return count as result.

Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int Rotation_8(long long num){
string str = to_string(num);
int length = str.length();
int digit = 0, count = 0;
if (length == 1){
if(digit % 8 == 0){
return 1;
}
else{
return 0;
}
}
else if(length == 2){
int part_1 = (str[0] - '0') * 10 + (str[1] - '0');
int part_2 = (str[1] - '0') * 10 + (str[0] - '0');
if (part_1 % 8 == 0){
count++;
}
if (part_2 % 8 == 0){
count++;
}
return count;
}
else{
for(int i = 0; i < (length - 2); i++){
digit = (str[i] - '0') * 100 + (str[i + 1] - '0') * 10 + (str[i + 2] - '0');
if (digit % 8 == 0){
count++;
}
}
}
digit = (str[length - 1] - '0') * 100 + (str[0] - '0') * 10 + (str[1] - '0');
if(digit % 8 == 0){
count++;
}
digit = (str[length - 2] - '0') * 100 + (str[length - 1] - '0') * 10 + (str[0] - '0');
if(digit%8 == 0){
count++;
}
return count;
}
int main(){
long long num = 24040;
cout<<"Count of rotations divisible by 8 are: "<<Rotation_8(num);
return 0;
}

Output

If we run the above code it will generate the following output −

Count of rotations divisible by 8 are: 3