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: 150Input: 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 '/' operatorlong multiplyByFifteen(long n){ // prod = 16 * n long prod = (n << 4); // ((16 * n) - n) = 15 * n prod = prod - n; return prod;}// Driver codeint main(){ long n = 7; cout << multiplyByFifteen(n); return 0;} |
Java
// Java implementation of the approachclass 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)); }} |
Python3
# Python3 implementation of the approach # Function to return (15 * N) without # using '*' or '/' operatordef multiplyByFifteen(n): # prod = 16 * n prod = (n << 4) # ((16 * n) - n) = 15 * n prod = prod - n return prod # Driver coden = 7print(multiplyByFifteen(n)) |
C#
// C# implementation of the approachusing 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 '/' operatorfunction 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..?> |
Javascript
<script>// JavaScript implementation of the approach // Function to return (15 * N) without // using '*' or '/' operator function multiplyByFifteen(n) { // prod = 16 * n let prod = (n << 4); // ((16 * n) - n) = 15 * n prod = prod - n; return prod; } // Driver code let n = 7; document.write(multiplyByFifteen(n)); // This code is contributed by Surbhi Tyagi.</script> |
105
Time Complexity: O(1)
Auxiliary Space: O(1)
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 '/' operatorlong 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 codeint main(){ long n = 7; cout << multiplyByFifteen(n); return 0;} |
Java
// Java implementation of the approachclass 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)); }} |
Python3
# Python3 implementation of the approach # Function to perform Multiplicationdef 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 coden = 7print(multiplyByFifteen(n)) |
C#
// C# implementation of the approachusing 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)); }} |
Javascript
<script>// Javascript implementation of the approach // Function to return (15 * N) without// using '*' or '/' operatorfunction multiplyByFifteen(n){ // prod = 8 * n var 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 codevar n = 7;document.write(multiplyByFifteen(n));// This code is contributed by shikhasingrajput </script> |
105
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



