Write a program to Division of two number without using '/' operator
Example 1:
Input:a=15,b=-5
Output:-3
Approach 1: Iterative
Java
public class DivisionTwoNumber {public static void main(String[] args) {int dividend = -15;int divisor = 5;int quotient = divideTwo(dividend, divisor);System.out.println("Quotient is " + quotient);}// iterative approachprivate static int divideTwo(int dividend, int divisor) {int quotient = 0;// if divisor=0 thenif (divisor == 0)return 0;int sign = 1; // plus sign// check negative signif (dividend * divisor < 0) {sign = -1;}// convert value into positivedividend = Math.abs(dividend);divisor = Math.abs(divisor);// iterate untill dividend>=divisorwhile (dividend >= divisor) {dividend = dividend - divisor;quotient++;}return quotient * sign;}}
C++
#include <bits/stdc++.h>using namespace std;// function to divide the numberint divideTwo(int dividend, int divisor){int quotient = 0;// if divisor=0 thenif (divisor == 0)return 0;int sign = 1; // plus sign// check negative signif (dividend * divisor < 0){sign = -1;}// convert value into positivedividend = abs(dividend);divisor=abs(divisor);// iterate untill dividend>=divisorwhile (dividend >= divisor) {dividend = dividend - divisor;quotient++;}return quotient * sign;}int main(){int dividend = -15;int divisor = 5;int quotient = divideTwo(dividend, divisor);cout<<"Quotient is "<<quotient;}
Approach 2: Recurring
Java
public class DivisionTwoNumber {public static void main(String[] args) {int dividend = -15;int divisor = 5;System.out.println("Quotient is " +divideTwoRec(dividend, divisor));}// using recurringprivate static int divideTwoRec(int a, int b) {// if b=0 thenif (b == 0)return 0;int sign = 1;// check for negativeif (a * b < 0)sign = -1;// convert into positivea = Math.abs(a);b = Math.abs(b);// base conditionif (a < b) {return 0;}// call recurring methodreturn sign * (1 + divideTwoRec(a - b, b));}}
C++
#include <bits/stdc++.h>using namespace std;// using recurringint divideTwoRec(int a, int b){// if b=0 thenif (b == 0)return 0;int sign = 1;// check for negativeif (a * b < 0)sign = -1;// convert into positivea = abs(a);b = abs(b);// base conditionif (a < b) {return 0;}// call recurring methodreturn sign * (1 + divideTwoRec(a - b, b));}int main(){int dividend = -15;int divisor = 5;int quotient = divideTwoRec(dividend, divisor);cout<<"Quotient is "<<quotient;}
Approach 3: Bit Manipulation
Java
public class DivisionTwoNumber {public static void main(String[] args) {int dividend = -15;int divisor = 5;int sign = 1;if (dividend * divisor < 0) {sign = -1;}dividend = Math.abs(dividend);divisor = Math.abs(divisor);int quot = sign * divideTwoUsingBit(dividend, divisor);System.out.println("Quotient is " + quot);}// Using bit manipulationprivate static int divideTwoUsingBit(int dividend,int divisor) {// hold for perform operationint denom = divisor;int current = 1;int answer = 0;// base caseif (denom > dividend)return 0;// base caseif (denom == dividend)return 1;// Left shift untill denom <=dividendwhile (denom <= dividend) {// one left shiftdenom <<= 1;current <<= 1;}// one right shiftdenom >>= 1;current >>= 1;// iterate untill current!=0while (current != 0) {if (dividend >= denom) {dividend = dividend - denom;// XORanswer = answer | current;}// right shiftcurrent >>= 1;denom >>= 1;}return answer;}}
C++
#include <bits/stdc++.h>using namespace std;// Using bit manipulationint divideTwoUsingBit(int dividend,int divisor){// hold for perform operationint denom = divisor;int current = 1;int answer = 0;// base caseif (denom > dividend)return 0;// base caseif (denom == dividend)return 1;// Left shift untill denom <=dividendwhile (denom <= dividend) {// one left shiftdenom <<= 1;current <<= 1;}// one right shiftdenom >>= 1;current >>= 1;// iterate untill current!=0while (current != 0) {if (dividend >= denom) {dividend = dividend - denom;// XORanswer = answer | current;}// right shiftcurrent >>= 1;denom >>= 1;}return answer;}int main(){int dividend = -15;int divisor = 5;int sign = 1;if (dividend * divisor < 0) {sign = -1;}dividend =abs(dividend);divisor =abs(divisor);int quot = sign * divideTwoUsingBit(dividend, divisor);cout<<"Quotient is "<< quot;return 0;}
No comments:
Post a Comment