Program for sum of consecutive numbers. My code does not work. It displays error " File size too large" Explain plz. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Program for sum of consecutive numbers. My code does not work. It displays error " File size too large" Explain plz.

N = int(input()) #your code goes here sum=0 while sum<=N : if sum==N : print(sum) else: sum += 1

12th Sep 2021, 1:42 PM
Frank
13 Answers
+ 3
Hey guys I finally managed to do it using while loop. https://code.sololearn.com/cdBu9vLS05mm/?ref=app
13th Sep 2021, 5:30 AM
Frank
+ 6
Frank , your code is running in an infinite loop, as soon as condition 'if sum == N' is evaluated to True. to get rid of this, you can add a 'break' in the next line after 'print(sum)'
12th Sep 2021, 1:54 PM
Lothar
Lothar - avatar
+ 3
Frank , sorry, in my last post i have not noticed that you are talking about "building the sum of consecutive nunbers". i only saw the infinite loop. if your input is 5, the result should be: 1+ 2 + 3 + 4 + 5 which is 15. to get this task done, it would be better to use a range() object, iterate over it and build the sum from all numbers coming from the range.
12th Sep 2021, 2:07 PM
Lothar
Lothar - avatar
+ 3
Frank , that is great - do your own try. if you get stuck somewhere come back. happy coding and good success!
12th Sep 2021, 2:18 PM
Lothar
Lothar - avatar
+ 1
U are getting this error because u are not increment your while loop because of this your code work for infinite
12th Sep 2021, 1:50 PM
Pariket Thakur
Pariket Thakur - avatar
+ 1
Thanks bro
12th Sep 2021, 1:54 PM
Frank
+ 1
Added a break , now it justs prints the input number
12th Sep 2021, 2:03 PM
Frank
+ 1
Yes bro i know I can use range I had already googled it but i wanted to do some other way. I could use the program from one of those websites but i want to practice.
12th Sep 2021, 2:10 PM
Frank
+ 1
Frank try this N = int(input()) print(int((N/2)*(N+1)))
12th Sep 2021, 3:05 PM
Pariket Thakur
Pariket Thakur - avatar
+ 1
Better you can use n*(n+1)/2 it will be good
13th Sep 2021, 8:18 AM
Mr.Dark
Mr.Dark - avatar
0
How do i resolve this issue
12th Sep 2021, 1:52 PM
Frank
0
N = int(input()) #your code goes here x=0 for i in range(1,N+1): x += i print (x)
14th Sep 2021, 6:06 AM
Mohammad Zanganeh
Mohammad Zanganeh - avatar
- 1
13th Sep 2021, 10:46 AM
Coding
Coding - avatar