¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita0
Ahh
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. Python I can solve this , help A i still didn’t even right a code , I have no idea, sorry
7 Respuestas
+ 3
Joudy Ramy
I will give you some hints.
1. You will need an integer input called N.
2. Create a variable called total and assign a value of 0 to it.
2. Create a range() and iterate through it.
3. As you iterate through the range, add the number to your total using the += trick.
When you print your total, it will show the sum of all numbers up to N
Remember that a range does not include the upper number, so you may need to adjust the range to suit. (+1)
+ 3
You could approach the problem like this:
* get input N and convert it to integer
* create a result variable, set it to 0
* use a for loop and the range function to iterate over the numbers from 1 to N
* on each iteration, add the current number to the result variable
* after the loop: print result variable
+ 2
Please tag the relevant programming language. If you can solve it, do it. If not, please link your code and explain at which point you are stuck.
+ 1
hey maybe you could include your code?
that way we can see what to help with.
+ 1
Thanks so much everyone , i solved it
+ 1
Great! 👍
+ 1
Joudy Ramy
Well done, great feeling!
🖐😁👍