Given two numbers, divide one from other without using ‘/’ operator.
Examples :
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
In order to perform division operation without using ‘/’ operator we followed the approach, in which we count the number of successful or complete number of subtraction of num2 from num1. Where num1 is the number to be divided and num2 is the number from which we have to divide num1.
C++
#include <bits/stdc++.h>
using namespace std;
int division( int num1, int num2)
{
if (num1 == 0)
return 0;
if (num2 == 0)
return INT_MAX;
bool negResult = false ;
if (num1 < 0)
{
num1 = -num1 ;
if (num2 < 0)
num2 = - num2 ;
else
negResult = true ;
}
else if (num2 < 0)
{
num2 = - num2 ;
negResult = true ;
}
int quotient = 0;
while (num1 >= num2)
{
num1 = num1 - num2 ;
quotient++ ;
}
if (negResult)
quotient = - quotient ;
return quotient ;
}
int main()
{
int num1 = 13, num2 = 2 ;
cout << division(num1, num2); ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int division( int num1, int num2)
{
if (num1 == 0 )
return 0 ;
if (num2 == 0 )
return Integer.MAX_VALUE;
boolean negResult = false ;
if (num1 < 0 )
{
num1 = -num1 ;
if (num2 < 0 )
num2 = - num2 ;
else
negResult = true ;
}
else if (num2 < 0 )
{
num2 = - num2 ;
negResult = true ;
}
int quotient = 0 ;
while (num1 >= num2)
{
num1 = num1 - num2 ;
quotient++ ;
}
if (negResult)
quotient = - quotient ;
return quotient ;
}
public static void main (String[] args)
{
int num1 = 13 , num2 = 2 ;
System.out.println( division(num1, num2));
}
}
|
Python3
def division(num1, num2):
if (num1 = = 0 ): return 0
if (num2 = = 0 ): return INT_MAX
negResult = 0
if (num1 < 0 ):
num1 = - num1
if (num2 < 0 ):
num2 = - num2
else :
negResult = true
elif (num2 < 0 ):
num2 = - num2
negResult = true
quotient = 0
while (num1 > = num2):
num1 = num1 - num2
quotient + = 1
if (negResult):
quotient = - quotient
return quotient
num1 = 13 ; num2 = 2
print (division(num1, num2))
|
C#
using System;
class GFG
{
static int division( int num1, int num2)
{
if (num1 == 0)
return 0;
if (num2 == 0)
return int .MaxValue;
bool negResult = false ;
if (num1 < 0)
{
num1 = -num1 ;
if (num2 < 0)
num2 = - num2 ;
else
negResult = true ;
}
else if (num2 < 0)
{
num2 = - num2 ;
negResult = true ;
}
int quotient = 0;
while (num1 >= num2)
{
num1 = num1 - num2 ;
quotient++ ;
}
if (negResult)
quotient = - quotient ;
return quotient ;
}
public static void Main ()
{
int num1 = 13, num2 = 2 ;
Console.Write( division(num1, num2));
}
}
|
PHP
<?php
function division( $num1 , $num2 )
{
if ( $num1 == 0)
return 0;
if ( $num2 == 0)
return INT_MAX;
$negResult = false;
if ( $num1 < 0)
{
$num1 = - $num1 ;
if ( $num2 < 0)
$num2 = - $num2 ;
else
$negResult = true;
}
else if ( $num2 < 0)
{
$num2 = - $num2 ;
$negResult = true;
}
$quotient = 0;
while ( $num1 >= $num2 )
{
$num1 = $num1 - $num2 ;
$quotient ++ ;
}
if ( $negResult )
$quotient = - $quotient ;
return $quotient ;
}
$num1 = 13; $num2 = 2 ;
echo division( $num1 , $num2 ) ;
?>
|
Javascript
<script>
function division( num1, num2)
{
if (num1 == 0)
return 0;
if (num2 == 0)
return Number.MAX_VALUE;;
let negResult = false ;
if (num1 < 0)
{
num1 = -num1;
if (num2 < 0)
num2 = -num2;
else
negResult = true ;
}
else if (num2 < 0)
{
num2 = -num2;
negResult = true ;
}
let quotient = 0;
while (num1 >= num2)
{
num1 = num1 - num2;
quotient++;
}
if (negResult)
quotient = -quotient;
return quotient;
}
let num1 = 13, num2 = 2;
document.write(division(num1, num2));
</script>
|
Time Complexity: O(num1/num2)
Auxiliary Space: O(1), As constant extra space is used.