[Type C] Python Revision Tour-1 – Chapter 1
Table of Contents
7. Write a program that reads an integer N from the keyboard, computes, and displays the sum of numbers from N to (2N) if N is nonnegative. If N is a negative number, then it’s the sum of the numbers from (2N) to N. The starting and ending points are included in the sum.
N = int(input("Enter an integer: "))
if N >= 0:
total = sum(range(N, 2*N+1))
else:
total = sum(range(2*N, N+1))
print("Sum of numbers:", total)
Output
Enter an integer: 3
Sum of numbers: 12