+ 1

Need help to write a program that calculates the sum of the first N numbers given that N is the input. And please explain

Python langue need help

9th Aug 2022, 9:46 AM
Ndi Favour
Ndi Favour - avatar
6 Antworten
+ 1
I don't know Python. But here a mathematical trick you can use: The sum of the first N natural numbers is given by N(N+1)/2. Make a program asking for an input (it should be an integer, you can't sum all the REAL numbers between 0 and N, as the cardinality of R is infinite) and apply the formula above. Then just output the sum.
9th Aug 2022, 9:49 AM
Bonsai
Bonsai - avatar
0
This question is vague. Are you referring to a list? The first index? You said 'first N numbers' and 'first' is singular where 'numbers' is plural. Therefore I cannot understand the question.
9th Aug 2022, 2:17 PM
ρү૨œdԌ૨ ×
ρү૨œdԌ૨ × - avatar
0
N = int(input()) #you had your input , and inputs are always in string so you can directly convert the string input to int then you have to do the sum sum = 0 #intialisation for i in range(N+1): sum += i print(sum) #range always takes values from 0 to n-1 by default and here as u have to take upto n (incl n) so u have to add 1 to n #under for loop sum adds all the numbers upto n and then next outside for loop it's printing the total sum value
11th Aug 2022, 4:12 AM
Swaraj Soubhagya Khandai
Swaraj Soubhagya Khandai - avatar
- 1
From 1 to N( input number) , if N is 6 you habe to do: 0+0, 0+1, 1+2, 3 +3 … and so on. N = input() Num = 0 For i in N: Num += i Print(Num) I think you stuck on this challenge: n = int(input()) for x in range(1, n,2): if x % 3 == 0 and x % 5 == 0: print("SoloLearn") continue elif x % 3 == 0: print("Solo") continue elif x % 5 == 0: print("Learn") continue else: print(x)
14th Oct 2022, 7:58 PM
S3R43o3
S3R43o3 - avatar
- 2
Def summary(n1,n2): sum = n1 +n2 return sum summary(10, 5) Is that your problem ?
10th Aug 2022, 10:25 PM
S3R43o3
S3R43o3 - avatar