While creating a python program to find sum of n numbers, there can be various ways through which the input can be provided to the program. Numbers are segregated as real and imaginary numbers. The real numbers are those that we can plot. Among these real numbers are rational and irrational numbers. Natural numbers are those rational numbers that occur naturally.
Basically, these are the counting numbers. All the numbers from 1 onwards are included in natural numbers. Note that all the natural numbers are positive and do not include zero.
The user can provide the upper limit and lower limit of all the natural numbers which are to be added together. Another option can be that the user inputs all the natural numbers that are to be added.
The code provided below adds all the natural numbers present between 1 and 100. The limit can be redefined as per the wish of the user. Note that the program uses a WHILE loop. Along with it, it also checks whether the upper limit entered by the user is a positive or negative number.
n = int (input (“Enter any natural number: “) if n < 0: print (“Wrong input. Please enter a positive number.”) else: sum = 0 while (n > 0): sum +=n n -=1 print (“The sum of the natural numbers is: “, sum)
Enter any natural number:
The sum of the natural numbers is 5500.
The code provided above first checks whether the number input is positive or not. If it is less than zero, then it will raise an error. If the number input is positive, then it will operate the while loop and keep adding all the natural numbers that fall between the input number and 1. This is done by decreasing the value of the number input after each iteration.
Once the condition for the WHILE loop is proven wrong, that is, when the number has reached 0, then the program will exit the loop and print the sum of the natural numbers.
One can use the FOR loop instead of the WHILE loop as well. The logic of the program will remain the same. Functions can also be used to calculate the sum of natural numbers.
There is another method through which the Python program to find the sum of n numbers can be created without using the loop. This is a mathematical formula that is used to obtain the sum of natural numbers. The code will be as follows.
n = int (input (“Enter any natural number: “) if n < 0: print (“Wrong input. Please enter a positive number.”) else: sum = n * (n+1) / 2 print (“The sum of the natural numbers is: “, sum)
Enter any natural number:
The sum of the natural numbers is 136.
Frequently Asked Questions
Q1. How do you find the sum of n numbers in Python?
Using WHILE loop, FOR loop, function, and mathematical formula are some of the ways through which Python program sum of n numbers can be created.
Q2. How do you find the sum of n numbers?
The Sum of n natural numbers is found by adding all the natural numbers lying between 1 and n, as defined.
Q3. How do you find the sum of first n natural numbers in Python?
Python program sum of n numbers is created by iterating the sum and adding all the natural numbers that lie between the input number, that is, n and 1.
Q4. How do you sum numbers in Python?
With the help of the addition operator, which is represented by the symbol “+”, the sum of the numbers is found in Python.