+ 1

Please tell me role of i in this code . And why it gives more terms than required by user .

https://code.sololearn.com/cLMQzzI0mdNX/?ref=app

2nd Mar 2022, 8:55 AM
Abhay mishra
Abhay mishra - avatar
4 Respuestas
+ 1
You are using i for iteration It is giving more terms because You have already printed 2 terms So you need to subtract 2 from terms And i must be less than terms -2 because i is starting from 0 So write i < terms - 2 as your condition in while loop
2nd Mar 2022, 9:18 AM
NonStop CODING
NonStop CODING - avatar
+ 2
Sir you method worked but for i <= terms-3 . Please tell me why -3 and you said for -2 . And thanks you for helping me ☺️
2nd Mar 2022, 9:44 AM
Abhay mishra
Abhay mishra - avatar
+ 1
i working as a counter initial i =0; and in while(i<=term) and each time i is increasing and condition will be false when i<=term will be false
2nd Mar 2022, 9:16 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
Abhay mishra Yeah you can -3 as well For example You entered 1, 1 Terms = 5 1, 1, 2, 3, 5 // it has 5 terms So if you write i <= terms - 3 Then only you will be able to print the above series Reason: Just note carefully you have already input 1, 1 Which means you already have 2 terms in the beginning. Now you need to find 3 more terms Why? Because Terms = 5 // as entered So for finding rest of the 3 terms you need to run the while loop only 3 times But if you write i <= terms Your while loop will runs 6 times. because i = 0 And you will have 3 extra terms in your output ( 5 + 3 = 8 terms) But we need only 5 terms not 8 Because we entered terms = 5 So that's why you need to subtract -3 as per your code because your condition was i <= terms But I subtracted -2 because My condition was i < terms - 2 I did not used the = operator Both of us are correct
2nd Mar 2022, 10:42 AM
NonStop CODING
NonStop CODING - avatar