Efficient way to multiply with 7
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<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; } |
chevron_right
filter_none
C
# 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; } |
chevron_right
filter_none
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. |
chevron_right
filter_none
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 = 4print(multiplyBySeven(n)) # This code is contributed by Danish Raza |
chevron_right
filter_none
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 |
chevron_right
filter_none
PHP
<?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. ?> |
chevron_right
filter_none
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.
Recommended Posts:
- Multiply two polynomials
- Multiply a given Integer with 3.5
- Program to multiply two matrices
- Multiply the given number by 2 such that it is divisible by 10
- Multiply a number by 15 without using * and / operators
- Efficient program to calculate e^x
- Multiply a number with 10 without using multiplication operator
- Multiply any Number with 4 using Bitwise Operator
- Efficient Program to Compute Sum of Series 1/1! + 1/2! + 1/3! + 1/4! + .. + 1/n!
- Ways to multiply n elements with an associative operation
- Multiply Large Numbers represented as Strings
- An efficient way to check whether n-th Fibonacci number is multiple of 10
- Space and time efficient Binomial Coefficient
- Efficient method for 2's complement of a binary string
- Check if a given number is Pronic | Efficient Approach



