0

Python

No one likes homework, but your math teacher has given you an assignment to find the sum of the first N numbers. Let’s save some time by creating a program to do the calculation for you! Take a number N as input and output the sum of all numbers from 1 to N (including N).

26th Mar 2022, 4:03 PM
Ofere Believe
Ofere Believe - avatar
9 ответов
+ 1
hint : just need a single loop
26th Mar 2022, 4:19 PM
NonStop CODING
NonStop CODING - avatar
+ 2
Yup, no one likes Homework, especially if it's someone else's
26th Mar 2022, 4:04 PM
Abhishek Tandon
Abhishek Tandon - avatar
+ 2
Paul K Sadler 🤓🤔🤣👍
27th Mar 2022, 3:45 AM
NonStop CODING
NonStop CODING - avatar
+ 1
Paul K Sadler it will throw wrong answer mathematically you are right. but write this way print( int( ( N * ( N + 1 ) ) / 2 ) ) btw using A.P, best👍
26th Mar 2022, 6:06 PM
NonStop CODING
NonStop CODING - avatar
+ 1
NonStop CODING the code will not generate an error. For example for 100 the result will be 5050.0(float). If an Integer is required using int as you did will resolve that issue alternatively floor division can be used: n * (1 + n) // 2
26th Mar 2022, 6:14 PM
Paul K Sadler
Paul K Sadler - avatar
+ 1
Paul K Sadler i said wrong answer, no error floor division will work 100% true🤓
26th Mar 2022, 6:21 PM
NonStop CODING
NonStop CODING - avatar
+ 1
NonStop CODING Ah, got you, the description said nothing about an Integer result being required, good catch
26th Mar 2022, 6:26 PM
Paul K Sadler
Paul K Sadler - avatar
+ 1
Thank you I got it😊
26th Mar 2022, 9:37 PM
Ofere Believe
Ofere Believe - avatar
0
print(n*(1+n)/2) or a loop as NonStop CODING suggested above.
26th Mar 2022, 6:02 PM
Paul K Sadler
Paul K Sadler - avatar