Is there any easier way of multiplying numbers in Java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there any easier way of multiplying numbers in Java?

Is there an easier way of multiplying numbers. I mean usually we use for(int i=1; i< 100; i++ or (i--)) for increment or decrement part. But how can we write this code: product of 15 or 20 numbers from 1to 15, 20?

1st Aug 2020, 8:49 AM
Umidbek
Umidbek - avatar
4 Answers
+ 1
i *= 15 or i *= 20?
1st Aug 2020, 9:34 AM
Seb TheS
Seb TheS - avatar
+ 1
Then you would need another variable: int x = 1; for (int i = 1; i <= 10; i++) { x *= i; }
1st Aug 2020, 11:00 AM
Seb TheS
Seb TheS - avatar
+ 1
Thank you Seb TheS. Now it outputs the code I wanted
1st Aug 2020, 11:14 AM
Umidbek
Umidbek - avatar
0
Seb TheS. You mean for(int=1; I<=10; i*=10){ But if I run this code it outputs 1 10 But I want code to multiple numbers: 1*2*3*4*5*6*7*8*9*10=3628800 Result should be like this. Or is this the only way to get the result.
1st Aug 2020, 10:57 AM
Umidbek
Umidbek - avatar