Divide Two Numbers Without Using Division and Modulus Operator

Write C# code to divide two numbers without using the division and modulus operator

• Aug 3rd, 2015

```Codeusing System;
class Division
{
static void Main()
{
int c=0;
Console.WriteLine("Enter Dividend:");
Console.Write("Enter Divisor:");
if(N== 0)
{
Console.WriteLine("Quotent =0");
return;
}

else if(D ==0)
{
Console.WriteLine("Divided By Zero");
return;
}
else
{
while(N>=D)
{
c+=1;
N = N-D;

}
Console.WriteLine("Quetiont ={0}",c);

}

}
}```

Ken

• Aug 12th, 2015

```Codeusing System;

public static class program
{

static void Main(string[] args)
{
Console.WriteLine(Divide(10, -3));
Console.WriteLine(Divide(-10, -3));
Console.WriteLine(Divide(1, 3));
Console.WriteLine(Divide(1, 2));
Console.WriteLine(Divide(2, 1));
Console.WriteLine(Divide(2, 200));
Console.WriteLine(Divide(555.55M, 5M));
Console.WriteLine(Divide(.1M, 3M));
}

public static string Divide(decimal numerator, decimal denominator)
{
//check the param
if (numerator == 0) { return "0"; }
if (denominator == 0) { throw new DivideByZeroException(); }

//format the param
string output = "";
if ((numerator < 0 && denominator > 0) || (numerator > 0 && denominator < 0))
{
output += "-";
}
numerator = Math.Abs(numerator);
denominator = Math.Abs(denominator);

//start dividing
int decimalPlaceCounter = 0;
int subtractCounter = 0;
bool containsDecimalPlace = false;
while (numerator > 0 && decimalPlaceCounter < 10)
{
if (numerator < denominator)
{
//append the number of times the denominator goes into the numerator
output += subtractCounter.ToString();

if (!containsDecimalPlace)
{
containsDecimalPlace = true;
output += ".";
}

//handle the remainer by multiplying it by 10.
numerator = numerator * 10;

//reset counters
subtractCounter = 0;

//increment decimal counter, since we dont want an infinite loop
decimalPlaceCounter++;
}
else
{
//denominator goes into the numerator at least once. increment the counter
numerator = numerator - denominator;
subtractCounter++;
}
}
output += subtractCounter.ToString();

return output;
}

}
```

Ankita

• Aug 27th, 2015

```Codeusing System;

namespace divide_without_div_op
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number:");
Console.WriteLine("Enter the divisor:");
if (d == 0) throw new DivideByZeroException();
int q = 0,r=0;
while (a - d >= 0)
{
q++;
a = a - d;
}
r = a;
Console.WriteLine("quotient={0},remainder={1}",q,r);
}
}
}
```

James Tyler

• Aug 27th, 2015

```Codeusing System;

namespace DivisionWithoutDivision
{
class Program
{
static void Main(string[] args)
{
// Gather user input values
Console.WriteLine("Enter an integer to divide:");
Console.WriteLine("Enter an integer to divide the first number by:");
// Call the CheckZero method to avoid division problems.
if (CheckZero(x))
{
// Call the Division method on the input values and display the result.
int z = Division(x, y);
Console.WriteLine(x + " divides into " + y + " -- " + z + " times.");
}
else
{
Console.WriteLine("Invalid entry.  Cannot divide a number by zero.");
}
}
static bool CheckZero(int x)
{
if (x == 0) return false;
else return true;
}
static int Division(int x, int y)
{
// Set up a counter that will function as the required answer
int count = -1;
// Run a loop that will continually sum a number with itself,
// ending the loop when the current sum is greater than or equal to the other number.
int sum = 0;
while (sum < y)
{
sum = sum + x;
count++;
}
// Return the counter in its current state.
return count;
}
}
}```

• Jan 26th, 2016

