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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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?
- Find One's Complement of an Integer
- Check for Integer Overflow
- Count set bits in an integer
- Compute the integer absolute value (abs) without branching
- Find closest integer with the same weight
- Next greater integer having one more number of set bits
- Count set bits in an integer using Lookup Table
- Previous smaller integer having one less number of set bits
Improved By : vt_m