
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multiply a number by 15 without using * and / operators in C++
We can using left shift (<<) operator to multiply with 15. If we left shift 1, then we are multiplying it with 2.
If we left shift the given number with 4, then we will get the 16 * n. Subtracting the given number from 16 * n will result in 15 * n.
Or
We can also divide it as 8 * n + 4 * n + 2 * n + n. You can easily multiply the powers of 2 using left shift.
Algorithm
- Initialise the number n.
- Find the n << 4 to get 16 * n.
- Subtract n from the above result.
- Return the final answer.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; long long getMultiplicationResult(long long n) { return (n << 4) - n; } int main() { long long n = 15; cout << getMultiplicationResult(n) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
225
- Related Questions & Answers
- How to multiply a given number by 2 using Bitwise Operators in C#?
- Check if a number is multiple of 5 without using / and % operators in C++
- Check if a large number is divisibility by 15 in C++
- Write you own Power without using multiplication(*) and division(/) operators in C Program
- Find maximum in an array without using Relational Operators in C++
- Find minimum in an array without using Relational Operators in C++
- How to sum two integers without using arithmetic operators in C/C++?
- Multiply any Number with using Bitwise Operator in C++
- How to sum two integers without using arithmetic operators in C/C++ Program?
- Check if n is divisible by power of 2 without using arithmetic operators in Python
- How to multiply all values in a list by a number in R?
- Print a number 100 times without using loop, recursion and macro expansion in C
- # and ## Operators in C ?
- Differentiate a polynomial and multiply each differentiation by a scalar in Python
- How to find the missing number and the repeated number in a sorted array without using any inbuilt functions using C#?
Advertisements