📌  相关文章
📜  将两个整数相乘而不使用乘法,除法和按位运算运算符,并且不使用循环

📅  最后修改于: 2021-05-04 22:13:41             🧑  作者: Mango

通过使用递归,我们可以将两个整数与给定的约束相乘。
若要将x和y相乘,请递归加xy次。

C++
// C++ program to Multiply two integers without
// using multiplication, division and bitwise
//  operators, and no loops
#include
 
using namespace std;
class GFG
{
     
/* function to multiply two numbers x and y*/
public : int multiply(int x, int y)
{
    /* 0 multiplied with anything gives 0 */
    if(y == 0)
    return 0;
 
    /* Add x one by one */
    if(y > 0 )
    return (x + multiply(x, y-1));
 
    /* the case where y is negative */
    if(y < 0 )
    return -multiply(x, -y);
}
};
 
// Driver code
int main()
{
    GFG g;
    cout << endl << g.multiply(5, -11);
    getchar();
    return 0;
}
 
// This code is contributed by SoM15242


C
#include
/* function to multiply two numbers x and y*/
int multiply(int x, int y)
{
   /* 0  multiplied with anything gives 0 */
   if(y == 0)
     return 0;
 
   /* Add x one by one */
   if(y > 0 )
     return (x + multiply(x, y-1));
  
  /* the case where y is negative */
   if(y < 0 )
     return -multiply(x, -y);
}
 
int main()
{
  printf("\n %d", multiply(5, -11));
  getchar();
  return 0;
}


Java
class GFG {
     
    /* function to multiply two numbers x and y*/
    static int multiply(int x, int y) {
         
        /* 0 multiplied with anything gives 0 */
        if (y == 0)
            return 0;
     
        /* Add x one by one */
        if (y > 0)
            return (x + multiply(x, y - 1));
     
        /* the case where y is negative */
        if (y < 0)
            return -multiply(x, -y);
             
        return -1;
    }
     
    // Driver code
    public static void main(String[] args) {
         
        System.out.print("\n" + multiply(5, -11));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Function to multiply two numbers
# x and y
def multiply(x,y):
 
    # 0 multiplied with anything
    # gives 0
    if(y == 0):
        return 0
 
    # Add x one by one
    if(y > 0 ):
        return (x + multiply(x, y - 1))
 
    # The case where y is negative
    if(y < 0 ):
        return -multiply(x, -y)
     
# Driver code
print(multiply(5, -11))
 
# This code is contributed by Anant Agarwal.


C#
// Multiply two integers without
// using multiplication, division
// and bitwise operators, and no
// loops
using System;
 
class GFG {
     
    // function to multiply two numbers
    // x and y
    static int multiply(int x, int y) {
         
        // 0 multiplied with anything gives 0
        if (y == 0)
            return 0;
     
        // Add x one by one
        if (y > 0)
            return (x + multiply(x, y - 1));
     
        // the case where y is negative
        if (y < 0)
            return -multiply(x, -y);
             
        return -1;
    }
     
    // Driver code
    public static void Main() {
         
        Console.WriteLine(multiply(5, -11));
    }
}
 
// This code is contributed by vt_m.


PHP
 0 )
    return ($x + multiply($x,
                          $y - 1));
 
/* the case where
y is negative */
if($y < 0 )
    return -multiply($x, -$y);
}
 
// Driver Code
echo multiply(5, -11);
 
// This code is contributed by mits.
?>


Javascript


输出:

-55