I am trying to write a code to find all common divisors of two given numbers using a function. Please, note that I don't want to find the greatest common divisor, I want to display all the divisors that number 1 and number 2 have in common.
I am close to solve it, but my code is not working because lacks of something I am not able to see right now. Can you help me please? My code is the following:
What does it do that you don't want it to do, or what does it not do that you think it should? What inputs do you feed it? What output do you get?
I see you've include iostream, but then you don't actually use any C++. Is there a reason for that? C style IO, printf and scanf, are common sources of mistakes and for this situation you don't need their advantages.
Right. I am thinking, I know I need more conditions...
*Edited*: I set i to 2 but for example for 4 and 8 the output is 2 and 4 and should be 4, right? or maybe I am a disaster at maths, LOL. I am thinking how can I do to make the program that if a divisor is repeated, multiplies it. Everything inside for loop.
*Edited*: Damn! I wish I can think as a machine...
for example for 4 and 8 the output is 2 and 4 and should be 4, right?
Not if you're trying to find all the common divisors, no. So what are you trying to do? Stop typing, start understanding the problem.
Here's a hard, cold fact about programming; if you don't know what you're trying to do, the chances of writing a correct programme are very, very, very small. It's actually a very common mistake made by programmers; even full-time software engineers often make this mistake. A lot of the modern software techniques such as (part of) the whole agile bandwagon and test driven development are to a large degree about trying to deal with this problem and to make sure that programmers don't just type away without knowing what the actual problem is.
Yes, I was confused. All the common factors is simply find all the factors that are the same in both numbers. If I set i to 2, the problem is almost solved. I tried with another example: 1225 and 490. The output gives me 5 and 7 but also the product of those numbers: 35 (5x7), 49 (7x7) and 245 (5x7x7).
Yes, I was thinking about prime factors but since this exercise doesn't ask for them, I would say the problem is then solved. Just curiosity, I wonder how the program would be if the exercise would state to print only prime factors...
For 1225 and 490 you seem happy about 5 and 7, but not about (5x7), (7x7) or (5x7x7).
Yet, for 8 and 4 you seemed happy about (2x2), but not about 2.
That is inconsistent.
You cannot solve the problem by tweaking the code. Tweaking might give you something that seems to be correct for some cases, but that is by accident rather by design.
What you have to do first is to describe the problem so clearly that everyone unambiguously understands it. The correct solution will be based on that description.
Yes, at first I failed to understand what the exercise is asking. The exercise says: "All common factors of two given numbers inserted by user". You are right, then, I understand is all the factors that 2 numbers have in common and that's it.