Sum of n number in python

GOCOURSE

Sum of n number :

Program:


  n = int(input("Enter the number of integers to sum: "))

  # Initialize the sum to zero
  total = 0

  # Ask the user to enter n integers
  for i in range(n):
        num = int(input("Enter an integer: "))
        total += num

   # Output the sum
   print("The sum of the", n, "integers is", total)


Output:


  Enter the number of integers to sum: 5
  Enter an integer: 3
  Enter an integer: 5
  Enter an integer: 1
  Enter an integer: 7
  Enter an integer: 2
  The sum of the 5 integers is 18

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send