Related Articles

Related Articles

Multiply a given Integer with 3.5
  • Difficulty Level : Easy
  • Last Updated : 16 Oct, 2020

Given an integer x, write a function that multiplies x with 3.5 and returns the integer result. You are not allowed to use %, /, *. 

Examples :
Input: 2
Output: 7

Input: 5
Output: 17 (Ignore the digits after decimal point)

Solution: 
1. We can get x*3.5 by adding 2*x, x and x/2. To calculate 2*x, left shift x by 1 and to calculate x/2, right shift x by 2. 
 Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to multiply
// a number with 3.5
#include <bits/stdc++.h>
  
int multiplyWith3Point5(int x)
{
    return (x<<1) + x + (x>>1);
  
/* Driver program to test above functions*/
int main()
{
    int x = 4; 
    printf("%d", multiplyWith3Point5(x));
    getchar();
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to multiply
// a number with 3.5
  
class GFG {
          
    static int multiplyWith3Point5(int x)
    {
        return (x<<1) + x + (x>>1);
    
      
    /* Driver program to test above functions*/
    public static void main(String[] args)
    {
        int x = 2
        System.out.println(multiplyWith3Point5(x));
    }
}
  
// This code is contributed by prerna saini.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to multiply
# a number with 3.5
  
def multiplyWith3Point5(x):
  
    return (x<<1) + x + (x>>1)
   
  
# Driver program to 
# test above functions
x = 4 
print(multiplyWith3Point5(x))
  
# This code is contributed by
# Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to multiply
// a number with 3.5
using System;
  
class GFG
{
          
    static int multiplyWith3Point5(int x)
    {
        return (x<<1) + x + (x>>1);
    
      
    /* Driver program to test above functions*/
    public static void Main()
    {
        int x = 2; 
        Console.Write(multiplyWith3Point5(x));
    }
      
}
  
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to multiply
// a number with 3.5
  
  
function multiplyWith3Point5( $x)
{
    return ($x << 1) + $x + ($x >> 1);
  
// Driver Code
$x = 4; 
echo multiplyWith3Point5($x);
      
// This code is contributed by vt_m.
?>

chevron_right


Output

14

2. Another way of doing this could be (8*x – x)/2 (See below code). Thanks to Ajaym for suggesting this.

C

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int multiplyWith3Point5(int x)
{
  return ((x<<3) - x)>>1;
}    

chevron_right


 Another Approach:



Another way of doing this could be by doing a binary multiplication by 7 then divide by 2 using only <<, ^, &, and >>.

But here we have to mention that only positive numbers can be passed to this method.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for above approach
import java.io.*;
  
class GFG 
{
     
    // Function to multiple number
    // with 3.5
    static int multiplyWith3Point5(int x)
    {
        int r = 0;
  
        // The 3.5 is 7/2, so multiply 
        // by 7 (x * 7) then
        // divide the result by 2 
        // (result/2) x * 7 -> 7 is
        // 0111 so by doing mutiply 
        // by 7 it means we do 2
        // shifting for the number 
        // but since we doing
        // multiply we need to take 
        // care of carry one.
        int x1Shift = x << 1;
        int x2Shifts = x << 2;
  
        r = (x ^ x1Shift) ^ x2Shifts;
        int c = (x & x1Shift) | (x & x2Shifts)
                | (x1Shift & x2Shifts);
        while (c > 0) {
            c <<= 1;
            int t = r;
            r ^= c;
            c &= t;
        }
  
        // Then divide by 2 
        // r / 2
        r = r >> 1;
        return r;
    }
    
    // Driver Code
    public static void main(String[] args)
    {
        System.out.println(multiplyWith3Point5(5));
    }
}

chevron_right


Output

17

Please write comments if you find the above code/algorithm incorrect, or find better ways to solve the same problem

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :