Multiply a number by 15 without using * and / operators
Given a integer N, the task is to multiply the number with 15 without using multiplication * and division / operators.
Examples:
Input: N = 10
Output: 150
Input: N = 7
Output: 105
Method 1: We can multiply integer N by 15 using bitwise operators. First left shift the number by 4 bits which is equal to (16 * N) then subtract the original number N from the shifted number i.e. ((16 * N) – N) which is equal to 15 * N.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return (15 * N) without // using '*' or '/' operator long multiplyByFifteen(long n) { // prod = 16 * n long prod = (n << 4); // ((16 * n) - n) = 15 * n prod = prod - n; return prod; } // Driver code int main() { long n = 7; cout << multiplyByFifteen(n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return (15 * N) without // using '*' or '/' operator static long multiplyByFifteen(long n) { // prod = 16 * n long prod = (n << 4); // ((16 * n) - n) = 15 * n prod = prod - n; return prod; } // Driver code public static void main(String[] args) { long n = 7; System.out.print(multiplyByFifteen(n)); } } |
Python
# Python3 implementation of the approach # Function to return (15 * N) without # using '*' or '/' operator def multiplyByFifteen(n): # prod = 16 * n prod = (n << 4) # ((16 * n) - n) = 15 * n prod = prod - n return prod # Driver code n = 7print(multiplyByFifteen(n)) |
C#
// C# implementation of the approach using System; class GFG { // Function to return (15 * N) without // using '*' or '/' operator static long multiplyByFifteen(long n) { // prod = 16 * n long prod = (n << 4); // ((16 * n) - n) = 15 * n prod = prod - n; return prod; } // Driver code public static void Main() { long n = 7; Console.Write(multiplyByFifteen(n)); } } |
PHP
<?php // PHP implementation of the approach // Function to return (15 * N) without // using '*' or '/' operator function multiplyByFifteen($n) { // prod = 16 * n $prod = ($n << 4); // ((16 * n) - n) = 15 * n $prod = $prod - $n; return $prod; } // Driver code $n = 7; echo multiplyByFifteen($n); // This code is contributed by anuj_67.. ?> |
105
Method 2: We can also calculate the multiplication (15 * N) as sum of (8 * N) + (4 * N) + (2 * N) + N which can be obtained by performing left shift operations as (8 * N) = (N << 3), (4 * N) = (n << 2) and (2 * N) = (n << 1).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return (15 * N) without // using '*' or '/' operator long multiplyByFifteen(long n) { // prod = 8 * n long prod = (n << 3); // Add (4 * n) prod += (n << 2); // Add (2 * n) prod += (n << 1); // Add n prod += n; // (8 * n) + (4 * n) + (2 * n) + n = (15 * n) return prod; } // Driver code int main() { long n = 7; cout << multiplyByFifteen(n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return (15 * N) without // using '*' or '/' operator static long multiplyByFifteen(long n) { // prod = 8 * n long prod = (n << 3); // Add (4 * n) prod += (n << 2); // Add (2 * n) prod += (n << 1); // Add n prod += n; // (8 * n) + (4 * n) + (2 * n) + n = (15 * n) return prod; } // Driver code public static void main(String[] args) { long n = 7; System.out.print(multiplyByFifteen(n)); } } |
Python
# Python3 implementation of the approach # Function to perform Multiplication def multiplyByFifteen(n): # prod = 8 * n prod = (n << 3) # Add (4 * n) prod += (n << 2) # Add (2 * n) prod += (n << 1) # Add n prod += n # (8 * n) + (4 * n) + (2 * n) + n = (15 * n) return prod # Driver code n = 7print(multiplyByFifteen(n)) |
C#
// C# implementation of the approach using System; class GFG { // Function to return (15 * N) without // using '*' or '/' operator static long multiplyByFifteen(long n) { // prod = 8 * n long prod = (n << 3); // Add (4 * n) prod += (n << 2); // Add (2 * n) prod += (n << 1); // Add n prod += n; // (8 * n) + (4 * n) + (2 * n) + n = (15 * n) return prod; } // Driver code public static void Main() { long n = 7; Console.Write(multiplyByFifteen(n)); } } |
105
Recommended Posts:
- Russian Peasant (Multiply two numbers using bitwise operators)
- Find the smallest number whose digits multiply to a given number n
- Multiply any Number with 4 using Bitwise Operator
- Multiply a number with 10 without using multiplication operator
- Check if a number is divisible by 8 using bitwise operators
- Check if a number is divisible by 17 using bitwise operators
- Check if a number is multiple of 9 using bitwise operators
- Check if a number is positive, negative or zero using bit operators
- Operators in C | Set 2 (Relational and Logical Operators)
- Multiply a given Integer with 3.5
- Efficient way to multiply with 7
- Program to multiply two Matrix by taking data from user
- Bitwise Operators in C/C++
- Add two numbers without using arithmetic operators
- Subtract 1 without arithmetic operators
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m

