For a given positive number we need to divide a number by 3 without using any of these *, /, +, – % operators

Examples:

Input : 48 Output : 16 Input : 16 Output : 5

**Algorithm**

- Take a number num, sum = 0
- while(num>3), shift the number left by 2 bits and sum = add(num >> 2, sum). Create a function add(x, y) for adding two number by bitwise opreator
- Take x, y
- run a do while loop till a is not zero
- a = x & y, b = x ^ y;, y = b;
- after terminating loop return value of b which gives sum of x and y

- Take bitwise AND of num with 3

for new num = add( num >> 2, num & 3) - After terminating while loop print the value of sum which give final result .

For example if your number is 10 then convert to binary 10 => 00001010

If 10 > 3 then shift the binary number 2 bits, Now num will be 00000010 i.e, 2

Now sum will be 2

num = (shift the number by 2 bits) + (number BITWISE AND 3)

num = 2+2

Now the number will be 4 then, 4 > 3 => true so loop will be repeated

4 => 00000100 then shift the binary number 2 bits

Now sum = 2 + 1 ie, sum = 3 (this value is returned)

num = (shift the number(00000100) by 2 bits) + (number BITWISE AND 3)

num = 1 + 0

ie remainder = 1

`// C++ program for divided a number by ` `// 3 without using operator ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A function to add 2 numbers without using + ` `int` `add(` `int` `x, ` `int` `y) ` `{ ` ` ` `int` `a, b; ` ` ` `do` `{ ` ` ` `a = x & y; ` ` ` `b = x ^ y; ` ` ` `x = a << 1; ` ` ` `y = b; ` ` ` `} ` `while` `(a); ` ` ` ` ` `return` `b; ` `} ` ` ` `// Function to divide by 3 ` `int` `divideby3(` `int` `num) ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `while` `(num > 3) { ` ` ` `sum = add(num >> 2, sum); ` ` ` `num = add(num >> 2, num & 3); ` ` ` `} ` ` ` ` ` `if` `(num == 3) ` ` ` `sum = add(sum, 1); ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver program ` `int` `main(` `void` `) ` `{ ` ` ` `int` `num = 48; ` ` ` `cout << ` `"The number divided by 3 is "` `; ` ` ` `cout << divideby3(num); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

The number divided by 3 is 16

This article is contributed by **DANISH_RAZA**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if a number is multiple of 5 without using / and % operators
- Divide two integers without using multiplication, division and mod operator | Set2
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Write you own Power without using multiplication(*) and division(/) operators
- Add two numbers without using arithmetic operators
- How to sum two integers without using arithmetic operators in C/C++?
- Decimal to binary conversion without using arithmetic operators
- Largest of two distinct numbers without using any conditional statements or operators
- Find Quotient and Remainder of two integer without using division operators
- Subtract 1 without arithmetic operators
- Count number of ways to divide a number in 4 parts
- Find count of digits in a number that divide the number
- Sum and Product of digits in a number that divide the number
- Find the number of ways to divide number into four parts such that a = c and b = d
- Check if a number is divisible by 8 using bitwise operators
- Count ways to divide circle using N non-intersecting chord | Set-2
- Product of all sorted subsets of size K using elements whose index divide K completely
- Count digits in given number N which divide N
- Divide number into two parts divisible by given numbers
- Minimum positive integer to divide a number such that the result is an odd