Divide Two Numbers Without Using Division and Modulus Operator

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

  
Showing Answers 1 - 13 of 13 Answers

shraddha Dohare

  • Aug 3rd, 2015
 

Code
  1. using System;

  2. class Division

  3. {

  4. static void Main()

  5. {

  6.  int c=0;

  7.  Console.WriteLine("Enter Dividend:");

  8.  double N= double.Parse(Console.ReadLine());

  9.  Console.Write("Enter Divisor:");

  10.  double D =double.Parse(Console.ReadLine());

  11.  if(N== 0)

  12. {

  13.   Console.WriteLine("Quotent =0");

  14. return;

  15. }

  16.  

  17. else if(D ==0)

  18. {

  19.   Console.WriteLine("Divided By Zero");

  20. return;

  21. }

  22.  else

  23.  {

  24.   while(N>=D)

  25.         {

  26.          c+=1;

  27.          N = N-D;

  28.    

  29.         }

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

  31.  

  32. }

  33.  

  34.  

  35.  

  36. }

  37. }

Ken

  • Aug 12th, 2015
 

Code
  1. using System;

  2.  

  3. public static class program

  4. {

  5.  

  6.     static void Main(string[] args)

  7.     {

  8.         Console.WriteLine(Divide(10, -3));

  9.         Console.WriteLine(Divide(-10, -3));

  10.         Console.WriteLine(Divide(1, 3));

  11.         Console.WriteLine(Divide(1, 2));

  12.         Console.WriteLine(Divide(2, 1));

  13.         Console.WriteLine(Divide(2, 200));

  14.         Console.WriteLine(Divide(555.55M, 5M));

  15.         Console.WriteLine(Divide(.1M, 3M));

  16.     }

  17.  

  18.  

  19.     public static string Divide(decimal numerator, decimal denominator)

  20.     {

  21.         //check the param

  22.         if (numerator == 0) { return "0"; }

  23.         if (denominator == 0) { throw new DivideByZeroException(); }

  24.  

  25.         //format the param

  26.         string output = "";

  27.         if ((numerator < 0 && denominator > 0) || (numerator > 0 && denominator < 0))

  28.         {

  29.             output += "-";

  30.         }

  31.         numerator = Math.Abs(numerator);

  32.         denominator = Math.Abs(denominator);

  33.  

  34.         //start dividing

  35.         int decimalPlaceCounter = 0;

  36.         int subtractCounter = 0;

  37.         bool containsDecimalPlace = false;

  38.         while (numerator > 0 && decimalPlaceCounter < 10)

  39.         {

  40.             if (numerator < denominator)

  41.             {

  42.                 //append the number of times the denominator goes into the numerator

  43.                 output += subtractCounter.ToString();

  44.  

  45.                 //add a decimal places

  46.                 if (!containsDecimalPlace)

  47.                 {

  48.                     containsDecimalPlace = true;

  49.                     output += ".";

  50.                 }

  51.  

  52.                 //handle the remainer by multiplying it by 10.

  53.                 numerator = numerator * 10;

  54.  

  55.                 //reset counters

  56.                 subtractCounter = 0;

  57.  

  58.                 //increment decimal counter, since we dont want an infinite loop

  59.                 decimalPlaceCounter++;

  60.             }

  61.             else

  62.             {

  63.                 //denominator goes into the numerator at least once. increment the counter

  64.                 numerator = numerator - denominator;

  65.                 subtractCounter++;

  66.             }

  67.         }

  68.         output += subtractCounter.ToString();

  69.  

  70.         return output;

  71.     }

  72.  

  73.  

  74. }

  75.  

  Was this answer useful?  Yes

Ankita

  • Aug 27th, 2015
 

Code
  1. using System;

  2.  

  3.  

  4. namespace divide_without_div_op

  5. {

  6.     class Program

  7.     {

  8.         static void Main(string[] args)

  9.         {

  10.             Console.WriteLine("Enter the number:");

  11.             int a = Convert.ToInt32(Console.ReadLine());

  12.             Console.WriteLine("Enter the divisor:");

  13.             int d = Convert.ToInt32(Console.ReadLine());

  14.             if (d == 0) throw new DivideByZeroException();

  15.             int q = 0,r=0;

  16.             while (a - d >= 0)

  17.             {

  18.                 q++;

  19.                 a = a - d;

  20.             }

  21.             r = a;

  22.             Console.WriteLine("quotient={0},remainder={1}",q,r);

  23.         }

  24.     }

  25. }

  26.  

  Was this answer useful?  Yes

James Tyler

  • Aug 27th, 2015
 

Code
  1. using System;

  2.  

  3. namespace DivisionWithoutDivision

  4. {

  5.     class Program

  6.     {

  7.         static void Main(string[] args)

  8.         {

  9.             // Gather user input values

  10.             Console.WriteLine("Enter an integer to divide:");

  11.             int y = Convert.ToInt32(Console.ReadLine());

  12.             Console.WriteLine("Enter an integer to divide the first number by:");

  13.             int x = Convert.ToInt32(Console.ReadLine());

  14.             // Call the CheckZero method to avoid division problems.

  15.             if (CheckZero(x))

  16.             {

  17.                 // Call the Division method on the input values and display the result.

  18.                 int z = Division(x, y);

  19.                 Console.WriteLine(x + " divides into " + y + " -- " + z + " times.");

  20.                 Console.ReadLine();

  21.             }

  22.             else

  23.             {

  24.                 Console.WriteLine("Invalid entry.  Cannot divide a number by zero.");

  25.             }

  26.         }

  27.         static bool CheckZero(int x)

  28.         {

  29.             if (x == 0) return false;

  30.             else return true;

  31.         }

  32.         static int Division(int x, int y)

  33.         {

  34.             // Set up a counter that will function as the required answer

  35.             int count = -1;

  36.             // Run a loop that will continually sum a number with itself,

  37.             // ending the loop when the current sum is greater than or equal to the other number.

  38.             int sum = 0;

  39.             while (sum < y)

  40.             {

  41.                 sum = sum + x;

  42.                 count++;

  43.             }

  44.             // Return the counter in its current state.

  45.             return count;

  46.         }

  47.     }

  48. }

  Was this answer useful?  Yes

