Related Articles
Efficient way to multiply with 7
• Difficulty Level : Easy
• Last Updated : 01 Apr, 2021

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

 `# include` `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<

## C

 `# include` `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

 `// 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

 `# Python program to multiply any``# positive number to 7` `# Function to mutiply any number with 7``def` `multiplyBySeven(n):` `    ``# Note the inner bracket here.``    ``# This is needed because``    ``# precedence of '-' operator is``    ``# higher than '<<'``    ``return` `((n << ``3``) ``-` `n)` `# Driver code``n ``=` `4``print``(multiplyBySeven(n))` `# This code is contributed by Danish Raza`

## C#

 `// C# program to multiply any``// positive number to 7``using` `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

 ``

## Javascript

 ``

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.

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