Multiply a given Integer with 3.5 - GeeksforGeeks

13 Oct, 2021
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 1.  Below is the implementation of the above approach:
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to multiply// a number with 3.5#include <iostream>using namespace std; int multiplyWith3Point5(int x){    return (x<<1) + x + (x>>1);} /* Driver program to test above functions*/int main(){    int x = 4;    cout << " "<< multiplyWith3Point5(x);    getchar();    return 0;}  // This code is contributed by shivanisinghss2110.
// C++ program to multiply// a number with 3.5#include <stdio.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;}
// 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.
# Python 3 program to multiply# a number with 3.5 def multiplyWith3Point5(x):     return (x<<1) + x + (x>>1)   # Driver program to# test above functionsx = 4print(multiplyWith3Point5(x)) # This code is contributed by# Smitha Dinesh Semwal
// C# Program to multiply// a number with 3.5using 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
<?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.?>
<script>// javascript Program to multiply// a number with 3.5  function multiplyWith3Point5(x){    return (x<<1) + x + (x>>1);} /* Driver program to test above functions*/var x = 4;document.write(multiplyWith3Point5(x)); // This code is contributed by Amit Katiyar</script>
14
2. Another way of doing this could be (8*x – x)/2 (See below code). Thanks to Ajaym for suggesting this.
C++
C
Java
Python3
C#
Javascript
// C++ program approach #include <iostream>using namespace std; int multiplyWith3Point5(int x){  return ((x<<3) - x)>>1;}    // This code is contributed by shivanisinghss2110
#include <stdio.h>int multiplyWith3Point5(int x){  return ((x<<3) - x)>>1;}   
// Java program for above approachimport java.io.*; class GFG{   // Function  static int multiplyWith3Point5(int x)  {    return ((x<<3) - x)>>1;  }} // This code is contributed by shivanisinghss2110
# Python program for above approach# Functiondef multiplyWith3Point5(x):       return ((x<<3) - x)>>1   # This code is contributed by shivanisinghss2110
// C# program for above approachusing System; class GFG{ // Function to multiple number// with 3.5static int multiplyWith3Point5(int x) {    return ((x<<3) - x)>>1;  }} // This code is contributed by shivanisinghss2110.
// JavaScript program for above approach// Functionfunction multiplyWith3Point5(x)  {    return ((x<<3) - x)>>1;  } // This code is contributed by shivanisinghss2110
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:
C++
Java
Python3
C#
Javascript
// C++ program for above approach#include <iostream>using namespace std; // Function to multiple number// with 3.5int 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 multiply        // 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 Codeint main() {    cout<<(multiplyWith3Point5(5));    return 0;} // This code is contributed by rohitsingh07052.
// Java program for above approachimport 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 multiply        // 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));    }}
# Python3 program for the above approach # Function to multiple number# with 3.5def multiplyWith3Point5(x):      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 multiply    # by 7 it means we do 2    # shifting for the number    # but since we doing    # multiply we need to take    # care of carry one.       x1Shift = x << 1    x2Shifts = x << 2     r = (x ^ x1Shift) ^ x2Shifts    c = (x & x1Shift) | (x & x2Shifts) | (x1Shift & x2Shifts)    while (c > 0):        c <<= 1        t = r        r ^= c        c &= t     # Then divide by 2    # r / 2    r = r >> 1    return r  # Driver Codeif __name__ == '__main__':    print(multiplyWith3Point5(5)) # This code is contributed by nirajgusain5
// C# program for above approachusing System; class GFG{ // Function to multiple number// with 3.5static 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 multiply    // 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 Codepublic static void Main(string[] args){    Console.WriteLine(multiplyWith3Point5(5));}} // This code is contributed by ukasp
<script> // Javascript program for the above approach // Function to multiple number// with 3.5function multiplyWith3Polet5(x){    let 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 multiply    // by 7 it means we do 2    // shifting for the number    // but since we doing    // multiply we need to take    // care of carry one.    let x1Shift = x << 1;    let x2Shifts = x << 2;     r = (x ^ x1Shift) ^ x2Shifts;    let c = (x & x1Shift) | (x & x2Shifts) |      (x1Shift & x2Shifts);           while (c > 0)    {        c <<= 1;        let t = r;        r ^= c;        c &= t;    }     // Then divide by 2    // r / 2    r = r >> 1;    return r;}     // Driver codedocument.write(multiplyWith3Polet5(5));    // This code is contributed by avijitmondal1998 </script>
17
YouTube<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=oG2_XmdQnVs" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Please write comments if you find the above code/algorithm incorrect, or find better ways to solve the same problem
 
vt_m
ghanykamal
amit143katiyar
rohitsingh07052
ukasp
avijitmondal1998
nirajgusain5
rajeev0719singh
shivanisinghss2110
piyushsachan1
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find the element that appears once
Bits manipulation (Important tactics)
Set, Clear and Toggle a given bit of a number in C
Bit Fields in C
C++ bitset and its application
Write an Efficient C Program to Reverse Bits of a Number
Check whether K-th bit is set or not
Count total set bits in all numbers from 1 to n
Divide two integers without using multiplication, division and mod operator
1's and 2's complement of a Binary Number