prints the sum of the numbers 1 to n in python

Question:

GOAL:
Write a program that asks the user for a number n and prints the sum
of the numbers 1 to n. The program keeps asking for a number until
the user enters 0.

expected output:
enter an integer number (0 to end): 5
1+2+3+4+5 = 15

I am able to solve the second problem which is until the user enters 0.
the problem I’m having is printing the numbers in a loop.

1+2+3+4+5 = 15

I am thinking maybe if I use a loop within a loop I can accomplish this. This is my current code.I’ve seen other questions answer this programming question but I wanna know how to specifically print the numbers leading up to the number I entered

num = int(input( "enter a integer: " ))
sum_num =0

if num != 0:
   for i in range(1, num+1): 
       sum_num += i
       print(sum_num)
else:
     exit()
Asked By: Angel Valenzuela

||

Answers:

Several issues with your code:

  1. Your while loop will never end. Its intended purpose is not clear.
  2. You are summing 1 instead of i each time in your loop.
  3. Your print statement only occurs at the end. You can include it within your loop.
  4. In Python, range(n) excludes n, so use range(n + 1) instead.
  5. You do not need to convert integers to string in order to print them.

Putting this all together:

num = int(input( "enter a integer: " ))
sum_num = 0

for i in range(1, num+1): 
    sum_num += i
    print(sum_num)

enter a integer: 5
1
3
6
10
15
Answered By: jpp

I think this matches what you want:

while True:
  output = ""
  num = int(input("enter a integer: "))

  if num == 0:
    exit()

  for i in range(1, num+1):
    output += "{}".format(i)
    if i != num:
      output += "+"
  output += " = {}".format(sum(range(num+1)))
  print (output)

This prints out the sum of integers and then the answer, then waits for the next input. Example output:

$ python test.py
enter a integer: 5
1+2+3+4+5 = 15
enter a integer: 4
1+2+3+4 = 10
enter a integer: 3
1+2+3 = 6
enter a integer: 2
1+2 = 3
enter a integer: 1
1 = 1
enter a integer: 0
 = 0
Answered By: TobyGWilliams

n*(n+1)/2

“zBody zmust zbe zat zleast z30 zcharacters; zyou zentered z9 z…”

Answered By: Vlad K.
num=int(input("Enter the Number " ))
sum=0

for i in range(1, num + 1):
        sum = sum+ i
print(sum)


Enter the Number 9
45

Answered By: Amit Kumar
n = int(input("enter the no. : "))
sum = 0
for i in range(1,n+1):
    if(i<n):
        print(i,"+", end=" ")
    else:
        print(i,end=" ")
    sum = sum + i
print("=",sum)
Answered By: Jahanvi Verma

num = int(input())
total = num
for x in range(num):
total += x
print(total)

Answered By: Omagad

What about:

def cumsum(n: int) -> int:
    """Helper function to calculate cumulative sum"""                                                                                
    return sum(range(n))                                                                                                                                                                                        

if __name__ == "__main__":
    """Main entry point"""
                                                   
    while n := int(input("Number: ")):                                                                  
        print(cumsum(n))
  • cumsum calculates the cumulative sum
  • __name__ == "__main__" is python-specific boilerplate
  • n := is the "walrus-operator", meaning it assigns to n at the point it’s evaluating the input. The while loop terminates at 0 simply because 0 is "falsy".

Without the boilerplate code, in 2 lines:

while n := int(input("Number: ")):
    print(sum(range(n)))
Answered By: martyn
n = int (input (''))
print(n*(n+1)//2)
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.