Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

Dividing number without operator can be implemented in two approaches - loop and bit operation


Example 1) divide based in loop

Code Block

#include <iostream>

int divideWithoutOperator(int dividend, int divisor) {
    // Handle division by zero or cases where the dividend is smaller than the divisor
    if (divisor == 0 || dividend < divisor)
        return 0;

    int quotient = 0;
    while (dividend >= divisor) {
        dividend -= divisor;
        quotient++;
    }

    return quotient;
}

int main() {
    int dividend, divisor;
    std::cout << "Enter dividend: ";
    std::cin >> dividend;
    std::cout << "Enter divisor: ";
    std::cin >> divisor;

    int result = divideWithoutOperator(dividend, divisor);
    std::cout << "Result: " << result << std::endl;

    return 0;
}

...