Multiply a given Integer with 3.5 in C++

C++Server Side ProgrammingProgramming

In this tutorial, we are going to write a program that multiplies the given integer with 3.5 without using the multiplication operator (*).

o get the result of n * 3.5 we need to calculate (n * 2) + n + (n / 2). Moving the bits to left by 1 will give you n * 2 and moving the bits to right by will you n / 2. Add those to get the result.

It's a straightforward problem with the above steps. Let's see the code.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int getTheResult(int x) {
   return (x << 1) + x + (x >> 1);
}
int main() {
   int n = 7;
   cout << getTheResult(n) << endl;
   return 0;
}

Output

If you run the above code, then you will get the following result.

24

Conclusion

If you have any queries in the tutorial, mention them in the comment section.

raja
Published on 01-Jul-2021 07:01:12
Advertisements