Multiply two numbers (double/float) using only + and - operators. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

28th Nov 2018, 7:23 PM
fabrizio
17 Answers
+ 8
a * b = a + a + a + ... + a + a + a (b times)
28th Nov 2018, 7:39 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 7
Try a for loop for ( int i = 0; i < b; i++) { ans += a; }
28th Nov 2018, 7:46 PM
Roel
Roel - avatar
+ 5
Art you can just share the idea, the code is not needed.
29th Nov 2018, 4:55 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 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.
30th Nov 2018, 10:45 AM
Alexandru Turculet
Alexandru Turculet - avatar
+ 3
Ok, but it works only if b is an integer..
28th Nov 2018, 7:46 PM
fabrizio
+ 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 😁
29th Nov 2018, 5:10 AM
Art
30th Nov 2018, 10:41 PM
Mayur Garg
Mayur Garg - avatar
+ 2
Its easy but I need better python skills 😂
29th Nov 2018, 4:45 AM
Art
+ 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)
28th Nov 2018, 7:49 PM
Roel
Roel - avatar
+ 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.
30th Nov 2018, 6:12 PM
Kishalaya Saha
Kishalaya Saha - avatar
0
I tried to write a code like that, but I can't calculate 0.x * 0.y
28th Nov 2018, 8:24 PM
fabrizio
0
Art , what's the result?
29th Nov 2018, 9:00 PM
fabrizio
0
interested as well
29th Nov 2018, 9:02 PM
Ryan Wakefield
Ryan Wakefield - avatar
0
fabrizio Im very busy with my daily works going to check it on sunday
30th Nov 2018, 8:54 AM
Art
0
You are right Androidus, thats what we need
30th Nov 2018, 11:12 AM
Art
0
a*b=a+a+a(b times)
3rd Dec 2018, 8:05 AM
Kanduri Abhinay
Kanduri Abhinay - avatar
0
Kanduri Abhinay he wanted to multiply decimal numbers too.
3rd Dec 2018, 9:56 AM
Alexandru Turculet
Alexandru Turculet - avatar