Q:

Print the sum of the current number and the previous number using Python programming

Print the sum of the current number and the previous number

Write a program to iterate the first 10 numbers and in each iteration, print the sum of the current and previous number.

Expected Output:

```Printing current and previous number sum in a range(10)
Current Number 0 Previous Number  0  Sum:  0
Current Number 1 Previous Number  0  Sum:  1
Current Number 2 Previous Number  1  Sum:  3
Current Number 3 Previous Number  2  Sum:  5
Current Number 4 Previous Number  3  Sum:  7
Current Number 5 Previous Number  4  Sum:  9
Current Number 6 Previous Number  5  Sum:  11
Current Number 7 Previous Number  6  Sum:  13
Current Number 8 Previous Number  7  Sum:  15
Current Number 9 Previous Number  8  Sum:  17```

Hint:

• Create a variable called `previous_num` and assign it to 0
• Iterate the first 10 numbers one by one using for loop and range() function
• Next, display the current number (`i`), previous number, and the addition of both numbers in each iteration of the loop. At last, change the value previous number to current number ( `previous_num = i`).

Solution:

``````print("Printing current and previous number and their sum in a range(10)")
previous_num = 0

# loop from 1 to 10
for i in range(1, 11):
x_sum = previous_num + i
print("Current Number", i, "Previous Number ", previous_num, " Sum: ", previous_num + i)
# modify previous number
# set it to the current number
previous_num = i
``````

Explanation in video: