Here’s an example code in Python to print the sum of the current number and the previous number:
# Define a list of numbers numbers = [1, 2, 3, 4, 5] # Loop through the list for i in range(1, len(numbers)): # Calculate the sum of the current number and the previous number current_number = numbers[i] previous_number = numbers[i - 1] sum = current_number + previous_number # Print the sum print("The sum of", current_number, "and", previous_number, "is", sum)
In this code, we first define a list of numbers called numbers
. Then we loop through the list using the range()
function, starting from index 1 (the second element in the list) and ending at the length of the list.
For each element in the list, we calculate the sum of the current number and the previous number by using the current index i
and the previous index i-1
. We assign the current number to the variable current_number
, the previous number to the variable previous_number
, and the sum to the variable sum
.
Finally, we print out the sum using the print()
function, along with a message that describes the sum.
When you run this code, it should output:
The sum of 2 and 1 is 3 The sum of 3 and 2 is 5 The sum of 4 and 3 is 7 The sum of 5 and 4 is 9
Note that you can replace the values in the numbers
list with any other sequence of numbers you want to calculate the sum of the current number and the previous number.