# Multiply the given number by 2 such that it is divisible by 10

This problem statement says that we are allowed to perform only one operation i.e. multiply the given number by 2 such that it is divisible by 10.

We will be given a number say n. The only operation that we can perform on a given number is that we can multiply the given number by 2 until it is divisible by 10. We need to determine the minimum number of operations required to make the number such that it is divisible by 10 by repeatedly multiplying the given number n by 2.

Else, print -1 if it is not possible to convert the number such that it is divisible by 10.

Example

INPUT: 25

OUTPUT: 1

Since multiplying the given number with 2 one time will give 50 which is divisible by 10.

INPUT: 20

OUTPUT: 0

The given number is already divisible by 10, so we don’t need to perform any set of operations on it.

INPUT: 2

OUTPUT: -1

Since multiplying the given number i.e 2 by 2 repeatedly never gives a number that will be divisible by 10. Thus, the output will be -1.

## Algorithm

We are going to solve this problem with a simple mathematics algorithm. Below is a step-by-step illustration of the approach that we are gonna use to solve this problem.

• Any number is divisible by 10 only if the unit digit of the given number is 0.

• So for this problem, we will take the unit digit of the given number and check it to solve this problem.

• If the last digit of the number will be 0, the least number of operations required to make the given number divisible by 10 will be 0 since it is already divisible by 10.

• If the last digit of the number is 5, then the minimum number of operations required will be 1 since it will be divided by 10 after being multiplied by 2 once.

• If the last digit of the number is any even number or an odd number (other than 0 or 5), multiplying it by 2 will always give an even number making it impossible to make a number divisible by 10. Thus, the output will be -1 since it is impossible to convert the given number into a number divisible by 10 by multiplying it by 2.

## Approach

### Approach-1 (using if-else-if)

In this approach we will use the same above algorithm using if-else-if conditional statements to get our output.

• We will declare two variables, first remainder to store the unit digit of the given number and oprtn to store the number of operations we need to perform.

• Then we’ll check the remainder using if-else-if statements and store the answer in oprtn according to the condition it will satisfy.

• And then finally we will return oprtn which will be our desired output.

### Example

Below is the C++ implementation of the above approach −

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int numberOf(int n){   //function to calculate no of operations
int remainder, oprtn;

remainder= n%10;  // to get the unit digit of the given number

if(remainder==0){  //if the unit digit is 0 or it is divisible by 10
oprtn=0;
}

else if(remainder==5){   //if the unit digit is 5, no of operation required is 1
oprtn=1;
}

else{   //if the unit digit is ny number other than 0 or 5
oprtn=-1;
}

return oprtn;

}
int main(){
int n=4;
cout<<numberOf(n)<<endl;

n=15;
cout<<numberOf(n)<<endl;

return 0;
}


### Output

-1
1


Time Complexity: O(1), since constant operations are carried out.

Space Complexity: O(1).

### Approach-2 (using nested if else statements)

In this approach, we will solve the problem using nested if else statements.

• Check if the number is divisible by 10. If it is print 0.

• If not then multiply the number by 2 and then again check if it is divisible by 10. If it is, that means the unit digit of the given number is 5 so print 1 since we require one operation to make it divisible by 10.

• Else, print -1 because the unit digit is any number other than 0 or 5 and it is impossible to make the number divisible by 10 just by multiplying it by 2.

### Example

Below is the C++ implementation of the above approach −

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

//Driver Code
int main(){
int N=13;

if(N%10==0){  //if the number is divisible by 10
cout<<0;
}

else{  //nested if-else
N=N*2; //multiplying the number by 2
if(N%10==0){  //if number is divisible by 10 after multiplying by 2 i.e. last digit was 5
cout<<1;
} else {  //the unit digit is any number other than 0 or 5
cout<<-1;
}
}

return 0;
}


### Output

-1


Since it is impossible to make 13 divisible by 10 just by multiplying it by 2.

Time Complexity : O(1), since constant operations are carried out.

Space Complexity : O(1).

## Conclusion

In this article, we have learned to solve the problem to find out the minimum number of operations required to make a given number divisible by 10 by multiplying the given number by 2. We have tried to solve the problem with two different approaches. I hope this article helps you in understanding the problem and clear your concept about the problem.