Python Program to Find the Sum of Natural Numbers

The provided Python program calculates the sum of natural numbers up to a given positive integer input by the user. It defines a function called sum_of_natural_numbers which takes a single parameter n, representing the upper limit until which natural numbers are summed. Within this function, it first checks if the input n is less than zero, indicating an invalid input. If n is negative, the function returns a message prompting the user to enter a positive integer. Otherwise, it calculates the sum of natural numbers up to n using the formula (n * (n + 1)) // 2, which exploits the arithmetic series sum formula. After defining the function, the program prompts the user to enter a positive integer, reads the input, and then calls the sum_of_natural_numbers function with the input as an argument. Finally, it prints out the calculated sum of natural numbers up to the provided input value. This program efficiently computes the sum without needing to iterate through all the numbers, making it particularly useful for large values of n.


Source code

def sum_of_natural_numbers(n):
    """
    Function to find the sum of natural numbers up to 'n'.
    
    Parameters:
        n (int): The upper limit until which natural numbers are summed.
    
    Returns:
        int: The sum of natural numbers up to 'n'.
    """
    if n < 0:
        return "Please enter a positive integer."
    else:
        return (n * (n + 1)) // 2

# Example usage:
n = int(input("Enter a positive integer: "))
result = sum_of_natural_numbers(n)
print("The sum of natural numbers up to", n, "is:", result)

Description

Certainly! Here’s a step-by-step description of the provided Python program:

  1. Function Definition: The program begins by defining a function called sum_of_natural_numbers using the def keyword. This function takes one parameter n, representing the upper limit of the natural numbers to be summed.
  2. Input Validation: Inside the function, there’s a conditional statement to check if the input n is less than zero. If n is less than zero, it means the input is not a valid positive integer. In such a case, the function returns a message asking the user to input a positive integer.
  3. Calculation of Sum: If the input n is a valid positive integer, the function proceeds to calculate the sum of natural numbers up to n. It uses the formula (n * (n + 1)) // 2 to calculate the sum efficiently without needing to iterate through all the numbers.
  4. User Input and Function Invocation: After defining the function, the program prompts the user to input a positive integer using the input() function and stores the input value in the variable n.
  5. Function Call: It then calls the sum_of_natural_numbers function with the input value n as an argument. This computes the sum of natural numbers up to the provided input value.
  6. Output Display: Finally, the program prints out the calculated sum of natural numbers up to the provided input value using the print() function.

By following these steps, the program efficiently calculates and displays the sum of natural numbers up to the input value entered by the user.


Screenshot


Download

By Admin

Leave a Reply

Your email address will not be published. Required fields are marked *