Sum of first 10 natural numbers in python using for loop

  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

In this post, you will learn a Python program to find the sum of n numbers using a for loop.

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

  1. Read the input [num] from the user.
  2. Initialize a variable sum with zero.
  3. Use a for loop to iterate from 1 to num.
  4. Inside the loop, add the num to the sum.
  5. At the end, print the value of the sum.

Python Program to find sum of n numbers using for loop

# Sum of natural numbers up to num

num = int[input["Please enter the number: "]]

sum = 0

for value in range[1, num + 1]:
    sum = sum + value
    
print[sum]

Output1:

Please enter the number: 20
210

We can see the sum of the number till 20 is 210 as the output.

Output2:

Please enter the number: 12
78

Output3:

Please enter the number: 23
276

Related Articles

Sum of even numbers in Python
Swapping of two numbers in Python
Find average of n numbers in Python
Python program to print all even numbers between 1 to 100
Sum of n numbers in Python using while loop
Python program to list even and odd numbers of a list
Python program to print odd numbers within a given range
Python program to multiply two numbers
Program to find area of triangle in Python
Find area of rectangle in Python
Swapping of two numbers in Python
Find average of n numbers in Python
Print multiplication table in Python
Python program to multiply two matrices
Python program to find area of circle
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Count consonants in a string Python
Convert array to list Python

General Knowledge

Learn Popular Language

Blogs

  • Jan 3

    Stateful vs Stateless

    A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

  • Dec 29

    Best programming language to learn in 2021

    In this article, we have mentioned the analyzed results of the best programming language for 2021...

  • Dec 20

    How is Python best for mobile app development?

    Python has a set of useful Libraries and Packages that minimize the use of code...

  • July 18

    Learn all about Emoji

    In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

  • Jan 10

    Data Science Recruitment of Freshers

    In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

Follow us


  • eTutorialsPoint©Copyright 2016-2022. All Rights Reserved.

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]].

Chủ Đề