Program to multiply two numbers without using * operator




















Improve this question. Lii Jesse James Jesse James 1, 3 3 gold badges 18 18 silver badges 34 34 bronze badges. Bitwise multiply — Oleksandr Pyrohov. If you like programming and you're willing to put in the work, then you're cut out for it. The same goes for technical interviewing. Only you can decide if you feel it's worth the effort — Simon. Or, if you created a new file, then appended X bytes to it Y times, then polled the OS for its file size. Or, the same, except with thread sleeps, then measured the result by subtracting starting and ending time stamps You might enjoy this: codegolf.

None of the 3 method listed here actually work for all ranges of a and b, even if the effects of overflow are ignored. Before embarking on reinventing the wheel, ensure you have a small flotilla of unit test cases ready. Show 2 more comments. Active Oldest Votes. I don't know whether that has to be a strictly "programming question". Improve this answer. If you used method 1, I would ask you if any rounding error was possible, just to watch you suffer.

That's why this trick doesn't work with integers at all. Add a comment. Ertai87 Ertai87 1 1 gold badge 8 8 silver badges 25 25 bronze badges.

I was right there with you until the last paragraph. If you don't realize that multiplication is repeated addition, then either you haven't taken grade-school math, or you haven't really internalized the concept of breaking a problem down to its smaller components. If it's the latter, that's actually a bit of a flag. In fact, it's probably the only useful bit of data I would hope to take from a question like this, as an interviewer.

I don't think it's a great question. And actually, having that realization should probably pretty quickly get you to realize the loop implementation, at least. That said, a good interviewer should help you get past any one bit you struggle on, so that they can evaluate you on the other components. If I were asking this question and they got stuck, I'd prompt them with "well, what does it really mean to multiply two numbers?

What does '4 times 3' actually mean? I think that's a flag, though. Maybe there's a good reason for it; but it's a basic enough bit of data as you alluded to that if someone didn't know it, I'd want to know why. Is this person's math proficiency low enough that they won't know what p99 latency means? Or do they have trouble breaking down obvious things? It's not a veto point, but it's a something to dig into. TL;DR - Inform the interviewer that re-inventing the wheel is a bad idea Rather than entertain the interviewer's Code Golf question, I would have answered the interview question differently: Brilliant engineers at Intel , AMD , ARM and other microprocessor manufacturers have agonized for decades as how to multiply 32 bit integers together in the fewest possible cycles, and in fact, are even able to produce the correct, full 64 bit result of multiplication of 32 bit integers without overflow.

These cookies do not store any personal information. Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies.

It is mandatory to procure user consent prior to running these cookies on your website. Share This Article!!! Let's Discuss Cancel reply. Iconic One Theme Powered by Wordpress. This website uses cookies to improve your experience. The case where y is negative. This code is contributed by Anant Agarwal. WriteLine multiply 5, ;. Next Russian Peasant Multiply two numbers using bitwise operators.

Recommended Articles. Maximum number of multiplication by 3 or division by 2 operations possible on an array. Smallest string without any multiplication sign that represents the product of two given numbers.

Largest of two distinct numbers without using any conditional statements or operators. Article Contributed By :. Easy Normal Medium Hard Expert.



0コメント

  • 1000 / 1000