Learn How To Divide Two Numbers without using Division (/) Operator in C Programming Language. We generally use division operator (/) to divide a number. Here, we shall use the (-) operator to find the product of the numbers. Alternatively, two numbers can be divided using Bitwise Operators.
The first code to find the division of two numbers without using division operator (/) makes use of the subtraction (–) operator. If you want to divide two integers without arithmetic operators, then you will have to use the bitwise operators which is demonstrated in the second program. It uses bitwise shift operator.
C Program To Divide Two Numbers without using Division Operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<stdio.h> int main() { int num1, num2, a, b, count = 0 ; printf("\nEnter First Number:\t"); scanf("%d", &a); printf("\nEnter Second Number:\t"); scanf("%d", &b); num1 = a; num2 = b; while(num1 >= num2) { num1 = num1 - num2; count++; } printf("\nDivision of %d and %d:\nQuotient:\t%d\nRemainder:\t%d\n", a, b, count, num1); return 0; } |
C Program To Divide Two Numbers using Bitwise Operators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include<stdio.h> int main() { int num1, num2, sign = 1, quotient = 1, temp; printf("\nEnter First Number:\t"); scanf("%num2", &num1); printf("\nEnter Second Number:\t"); scanf("%num2", &num2); if(num1 < 0) { num1 = -num1; sign = -1; } else if(num2 < 0) { num2 = -num2; sign = sign * (-1); } temp = num2; if((num1 != 0 && num2 != 0) && (num2 < num1)) { while(((temp<<1) - num1) < 0) { temp = temp<<1; quotient = quotient<<1; } while((temp + num2) <= num1) { temp = temp + num2; quotient = quotient + 1; } } else if(num1 == 0) { quotient = 0; } if(num2) { printf("\nDivision of %num2 and %num2:\nQuotient:\temp%num2\n", num1, num2, quotient * sign); } else { printf("\nDividing A Number By Zero Not Possible\n"); } return 0; } |
Output

If you have any compilation error or doubts in this C program to Divide Two Integers using Bitwise Operators, let us know about it in the comment section below.
thanks
Explain the logic, how bitwise operation working.
First code wouldn’t work for edge cases like:
a) -15 / 5
b) 15 / -5