```Codeimport java.util.*;
class WithoutO
{
public static void main(String args[])
{

int res,rem=0,num,div,qu=0;

Scanner s=new Scanner(System.in);
System.out.println("Enter number:");
num=s.nextInt();
System.out.println("Enter divisor:");
div=s.nextInt();

for(int i=1;i<num;i++)
{
res=div*i;

if(res>num)
{
res=res-div;
qu=i-1;
rem=num-res;
break;
}
}
System.out.println("Quotient:" +qu);
System.out.println("Remainder:" +rem);

}
}

/*
D:Javap>java WithoutO
Enter number:
92
Enter divisor:
4
Quotient:23
Remainder:0

*/```

Cristina Cimpoi

• Feb 26th, 2016

Converting it to binary and using a mask

Varun Tripathi

• Mar 26th, 2016

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int OriginalDivisor =0;
int NewDivisor = 0;
int Dividend =0;
int Count = 0;
int remainder = 0;
bool IsDividendInteger = int.TryParse(Console.ReadLine() , out Dividend);
bool IsDivisorInteger = int.TryParse(Console.ReadLine(), out OriginalDivisor);
if (IsDividendInteger && IsDivisorInteger)
{
NewDivisor = OriginalDivisor;
if (OriginalDivisor != 0)
{
while (NewDivisor <= Dividend)
{
NewDivisor += OriginalDivisor;
Count++;
}
if (OriginalDivisor <= Dividend)
{
int x = (NewDivisor-OriginalDivisor) - Dividend;
if (x == 0)
{
remainder = 0;
}
else
{
remainder = NewDivisor - Dividend;
}
}
else
{
remainder = -(NewDivisor - OriginalDivisor - Dividend);
}
}
else
{
Console.WriteLine("when ever you divide a number by 0 , you get infinity as answer");
}
}
Console.WriteLine( "the Quotient is {0} and the remainder is {1}",Count ,remainder );
}
}
}

James Poulose

• Apr 18th, 2016

Division is nothing but repeated subtraction. E.g 20 divided by 5 - Keep subtracting 5 from 20 until you hit 0 and the count of iterations will be your answer.

Thirunavukkarasu G

• Sep 15th, 2016

```Codeusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Quotient_and_Remainder
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Program to Find Quotient and Remainer without using division(/) operator");
int i, j, diff=0,Quotient;
if (d > div)
{
diff = d - div;
}
else if (div > d)
{
diff = div - d;
}
for (i = 1; i < diff; i++)
{
Quotient = div * i;
for (j = 0; j < div; j++)
{

if ((Quotient + j) == d)
{
Console.WriteLine("The Given number {0} has Quotient {1} and Remainer={2}", d, i, j);
break;
}
else if ((Quotient == d) && (j==0))
{
Console.WriteLine("The Given number {0} has Quotient {1} and Remainer={2}", d, i, j);
break;
}
}
}

}
}
}
```

Samwise Galenorn

• Oct 27th, 2016

Use the laws of logs

```Codeprivate double divideWO(double dividend, double divisor)
{
double sign = (dividend * divisor < 0) ? -1.0 : 1.0;
double quotient;

if (dividend == 0)
{
quotient = 0;
} else {
quotient = sign *
Math.Exp(Math.Log(Math.Abs(dividend)) - Math.Log(Math.Abs(divisor)));
}

return quotient;
}```

Ankita Kulkarni

• Feb 12th, 2017

It is simple logic but how can we build code for this logic in C language?

Pushpa Sundar

• Mar 16th, 2018

```Codeimport java.util.Scanner;

public class Divition {

public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
System.out.println("Enter the Divident: ");
int divident=scn.nextInt();
System.out.println(divident);
System.out.println("Enter the Divisor: ");
int divisor=scn.nextInt();
System.out.println(divisor);
int quotient=0;
while(divident>=divisor)
{
divident=divident-divisor;
quotient++;
}
System.out.println("The Quotient is : "+quotient);
System.out.println("The Reminder is : "+divident);

}

}
```

Sujit

• Feb 18th, 2019

Keep on subtracting divisor from dividend until dividend is 0 and return the count as quotient.