Sum of first 10 natural numbers in python using for loop
In this post, you will learn a Python program to find the sum of n numbers using a for loop. Show
In the given example, we have used the for loop to calculate the sum of n numbers. First, we have taken an int data type number input from the user and stored it in a variable num. Initially, the sum is initialised to 0. Then, we used the for loop for iteration in the range from 1 to num + 1 to increase the number up to the given input. In each iteration, we have added the value to the sum and, at last, printed the sum variable. Algorithm
Python Program to find sum of n numbers using for loop
Output1:
We can see the sum of the number till 20 is 210 as the output. Output2:
Output3:
Related Articles
Sum of even numbers in Python General Knowledge
Learn Popular Language
A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities... In this article, we have mentioned the analyzed results of the best programming language for 2021... Python has a set of useful Libraries and Packages that minimize the use of code... In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more... In this
article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...
How do you find the sum of the first 10 natural numbers in Python?So, a list of natural number can be defined as: N= {0, 1, 2, 3, 4, .... and so on}. 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.. How do you print the sum of the first 10 natural numbers in Python using for loop?Sum and average of first n natural numbers. Accept the number n from a user. Use input() function to accept integer number from a user.. Run a loop till the entered number. Next, run a for loop till the entered number using the range() function. ... . Calculate the sum. ... . Calculate the average.. How do you find the sum of natural numbers in a for loop in Python?First, we have taken an int data type number input from the user and stored it in a variable num.. Read the input (num) from the user.. Initialize a variable sum with zero.. Use a for loop to iterate from 1 to num.. Inside the loop, add the num to the sum.. At the end, print the value of the sum.. How do you print the sum of the first n natural numbers in Python?num = 5 sum = 0 for i in range(num+1): sum+=i print(sum). num = 5 print(int(num*(num+1)/2)). def getSum(num): if num == 1: return 1 return num + getSum(num-1) num = 5 print(getSum(num)). |