Given a 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)
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 2.
2. Another way of doing this could be (8*x – x)/2 (See below code). Thanks to Ajaym for suggesting this.
Please write comments if you find the above code/algorithm incorrect, or find better ways to solve the same problem
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Cyclic shifts of integer N by another integer m
- Efficient way to multiply with 7
- Multiply a number by 15 without using * and / operators
- Multiply any Number with 4 using Bitwise Operator
- Multiply a number with 10 without using multiplication operator
- Russian Peasant (Multiply two numbers using bitwise operators)
- Check for Integer Overflow
- How to swap two bits in a given integer?
- Find One's Complement of an Integer
- Count set bits in an integer
- Find closest integer with the same weight
- Next greater integer having one more number of set bits
- Compute the integer absolute value (abs) without branching
- Count set bits in an integer using Lookup Table
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Previous smaller integer having one less number of set bits
- Minimum numbers needed to express every integer below N as a sum
- Choose an integer K such that maximum of the xor values of K with all Array elements is minimized
- Find the largest number smaller than integer N with maximum number of set bits
Improved By : vt_m