+ 5
Multiply two numbers (double/float) using only + and - operators.
I need to create an algorithm that could do what i wrote in the title. Have you any ideas? I can't use any third-parts functions.
17 Antworten
+ 8
a * b = a + a + a + ... + a + a + a (b times)
+ 7
Try a for loop
for ( int i = 0; i < b; i++)
{
ans += a;
}
+ 5
Art you can just share the idea, the code is not needed.
+ 4
Might be able to help you. Just think of how we define the multiplication of 2 numbers that have decimals. For 0.5 * 2.5 we first multiply 5 with 25 to get 125 and then we add the number of decimals that were in the first 2 numbers. The result becomes 1.25. So just use a couple of while loops, if statements and a for loop to add them.
+ 3
Ok, but it works only if b is an integer..
+ 3
Im going to study on it all day and going to write a code.I need to see my Idea is working or not 😁
+ 3
Here is my solution.
https://code.sololearn.com/cIXPhFPwB0h8/#py
+ 2
Its easy but I need better python skills 😂
+ 1
Yes, but then you'll have to do (floor b times) +a
Then you'll have to do +a/1.(other part of b)
+ 1
I have a few questions about your approaches. How do you know how many decimal places the numbers have? And how do you get 1.25 from 125? Is it via number<-->string conversion? But that's kind of cheating, right? Because those methods probably require multiplication and division.
0
I tried to write a code like that, but I can't calculate 0.x * 0.y
0
Art , what's the result?
0
interested as well
0
fabrizio Im very busy with my daily works going to check it on sunday
0
You are right Androidus, thats what we need
0
a*b=a+a+a(b times)
0
Kanduri Abhinay he wanted to multiply decimal numbers too.