- 3
What is the output of this code
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.
9 Answers
+ 10
SâgĂŚrÄvĂźrĂŻ , we all appreciate your engagement in helping people here. but most of us believe that giving a code solution to an op before he has done a try first, is not the best way to support him in developing his coding skills. a more helpful way is to give hints, so that he is guided to solve the task.
I am sure you are proud of your solution you provided here, but please help us keeping sololearn what it is made for - a SELF-LEARNING platform. it would be great if you could support us. thanks a lot !
+ 6
There is no Python code, so we cannot tell what is the output...
+ 4
Hi! You tried solved?
+ 3
you want tips or the solution?
what have you tried?
+ 1
with the mathematical formula to compute sum of numbers from 1 to n:
print(n*(n+1)//2)
- 1
Rakesh Santwani
n = int(input("Enter number"))
sum = 0
# loop from 1 to n
for num in range(1, n + 1, 1):
sum = sum + num
print("Sum of first ", n, "numbers is: ", sum)
- 1
#python
value = int(input())
counter = 0
for i in range(1, value + 1):
counter += i
print(counter)
- 1
I just gave u the answer but u have to try to solve it by ur own, I recommend u to divide the problem into small pieces, the method called divide and conquer, then u will have sub problems to solve, when u solve all the sub problems then just combine them and u will solve any problem easily, even if this an easy problem to solve, u can break it down into small pieces. Like:
1# I have to ask the user for an input and the input should be an integer
2# now I have the input and the input is stored in a variable let's called it, UserInput
3# then u need to create an array, to store all the numbers that starts from [1 --> UserInput]
# after that u loop through all of these numbers, like i = 1, then I = 2 .... Until u reach the UserInput + 1,, u will ask why we add 1 to the UserInput because when u loop from 1--100, the loop will stop when the variable reach 99 then u have to add 1 to the UserInput
# then u have to sum all the numbers, like 1 + 2 + 3 + 4 .... Etc
- 2
you should to sum the first N Number, with a for loop and range, p.e.:
N = int(input())
sum = 0
for i in range(1, N+1):
sum+=i
print(sum)