División sin usar el operador ‘/’

Dados dos números, divide uno de otro sin usar el operador ‘/’. 
Ejemplos: 
 

Input : num1 = 13, num2 = 2
Output : 6

Input : num1 = 14, num2 = -2
Output : -7

Input : num1 = -11, num2 = 3
Output : -3

Input : num1 = 10, num2 = 10
Output : 1

Input : num1 = -6, num2 = -2
Output : 3

Input : num1 = 0, num2 = 5
Output : 0

Para realizar la operación de división sin usar el operador ‘/’, seguimos el enfoque, en el que contamos el número de restas exitosas o completas de num2 de num1. Donde num1 es el número a dividir y num2 es el número del que tenemos que dividir num1.
 

C++

// CPP program to divide a number by other
// without using / operator
#include <bits/stdc++.h>
using namespace std;
 
// Function to find division without using
// '/' operator
int division(int num1, int num2)
{
   if (num1 == 0)
       return 0;
   if (num2 == 0)
     return INT_MAX;
 
   bool negResult = false;
 
   // Handling negative numbers
   if (num1 < 0)
   {
       num1 = -num1 ;
       if (num2 < 0)
           num2 = - num2 ;
       else
           negResult = true;
   }
   else if (num2 < 0)
   {
       num2 = - num2 ;
       negResult = true;
   }
 
   // if num1 is greater than equal to num2
   // subtract num2 from num1 and increase
   // quotient by one.
   int quotient = 0;
   while (num1 >= num2)
   {
       num1 = num1 - num2 ;
       quotient++ ;
   }
 
   // checking if neg equals to 1 then making
   // quotient negative
   if (negResult)
        quotient = - quotient ;
   return quotient ;
}
 
// Driver program
int main()
{
    int num1 = 13, num2 = 2 ;
    cout << division(num1, num2); ;
    return 0;
}

Java

// Java program to divide a number by other
// without using / operator
import java.io.*;
 
class GFG
{
    // Function to find division without using
    // '/' operator
    static int division(int num1, int num2)
    {
        if (num1 == 0)
            return 0;
        if (num2 == 0)
            return Integer.MAX_VALUE;
         
        boolean negResult = false;
         
        // Handling negative numbers
        if (num1 < 0)
        {
            num1 = -num1 ;
            if (num2 < 0)
                num2 = - num2 ;
            else
                negResult = true;
        }
        else if (num2 < 0)
        {
            num2 = - num2 ;
            negResult = true;
        }
         
        // if num1 is greater than equal to num2
        // subtract num2 from num1 and increase
        // quotient by one.
        int quotient = 0;
        while (num1 >= num2)
        {
            num1 = num1 - num2 ;
            quotient++ ;
        }
         
        // checking if neg equals to 1 then making
        // quotient negative
        if (negResult)
                quotient = - quotient ;
        return quotient ;
    }
     
    // Driver program
    public static void main (String[] args)
    {
        int num1 = 13, num2 = 2 ;
        System.out.println( division(num1, num2));
             
    }
}
 
// This code is contributed by vt_m.

Python3

# Python3 program to divide a number
# by other without using / operator
 
# Function to find division without 
# using '/' operator
def division(num1, num2):
     
    if (num1 == 0): return 0
    if (num2 == 0): return INT_MAX
     
    negResult = 0
     
    # Handling negative numbers
    if (num1 < 0):
        num1 = - num1
         
        if (num2 < 0):
            num2 = - num2
        else:
            negResult = true
                 
    elif (num2 < 0):
        num2 = - num2
        negResult = true
     
    # if num1 is greater than equal to num2
    # subtract num2 from num1 and increase
    # quotient by one.
    quotient = 0
 
    while (num1 >= num2):
        num1 = num1 - num2
        quotient += 1
     
    # checking if neg equals to 1 then
    # making quotient negative
    if (negResult):
            quotient = - quotient
    return quotient
 
# Driver program
num1 = 13; num2 = 2
print(division(num1, num2))
 
# This code is contributed by Ajit.

C#

// C# program to divide a number by other
// without using / operator
using System;
 
class GFG
{
    // Function to find division without
    // using '/' operator
    static int division(int num1, int num2)
    {
        if (num1 == 0)
            return 0;
        if (num2 == 0)
            return int.MaxValue;
         
        bool negResult = false;
         
        // Handling negative numbers
        if (num1 < 0)
        {
            num1 = -num1 ;
            if (num2 < 0)
                num2 = - num2 ;
            else
                negResult = true;
        }
        else if (num2 < 0)
        {
            num2 = - num2 ;
            negResult = true;
        }
         
        // if num1 is greater than equal to num2
        // subtract num2 from num1 and increase
        // quotient by one.
        int quotient = 0;
        while (num1 >= num2)
        {
            num1 = num1 - num2 ;
            quotient++ ;
        }
         
        // checking if neg equals to 1 then making
        // quotient negative
        if (negResult)
                quotient = - quotient ;
        return quotient ;
    }
     
    // Driver Code
    public static void Main ()
    {
        int num1 = 13, num2 = 2 ;
        Console.Write( division(num1, num2));
             
    }
}
 
// This code is contributed by nitin mittal.

PHP

<?php
// CPP program to divide a number by other
// without using / operator
 
// Function to find division without using
// '/' operator
 
function division($num1, $num2)
{
    if ($num1 == 0)
        return 0;
         
    if ($num2 == 0)
        return INT_MAX;
     
    $negResult = false;
     
    // Handling negative numbers
    if ($num1 < 0)
    {
        $num1 = -$num1 ;
         
        if ($num2 < 0)
            $num2 = - $num2 ;
        else
            $negResult = true;
    }
    else if ($num2 < 0)
    {
        $num2 = - $num2 ;
        $negResult = true;
    }
     
    // if num1 is greater than equal to num2
    // subtract num2 from num1 and increase
    // quotient by one.
    $quotient = 0;
    while ($num1 >= $num2)
    {
        $num1 = $num1 - $num2 ;
        $quotient++ ;
    }
     
    // checking if neg equals to 1 then making
    // quotient negative
    if ($negResult)
            $quotient = - $quotient ;
    return $quotient ;
}
 
// Driver program
$num1 = 13; $num2 = 2 ;
echo division($num1, $num2) ;
     
// This code is contributed by ajit
?>

Javascript

<script>
// Javascript program to divide a number by other
// without using / operator
 
    // Function to find division without using
    // '/' operator
    function division( num1, num2)
    {
        if (num1 == 0)
            return 0;
        if (num2 == 0)
            return Number.MAX_VALUE;;
        let negResult = false;
 
        // Handling negative numbers
        if (num1 < 0)
        {
            num1 = -num1;
            if (num2 < 0)
                num2 = -num2;
            else
                negResult = true;
        }
        else if (num2 < 0)
        {
            num2 = -num2;
            negResult = true;
        }
 
        // if num1 is greater than equal to num2
        // subtract num2 from num1 and increase
        // quotient by one.
        let quotient = 0;
        while (num1 >= num2)
        {
            num1 = num1 - num2;
            quotient++;
        }
 
        // checking if neg equals to 1 then making
        // quotient negative
        if (negResult)
            quotient = -quotient;
        return quotient;
    }
 
    // Driver program    
    let num1 = 13, num2 = 2;
    document.write(division(num1, num2));
 
// This code is contributed by gauravrajput1
</script>

Producción : 
 

6

Publicación traducida automáticamente

Artículo escrito por nikunj_agarwal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *