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:
- Function Definition: The program begins by defining a function called
sum_of_natural_numbersusing thedefkeyword. This function takes one parametern, representing the upper limit of the natural numbers to be summed. - Input Validation: Inside the function, there’s a conditional statement to check if the input
nis less than zero. Ifnis 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. - Calculation of Sum: If the input
nis a valid positive integer, the function proceeds to calculate the sum of natural numbers up ton. It uses the formula(n * (n + 1)) // 2to calculate the sum efficiently without needing to iterate through all the numbers. - 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 variablen. - Function Call: It then calls the
sum_of_natural_numbersfunction with the input valuenas an argument. This computes the sum of natural numbers up to the provided input value. - 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

