Skip to content
Related Articles

Related Articles

Improve Article
Multiply the given number by 2 such that it is divisible by 10
  • Difficulty Level : Basic
  • Last Updated : 04 May, 2021

Given a number, the only operation allowed is to multiply the number by 2. Calculate the minimum number of operations to make the number divisible by 10.
NOTE: If it is not possible to convert then print -1.
Examples: 
 

Input: 10 
Output:
As the given number is itself divisible by 10, 
the answer is 0.
Input:
Output: -1 
As by multiplying with 2, given no. can’t be 
converted into a number that is divisible by 10, 
therefore the answer is -1.
 

 

Approach: Any given number is divisible by 10 only if the last digit of the number is 0. For this problem, extract the last digit of the input number and check it in the following ways : 
1) If the last digit is 0 then it is already divisible by 10 , so the minimum number of steps is 0.
2) If the last digit is 5 then multiplying it by 2 one time will make it divisible by 10, so the minimum number of steps is 1.
3) If the last digit is an even or odd number (apart from 0 and 5) then multiplying it by 2 any number of times will only produce even number so we can never make it divisible by 10. Therefore the number of steps is -1.
 

C++




// C++ code for finding
// number of operations
#include <bits/stdc++.h>
using namespace std;
 
int multiplyBy2(int n)
{
    int rem, value;
 
    // Find the last digit or remainder
    rem = n % 10;
    switch (rem) {
 
    // If the last digit is 0
    case 0:
        value = 0;
        break;
 
    // If the last digit is 5
    case 5:
        value = 1;
        break;
 
    // If last digit is other
    // than 0 and 5.
    default:
        value = -1;
    }
 
    return value;
}
 
// Driver code
int main()
{
 
    int n = 28;
    cout << multiplyBy2(n) << endl;
 
    n = 255;
    cout << multiplyBy2(n) << endl;
 
  return 0;
}

Java




// JAVA code for finding
// number of operations
import java.io.*;
 
class GFG
{
    static int multiplyBy2(int n)
{
    int rem, value;
 
    // Find the last digit
    // or remainder
    rem = n % 10;
    switch (rem)
    {
 
    // If the last digit is 0
    case 0:
        value = 0;
        break;
 
    // If the last digit is 5
    case 5:
        value = 1;
        break;
 
    // If last digit is other
    // than 0 and 5.
    default:
        value = -1;
    }
 
    return value;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 28;
    System.out.println(multiplyBy2(n));
 
    n = 255;
    System.out.println(multiplyBy2(n));
}
}
 
// This code is contributed
// by shiv_bhakt.

Python3




# Python3 code for finding number
# of operations
def dig(argu):
    switcher = {
        0: 0,
        5: 1,
    }
    return switcher.get(argu, -1)
     
def multiplyBy2(n):
 
    # Find the last digit or remainder
    rem = n % 10;
    return dig(rem);
 
# Driver code
n = 28;
print(multiplyBy2(n));
 
n = 255;
print(multiplyBy2(n));
         
# This code is contributed by mits

C#




// C# code for finding
// number of operations
using System;
 
class GFG
{
    static int multiplyBy2(int n)
    {
    int rem, value;
 
    // Find the last digit
    // or remainder
    rem = n % 10;
    switch (rem)
    {
 
    // If the last
    // digit is 0
    case 0:
        value = 0;
        break;
 
    // If the last
    // digit is 5
    case 5:
        value = 1;
        break;
 
    // If last digit is
    // other than 0 and 5.
    default:
        value = -1;
        break;
    }
 
    return value;
}
 
// Driver code
public static void Main ()
{
    int n = 28;
    Console.WriteLine(multiplyBy2(n));
 
    n = 255;
    Console.WriteLine(multiplyBy2(n));
}
}
 
// This code is contributed
// by shiv_bhakt.

PHP




<?php
// PHP  code for finding
// number of operations
 
function  multiplyBy2($n)
{
     $rem;
     $value;
 
    // Find the last digit or remainder
    $rem = $n % 10;
    switch ($rem) {
 
    // If the last digit is 0
    case 0:
        $value = 0;
        break;
 
    // If the last digit is 5
    case 5:
        $value = 1;
        break;
 
    // If last digit is other
    // than 0 and 5.
    default:
        $value = -1;
    }
 
    return $value;
}
 
// Driver code
    $n = 28;
    echo  multiplyBy2($n),"\n";
 
    $n = 255;
        echo  multiplyBy2($n),"\n";
         
// This code is contributed by aj_36
?>

Javascript




<script>
 
// Javascript code for finding
// number of operations
 
    function multiplyBy2(n)
    {
        var rem, value;
 
        // Find the last digit
        // or remainder
        rem = n % 10;
        switch (rem) {
 
        // If the last digit is 0
        case 0:
            value = 0;
            break;
 
        // If the last digit is 5
        case 5:
            value = 1;
            break;
 
        // If last digit is other
        // than 0 and 5.
        default:
            value = -1;
        }
 
        return value;
    }
 
    // Driver code
     
        var n = 28;
        document.write(multiplyBy2(n)+"<br/>");
 
        n = 255;
        document.write(multiplyBy2(n));
 
// This code is contributed by todaysgaurav
 
</script>
Output: 
-1
1

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :