In the program below, we've used an Show
Source Code
Output The sum is 136 Note: To test the program for a different number, change the value of num. Initially, the sum is initialized to 0. And, the number is stored in variable num. Then, we used the We could have solved the above problem without using a loop by using the following formula. n*(n+1)/2 For example, if n = 16, the sum would be (16*17)/2 = 136. Your turn: Modify the above program to find the sum of natural numbers using the formula below. In this tutorial, we will write a simple Python program to calculate the sum of first n natural numbers. Program to calculate sum of first n natural numbers in PythonIn this program we are not using the natural number addition formula n(n+1)/2, instead we are adding the natural numbers using while loop. The user is asked to enter the value of n and then the program calculates the sum of natural numbers upto the entered value n. # Program published on https://beginnersbook.com # Python program to calculate the sum of n Natural Numbers # n denotes upto which number you want to calculate the sum # for example, if n is 5 then the sum of first 5 natural numbers num = int(input("Enter the value of n: ")) hold = num sum = 0 if num <= 0: print("Enter a whole positive number!") else: while num > 0: sum = sum + num num = num - 1; # displaying output print("Sum of first", hold, "natural numbers is: ", sum) Output 1: Enter the value of n: 6 Sum of first 6 natural numbers is: 21 Output 2: Enter the value of n: 0 Enter a whole positive number! Output 3: Enter the value of n: -10 Enter a whole positive number! Output 4: Enter the value of n: 20 Sum of first 20 natural numbers is: 210 Related Python Examples:1. Python program to add
digits of a number In this python tutorial, you will learn about Python program to find sum of n numbers and also we will check:
Now, we can see how to find the sum of 3 numbers in python. In this example, I have taken three inputs. The int data type is used and the “+” operator is used to find the sum of the three numbers. Example:
We can see the sum of three inputs is 16 as the output. You can refer to the below screenshot for the output. Python program to find the sum of 3 numbersThis is how to find sum of 3 numbers in Python. You may like to read, How to print factorial of a number in Python and How to calculate simple interest in Python. Python program to find sum of n numbers using for loopHere, we can how to find the sum of n numbers using for loop in python.
Example:
We can see the sum of number till 10 is 55 as the output. You can refer to the below screenshot for the output. Python program to find the sum of n numbers using for loopThis is how to find sum of n numbers using for loop in Python. You may like Python For Loop with Examples Python program to find sum of n numbers using a functionHere, we can how to find the sum of n numbers using a function in python.
Example:
As the input is 6. We can see the sum of numbers is 21 as the output. The below screenshot shows the output. Python program to find the sum of n numbers using a functionThis is how to find sum of n numbers using a function in Python. You may like, Function in Python. Python program to find sum of n numbers using while loopNow, we can see how to find sum of n numbers using while loop in python.
Example:
The below screenshot shows the sum of numbers as the output. python program to find sum of n numbers using while loopThe above code, we can use to find sum of n numbers using while loop in Python. Check out While loop in Python. Python program to find sum of n numbers using recursionHere, we can see how to find sum of n numbers using recursion in python.
Example:
The below screenshot show the sum of numbers upto 9 as the output. Python program to find sum of n numbers using recursionThe above code we can use to find sum of n numbers using recursion in Python. Python program to find sum of n even numbersNow, we can see how to find sum of n even numbers in python
Example:
The sum of even numbers is the output. You can refer to the below screenshot for the output. python program to find sum of n even numbersThis is how to find sum of n even numbers in Python. Python program to find sum of n odd numbersHere, we can see how to find the sum of n odd numbers in python
Example:
We can see the sum of odd numbers as the output. You can refer to the below screenshot for the output. Python program to find the sum of n odd numbersThis code, we can use to find sum of n odd numbers in Python. Python program to find sum of n prime numbersHere, we can see how to find the sum of n prime numbers in python.
Example:
We can see the sum of prime numbers with the given range as the output. You can refer to the below screenshot for the output. Python program to find the sum of n prime numbersThis is how to find sum of n prime numbers in Python. You may like to read, Check if a number is a prime Python. Python program to find sum of first n numbersHere, we can see how to find the sum of first n number in python.
Example:
The below screenshot shows the sum of first n numbers as the output. Python program to find the sum of first n numbersThe above code we can use to find sum of first n numbers in Python. Python program to find sum of first n even numbersNow, we can see how to find the sum of first n even numbers in python.
Example:
The below screenshot shows the sum of even numbers as the output. Python program to find the sum of first n even numbersThe above code we can use to find sum of first n even numbers in Python. Python program to find sum of numbers in a listNow, we can see how to find the sum of numbers in a list in python
Example:
We can the sum of numbers from the list is 17 as the output. You can refer to the below screenshot for the output. Python program to find the sum of numbers in a listThis is the Python program to find sum of numbers in a list. Python program to find sum of numbers in a stringNow, we can see how to find the sum of numbers in the string in python
Example:
In the below screenshot we can see the input as 1a2b34, the sum of numbers is 10 as the output. Python program to find the sum of numbers in a stringThis is the code to find sum of numbers in a string in Python. Python program to find sum of numbers in a fileNow, we can see how to find the sum of numbers in a file in python
Example:
The below screenshot shows the content of the file. find sum of numbers in a file in pythonThe numbers from the file is added and the output is 22. You can refer to the below screenshot for the output. Python program to find the sum of numbers in a fileThe above code, we can use to find sum of numbers in a file in Python. You may like the following python tutorials:
In this Python tutorial, we have learned about the Python program to find the sum of numbers. Also, we covered these below topics:
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile. How do you find the sum of n numbers in Python?Sum and average using a mathematical formula. The sum of the first n natural number = n * (n+1) / 2.. the average of first n natural number = (n * (n+1) / 2) / n.. How do you write a sum function in Python?The sum() function is used to get the sum of all items in an iterable.. Version: ... . Syntax: sum(iterable[, start]). Parameter: ... . Return value: ... . Example: Python sum() num = [3.5, 5, 2, -5] # start parameter is not provided numSum = sum(num) print(numSum) # start = 15 numSum = sum(num, 15) print(numSum) ... . Pictorial Presentation:. How do you write the sum of n natural numbers in Python?See this example:. num = int(input("Enter a number: ")). if num < 0:. print("Enter a positive number"). sum = 0.. # use while loop to iterate un till zero.. while(num > 0):. sum += num.. What is sum () sum () in Python?Python sum() Function
The sum() function returns a number, the sum of all items in an iterable.
|