How can I divide a number with an unknown number without using these operators('*', '/', '%')
. Denominator is given during runtime.

- 589
- 2
- 6
- 20
-
float or int? signed or unsigned? when did you stuck? why? – huseyin tugrul buyukisik Jan 12 '14 at 12:38
-
Show us some code and tell us where you're stuck. This is no "help with your homework assignment" site. – Guntram Blohm Jan 12 '14 at 12:41
-
If you are allowed to use - in loop control, then you should take a look at: http://stackoverflow.com/questions/12697523/performing-bit-division-without-arithmetic-operators/12699549#12699549 – Roger Lindsjö Jan 12 '14 at 12:46
-
1@vinayawsm Your edit substantially changed the question. The fact that answers exist that use addition/subtraction doesn't mean that you should change the question to allow their use. – simonc Jan 12 '14 at 12:58
8 Answers
void main(){
int a,b,i=0;
clrscr();
printf("Enter the dividend and divisor");
scanf("%d%d",&a,&b);
while(a>=b){
a=a-b;
i++;
}
printf("qoutient is :%d \n remainder : %d",i,a);
getch();
}

- 6,434
- 1
- 35
- 37
-
5While code can speak for itself, code-only answers don't provide sufficient information to be considered high quality. Please add details about why your code solves the question asked. – Jul 08 '14 at 06:44
-
1Can you do this without using a loop? I was asked the same thing in an interview. When i did it this way, the interviewer told me to do it without loops. – AlphaGoku Oct 28 '16 at 10:01
You can use this function
int divide(int nu, int de) {
int temp = 1;
int quotient = 0;
while (de <= nu) {
de <<= 1;
temp <<= 1;
}
//printf("%d %d\n",de,temp,nu);
while (temp > 1) {
de >>= 1;
temp >>= 1;
if (nu >= de) {
nu -= de;
//printf("%d %d\n",quotient,temp);
quotient += temp;
}
}
return quotient;
}
You can pass a numerator and a denominator to this function and get the required quotient.

- 15,750
- 31
- 68
- 83

- 845
- 9
- 28
-
3
-
@V-X Sorry but its fine using + or - operators. I actually was trying to code for without using '/' and '%' operators. – nomorequestions Jan 12 '14 at 13:04
-
You also need to check whether the arguments are negative / positive. – alampada Jul 11 '15 at 16:14
Simplest way:
int divideIntegers(int num, int den){
int sign = (num*den < 0)? -1 : 1;
num = abs(num);
den = abs(den);
int quo = 0;
while( (num -= den) >= 0 )
quo++;
return sign*quo;
}

- 6,317
- 7
- 43
- 51

- 31
- 3
For integer division, you can use div
, ldiv
or lldiv
functions from the standard library:
#include <stdlib.h>
div_t div(int numer, int denom);
ldiv_t ldiv(long int numer, long int denom);
lldiv_t lldiv(long long int numer, long long int denom);

