What have you written so far? Make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.
output the sum of all numbers from 1 to x
Use a running total inside a loop with a condition that does not exceed the number the user inputted.
If the number entered is less than zero, output an error message.
Use a validation method to ensure the number is not a negative number (less than zero).
It is up to you to figure out what this code would look like. Give it some work, then comeback with a specific question. Remember, just like programming, vague questions will get vague answers. Good luck.
This was a problem that a friend and myself were trying to solve. We kept getting stuck on how to obtain the sum from numbers 1 to x, x being any number.
We kept getting stuck on how to obtain the sum from numbers 1 to x, x being any number.
A basic for loop adding the current index to a variable should be all it takes for naive approach. There's also a famous formula for calculating that particular sum without using a loop.