- 3
Some one help me out
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). Sample Input 100 Sample Output 5050 Explanation: The sum of all numbers from 1 to 100 is equal to 5050. You can iterate over a range and calculate the sum of all numbers in the range. Remember, range(a, b) does not include b, thus you need to use b+1 to include b in the range. N = int(input()) #your code goes here N = range(a,b) sum = 0 for x in range("a","b"): sum +=1 print(sum) This was my code on phthon
10 Respostas
+ 3
N=int(input())
print((N+1)*N//2)
+ 1
Please tag the relevant programming language and link your code attempt
+ 1
N = int(input())
#your code goes here
sum = 0
for x in range(0,N+1):
sum +=x
print(sum)
+ 1
N = int(input())
#your code goes here
R = range(0 , N)
print (sum(R)+N)
0
Lisa how do i link my code attempt and example of a relevant programing language
in details
0
Copy your code. Then go to Code section, click +, select Python, insert Code, save.
Then come back to the thread, click +, Insert Code, sort for My Code Bits, select your code
0
* do not over-write the value of N
* range(a,b) is only an example â you need to put your actual N there
0
This is the answer for your problem
0
If you dont want to use a loop you can directly calculate the sum by n(n+1)/2 this gives us the sum of first 'n' natural numbers