- 142,963
- 15
- 272
- 331
-
Downvoter, care to explain your downvote? Does calling these functions use any of the forbidden operators? – ouah Jan 12 '14 at 12:50
-
Not the downvoter, but the question shows no effort. – LittleBobbyTables - Au Revoir Jan 12 '14 at 12:53
-
@LittleBobbyTables yes, I was asking on the downvote to my answer, not the downvotes to OP question. – ouah Jan 12 '14 at 12:55
-
1@oauh- I know, and I was pointing out that there's a tendency to downvote answers to low-quality/no-effort questions, which is why you may have been dinged. – LittleBobbyTables - Au Revoir Jan 12 '14 at 12:56
Below method is the implementation of binary divide considering both numbers are positive. If subtraction is a concern we can implement that as well using binary operators.
======
-(int)binaryDivide:(int)numerator with:(int)denominator
{
if (numerator ==0 || denominator ==1) {
return numerator;
}
if (denominator ==0) {
#ifdef DEBUG
NSAssert(denominator==0, @"denominator should be greater then 0");
#endif
return INFINITY;
}
// if (numerator <0) {
// numerator = abs(numerator);
// }
int maxBitDenom = [self getMaxBit:denominator];
int maxBitNumerator = [self getMaxBit:numerator];
int msbNumber = [self getMSB:maxBitDenom ofNumber:numerator];
int qoutient = 0;
int subResult = 0;
int remainingBits = maxBitNumerator-maxBitDenom;
if(msbNumber>=denominator){
qoutient |=1;
subResult = msbNumber- denominator;
}
else{
subResult = msbNumber;
}
while(remainingBits>0){
int msbBit = (numerator & (1<<(remainingBits-1)))>0?1:0;
subResult = (subResult <<1) |msbBit;
if(subResult >= denominator){
subResult = subResult-denominator;
qoutient= (qoutient<<1)|1;
}
else{
qoutient = qoutient<<1;
}
remainingBits--;
}
return qoutient;
}
-(int)getMaxBit:(int)inputNumber
{
int maxBit =0;
BOOL isMaxBitSet = NO;
for(int i=0;i<sizeof(inputNumber)*8;i++){
if( inputNumber & (1<<i) ){
maxBit = i;
isMaxBitSet=YES;
}
}
if (isMaxBitSet) {
maxBit+=1;
}
return maxBit;
}
-(int)getMSB:(int)bits ofNumber:(int)number
{
int numbeMaxBit = [self getMaxBit:number];
return number>>(numbeMaxBit -bits);
}

- 4,324
- 2
- 24
- 14
your question is very vague, but I can give you a particular case of dividing a number with 2. it can be performed by a bit shift operation that shifts the number one place to the right. This is a form of strength reduction optimization.
For example, 1101000 in binary (the decimal number 104), shifted one place to the right, is 0110100 (the decimal number 52): the lowest order bit, a 1, is removed. Similarly, division by any power of two (2 pow k) may be performed by right-shifting k positions. Because bit shifts are often much faster operations than division.
code to test that :
#include <stdio.h>
main()
{
int i = 104;
int k = 3; //
int j = i >> k ; //==> i / 2 pow k
printf("j = %d \n",j);
}

- 3,306
- 4
- 30
- 53
This is a very simple approach to the problem; using loops and basic [+-]operators.
If you need an answer in decimals, you could use a times_ten and divide_by_ten function. In that case, you should take a look at the atoi() function; times_ten would extract the integer in a char-array, adding a '0' in the end before converting it back to integer. divide_by_ten would store the last character of an integer, substract this character with a '.' and adding the stored last digit back to the array before converting it back to integer. Atoi() will round the integer based on the decimal which we manipulated in the char-array.
Heres a version only supporting integer results, with an extra function (leftover_division()) replacing the '%'-operator. [b]Passing pointers to integers instead of regular integers to the divide_rounded() function and adjusting the value of 'a' in divide_rounded() should make the leftover function redundant, saving a lot of calculation time if you'd need to know the lefover.[/b]
#include <stdio.h>
#include <stdlib.h>
int divide_rounded(int a, int b){
int outcome_rounded = 0;
while(a > b){
a = a - b;
outcome_rounded ++;
}
return outcome_rounded;
}
int leftover_division(int a, int b){
while (a >= b){
a = a - b;
}
return a;//this will return remainder
}
main(){
int number = 20;
int divisor = 3;
int outcome;
int leftover;
outcome = divide_rounded(number, divisor);
leftover = leftover_division(number, divisor);
printf("[%d] divided by [%d] = [%d] + [%d]\n", number, divisor, outcome, leftover);
}

- 142
- 1
- 1
- 12

- 1
Psuedo code in python for divide by constant
n_bits = Number of bits of input over which division is expected to be accurate
den = divisor
prec = int(math.ceil(math.log(den,2)))
shift = n_bits + prec
mult = int(math.ceil((1<<shift)/float(den)))
answer = (x*mult) >> shift
err = sum([round(x/den) - ((x*mult) >> shift) for x in range(1<<n_bits)])
Multiplication can be implemented with shifts and adds

- 1
- 1