Swapnil Mahadik

  • Jan 26th, 2016
 

Code
  1. import java.util.*;

  2. class WithoutO

  3. {

  4.  public static void main(String args[])

  5.  {

  6.  

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

  8.  

  9.  Scanner s=new Scanner(System.in);

  10.  System.out.println("Enter number:");

  11.  num=s.nextInt();

  12.  System.out.println("Enter divisor:");

  13.  div=s.nextInt();

  14.  

  15.  for(int i=1;i<num;i++)

  16.  {

  17.  res=div*i;

  18.  

  19.   if(res>num)

  20.   {

  21.   res=res-div;

  22.   qu=i-1;

  23.   rem=num-res;

  24.   break;

  25.   }

  26.  }

  27.  System.out.println("Quotient:" +qu);

  28.  System.out.println("Remainder:" +rem);

  29.  

  30.  

  31.  }

  32. }

  33.  

  34.  

  35. /*

  36. D:Javap>java WithoutO

  37. Enter number:

  38. 92

  39. Enter divisor:

  40. 4

  41. Quotient:23

  42. Remainder:0

  43.  

  44. */

  Was this answer useful?  Yes

Cristina Cimpoi

  • Feb 26th, 2016
 

Converting it to binary and using a mask

  Was this answer useful?  Yes

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;
Console.WriteLine("Please Enter the dividend");
bool IsDividendInteger = int.TryParse(Console.ReadLine() , out Dividend);
Console.WriteLine("Please Enter the divisor");
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 );
Console.ReadLine();
}
}
}

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

Thirunavukkarasu G

  • Sep 15th, 2016
 

Code
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using System.Threading.Tasks;

  6.  

  7. namespace Quotient_and_Remainder

  8. {

  9.     class Program

  10.     {

  11.         static void Main(string[] args)

  12.         {

  13.             Console.WriteLine("Program to Find Quotient and Remainer without using division(/) operator");

  14.             int d = Convert.ToInt32(Console.ReadLine());

  15.             int div = Convert.ToInt32(Console.ReadLine());

  16.             int i, j, diff=0,Quotient;

  17.             if (d > div)

  18.             {

  19.                 diff = d - div;

  20.             }

  21.             else if (div > d)

  22.             {

  23.                 diff = div - d;

  24.             }

  25.             for (i = 1; i < diff; i++)

  26.             {

  27.                 Quotient = div * i;

  28.                 for (j = 0; j < div; j++)

  29.                 {

  30.                                      

  31.                     if ((Quotient + j) == d)

  32.                     {

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

  34.                         break;

  35.                     }

  36.                     else if ((Quotient == d) && (j==0))

  37.                     {

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

  39.                         break;

  40.                     }

  41.                 }

  42.             }

  43.             Console.ReadLine();

  44.  

  45.  

  46.         }

  47.     }

  48. }

  49.  

  Was this answer useful?  Yes

Samwise Galenorn

  • Oct 27th, 2016
 

Use the laws of logs

Code
  1. private double divideWO(double dividend, double divisor)

  2. {

  3.     double sign = (dividend * divisor < 0) ? -1.0 : 1.0;

  4.     double quotient;

  5.  

  6.     if (dividend == 0)

  7.     {

  8.         quotient = 0;

  9.     } else {

  10.         quotient = sign *

  11.             Math.Exp(Math.Log(Math.Abs(dividend)) - Math.Log(Math.Abs(divisor)));

  12.     }

  13.  

  14.     return quotient;

  15. }

  Was this answer useful?  Yes

Ankita Kulkarni

  • Feb 12th, 2017
 

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

  Was this answer useful?  Yes

Pushpa Sundar

  • Mar 16th, 2018
 

Code
  1. import java.util.Scanner;

  2.  

  3. public class Divition {

  4.  

  5.         public static void main(String[] args) {

  6.                 Scanner scn=new Scanner(System.in);

  7.                 System.out.println("Enter the Divident: ");

  8.                 int divident=scn.nextInt();

  9.                 System.out.println(divident);

  10.                 System.out.println("Enter the Divisor: ");

  11.                 int divisor=scn.nextInt();

  12.                 System.out.println(divisor);

  13.                 int quotient=0;

  14.                 while(divident>=divisor)

  15.                 {

  16.                         divident=divident-divisor;

  17.                         quotient++;

  18.                 }

  19.                 System.out.println("The Quotient is : "+quotient);

  20.                 System.out.println("The Reminder is : "+divident);

  21.  

  22.         }

  23.  

  24. }

  25.  

  Was this answer useful?  Yes

Sujit

  • Feb 18th, 2019
 

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

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions