Continuous Multiplications | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Continuous Multiplications

The given code declares a loop and calculates the multiplication of all numbers from 1 to 10000. Make the necessary changes to break the loop when the result gets larger than 10000 and output the result. Code: res <- 1 for(x in 1:10000) { res <- res * x if(res > 10000) { break } print(res) } why does not it work, please, help..

11th Oct 2021, 4:16 PM
Aziz Ergashev
Aziz Ergashev - avatar
7 Answers
+ 3
First print res then do break
11th Oct 2021, 4:19 PM
A͢J
A͢J - avatar
+ 2
Put the if statement before multiplication
11th Oct 2021, 4:57 PM
Oma Falk
Oma Falk - avatar
+ 2
Or just put the print in the line above the break...
11th Oct 2021, 5:01 PM
Lisa
Lisa - avatar
+ 2
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Thank you! now I understand, the code works !!!
11th Oct 2021, 5:25 PM
Aziz Ergashev
Aziz Ergashev - avatar
0
Thanks, tried it but doesn't work. The result is considered incorrect.
11th Oct 2021, 4:53 PM
Aziz Ergashev
Aziz Ergashev - avatar
0
let num = 1; for(i = 1; i<=100, i++;){ num*=i; //your code goes here if (num > 10000) break } console.log(num);
2nd Nov 2023, 5:40 PM
Prostibozenko Ilja
Prostibozenko Ilja - avatar
0
@Prostibozenko Ilja I have some doubt on ur suggestion . how come num involve here when the result is stored in "i" , i think "i"should be used but include while loop.
15th Feb 2024, 6:46 AM
Nikhita
Nikhita - avatar