# prompt: simple python program to find sum of n numbers

 # prompt: simple python program to find sum of n numbers


def sum_n_numbers(n):

  """

  Calculates the sum of the first n natural numbers.


  Args:

      n: An integer representing the number of terms to sum.


  Returns:

      The sum of the first n natural numbers.

      Returns an error message if n is not a positive integer.

  """

  if not isinstance(n, int) or n <= 0:

    return "Input must be a positive integer."

  else:

    return n * (n + 1) // 2


# Get input from the user.

try:

  num_terms = int(input("Enter a positive integer: "))

  result = sum_n_numbers(num_terms)

  print(f"The sum of the first {num_terms} natural numbers is: {result}")

except ValueError:

  print("Invalid input. Please enter a positive integer.")

Comments

Popular posts from this blog

Ecommerce website

Yes, Python is an object-oriented programming (OOP) language, but it is also a multi-paradigm language

Your task is to find the missing number.