Efficient way to multiply with 7 - GeeksforGeeks

06 Jan, 2022
We can multiply a number by 7 using bitwise operator. First left shift the number by 3 bits (you will get 8n) then subtract the original numberfrom the shifted number and return the difference (8n – n). Program:
CPP
C
Java
Python3
C#
PHP
Javascript
# include<bits/stdc++.h> using namespace std; //c++ implementation long multiplyBySeven(long n){      /* Note the inner bracket here. This is needed        because precedence of '-' operator is higher        than '<<' */    return ((n<<3) - n);}   /* Driver program to test above function */int main(){    long n = 4;         cout<<multiplyBySeven(n);       return 0;}
# include<stdio.h> int multiplyBySeven(unsigned int n){     /* Note the inner bracket here. This is needed       because precedence of '-' operator is higher       than '<<' */    return ((n<<3) - n);} /* Driver program to test above function */int main(){    unsigned int n = 4;    printf("%u", multiplyBySeven(n));     getchar();    return 0;}
// Java program to multiply any// positive number to 7 class GFG {         static int multiplyBySeven(int n)    {        /* Note the inner bracket here.        This is needed because precedence        of '-' operator is higher        than '<<' */        return ((n << 3) - n);    }         // Driver code    public static void main (String arg[])    {        int n = 4;                 System.out.println(multiplyBySeven(n));    }} // This code is contributed by Anant Agarwal.
# Python program to multiply any# positive number to 7 # Function to multiply any number with 7def multiplyBySeven(n):     # Note the inner bracket here.    # This is needed because    # precedence of '-' operator is    # higher than '<<'    return ((n << 3) - n) # Driver coden = 4print(multiplyBySeven(n)) # This code is contributed by Danish Raza
// C# program to multiply any// positive number to 7using System; class GFG{    static int multiplyBySeven(int n)    {        /* Note the inner bracket here. This is needed        because precedence of '-' operator is higher        than '<<' */        return ((n << 3) - n);    }         // Driver code    public static void Main ()    {        int n = 4;        Console.Write(multiplyBySeven(n));    }} // This code is contributed by Sam007
<?php  function multiplyBySeven($n){     // Note the inner bracket here.    // This is needed because    // precedence of '-' operator     // is higherthan '<<'    return (($n<<3) - $n);}     // Driver Code    $n = 4;    echo multiplyBySeven($n); // This code is contributed by vt_m.?>
<script>   function multiplyBySeven(n){      /* Note the inner bracket here. This is needed      because precedence of '-' operator is higher      than '<<' */     return ((n << 3) - n);}   // Driver program to test above function      n = 4;    document.write(multiplyBySeven(n));       // This code is contributed by simranarora5sos</script>
Output:
28
Time Complexity: O(1) Space Complexity: O(1)Note: Works only for positive integers. Same concept can be used for fast multiplication by 9 or other numbers.
vt_m
mohit kumar 29
simranarora5sos
simmytarika5
amartyaghoshgfg
Bit Magic
Mathematical
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
How to swap two numbers without using a temporary variable?
C++ Data Types
Set in C++ Standard Template Library (STL)
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples