Can someone explain this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone explain this code?

https://code.sololearn.com/cnjGa9rzJEXl/?ref=app this is example given in my school book. And the most important thing 1. why i= num1 2.how the answer is correct when we're not even using num1*num2

6th Sep 2020, 1:05 PM
Atul
Atul - avatar
3 Answers
+ 5
Instead of using a while loop and a counter varibale, it can also be done with a for loop and a range: ... for i in range(0,num2): product += num1 ...
6th Sep 2020, 3:08 PM
Lothar
Lothar - avatar
+ 1
i = num1 because it is the number of iterations the loop will undergo and you don't want to modify num1 so you use an extra variable i. For example if the input is- 5 6 Then you keep on adding 6 for 5 times in the 'product' variable which is equal to 6*5 so the answer would be 30.
6th Sep 2020, 1:09 PM
Avinesh
Avinesh - avatar
+ 1
At the start you get over input two numbers. First of them will be assigned to the variable i, which is used in the while loop. i is the number of times the second input number will be added to the variable product. For the reason the product equals to num1*num2.
6th Sep 2020, 1:18 PM
JaScript
JaScript - avatar