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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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)
- How to swap two bits in a given integer?
- Check for Integer Overflow
- Count set bits in an integer
- Find One's Complement of an Integer
- Next greater integer having one more number of set bits
- Compute the integer absolute value (abs) without branching
- Find closest integer with the same weight
- Minimum numbers needed to express every integer below N as a sum
- Count set bits in an integer using Lookup Table
- Previous smaller integer having one less number of set bits
- 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