+ 2

Why it is not giving right output ? Please help me . Thanks you for help . â˜ș

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

8th Mar 2022, 11:36 AM
Abhay mishra
Abhay mishra - avatar
8 Answers
8th Mar 2022, 11:45 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 3
2n-1 is the general formula to find the nth odd number.
8th Mar 2022, 11:56 AM
Simba
Simba - avatar
+ 2
Simba n is the number of terms not the last term eg. n = 3 will be 1+3+5 = 9 (three terms)
8th Mar 2022, 11:42 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 2
Thanks infinity sir . Code is exactly right . Sir if you have time may you please explain logic of i*2-1 .
8th Mar 2022, 11:49 AM
Abhay mishra
Abhay mishra - avatar
+ 1
It returns 16 for input 7.(1+3+5+7) What's your expected output?
8th Mar 2022, 11:39 AM
Simba
Simba - avatar
+ 1
Yes sir but it for 7 it should be ( 1+3+5+7+9+11+13) = 49 . Means 7 terms . but it is giving different output .
8th Mar 2022, 11:42 AM
Abhay mishra
Abhay mishra - avatar
+ 1
I have two things to say: 1. This is an arithmatic progression. You can use the formula directly to find the sum of an AP of n terms. 2. Since in this case, it is sum of odd natural numbers of n terms, the output will always be n^2 (ex: n = 7, output = 49 n = 8, output = 64 n = 10, output = 100) No need to write the code in my opinion Abhay mishra please read this
8th Mar 2022, 11:48 AM
Abhishek Tandon
Abhishek Tandon - avatar
0
for(i=1;i<=terms*2;i=i+2) {
8th Mar 2022, 8:55 PM
HungryTradie
HungryTradie - avatar