Home Misc
Category:

Misc

View Miscellaneous problems

  • Question: How can you divide a number by 3 without using *, /, +, – % operator? Input: 48 Output: 16 The method below implements the method using bit-wise operators. 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 ie, 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 …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Given a number ‘n’, how to check if n is a Fibonacci number. A simple way is to generate Fibonacci numbers until the generated number is greater than or equal to ‘n’. Following is an interesting property about Fibonacci numbers that can also be used to check if a given number is Fibonacci or not. A number is Fibonacci if and only if one or both of 5n2+4 or 5×2-4 is a perfect square (Source: Wikipedia). Following is a simple program based on this concept. Output:- 1 is a Fibonacci …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More