📜  将数字乘以15而不使用*和/运算符

📅  最后修改于: 2021-05-25 04:23:55             🧑  作者: Mango

给定整数N ,任务是将数字乘以15,而不使用乘法*和除法/运算符。

例子:

方法1:我们可以使用按位运算运算符将整数N乘以15 。首先左移数字等于(16 * N)4位,然后从移位后的数字中减去原始数字N ,即((16 * N)– N)等于15 *N

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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 = 7
print(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


Javascript


C++
// C++ implementation of the approach
#include 
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));
    }
}


Python3
# 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 = 7
print(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));
    }
}


Javascript


输出:
105

方法2:我们还可以将乘积(15 * N)计算为(8 * N)+(4 * N)+(2 * N)+ N的总和,可以通过执行(8 * N )=(N << 3)(4 * N)=(n << 2)(2 * N)=(n << 1)

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
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));
    }
}

Python3

# 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 = 7
print(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));
    }
}

Java脚本


输出:
105

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。