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:

[pastacode lang=”c” manual=”%23%20include%3Cstdio.h%3E%0A%20%0Aint%20multiplyBySeven(unsigned%20int%20n)%0A%7B%20%20%0A%20%20%20%20%2F*%20Note%20the%20inner%20bracket%20here.%20This%20is%20needed%20%0A%20%20%20%20%20%20%20because%20precedence%20of%20′-‘%20operator%20is%20higher%20%0A%20%20%20%20%20%20%20than%20’%3C%3C’%20*%2F%0A%20%20%20%20return%20((n%3C%3C3)%20-%20n)%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20unsigned%20int%20n%20%3D%204%3B%0A%20%20%20%20printf(%22%25u%22%2C%20multiplyBySeven(n))%3B%0A%20%0A%20%20%20%20getchar()%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

Time Complexity: O(1)
Space Complexity: O(1)

[ad type=”banner”]