Computer Science
Write a program that reads an integer N from the keyboard computes and displays the sum of the numbers from N to (2 * N) if N is nonnegative. If N is a negative number, then it's the sum of the numbers from (2 * N) to N. The starting and ending points are included in the sum.
Python
Python Funda
30 Likes
Answer
n = int(input("Enter N: "))
sum = 0
if n < 0:
for i in range(2 * n, n + 1):
sum += i
else:
for i in range(n, 2 * n + 1):
sum += i
print("Sum =", sum)
Output
Enter N: 5
Sum = 45
Enter N: -5
Sum = -45
Answered By
15 Likes
Related Questions
Write a program that asks the user the day number in a year in the range 2 to 365 and asks the first day of the year — Sunday or Monday or Tuesday etc. Then the program should display the day on the day-number that has been input.
One foot equals 12 inches. Write a function that accepts a length written in feet as an argument and returns this length written in inches. Write a second function that asks the user for a number of feet and returns this value. Write a third function that accepts a number of inches and displays this to the screen. Use these three functions to write a program that asks the user for a number of feet and tells them the corresponding number of inches.
Write a program that reads a date as an integer in the format MMDDYYYY. The program will call a function that prints print out the date in the format <Month Name> <day>, <year>.
Sample run :
Enter date : 12252019 December 25, 2019
Write a program that prints a table on two columns — table that helps converting miles into kilometres.