Python program for sum of n numbers using while loop

In the program below, we've used an if...else statement in combination with a while loop to calculate the sum of natural numbers up to num.

Source Code

# Sum of natural numbers up to num

num = 16

if num < 0:
   print["Enter a positive number"]
else:
   sum = 0
   # use while loop to iterate until zero
   while[num > 0]:
       sum += num
       num -= 1
   print["The sum is", sum]

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 while loop to iterate until num becomes zero. In each iteration of the loop, we have added the num to sum and the value of num is decreased by 1.

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.

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

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.

The question was tp :write a program to find the sum of n natural numbers using while loop in python.

n = int[input["Enter a number: "]]
i = 1
while i 0]:
    totalSum += number
    number -= 1
    print ["The sum is" , totalSum]

answered Jul 28 at 21:45

num = int[input['Enter the number : ']]
sum = 0
while 0 0]:.
sum += num..

How do you find the sum of a number in a while loop?

In each iteration you are add the number with the sum variable which is initialised as 0 . so need to change the addition expression & add update the sum variable in each iteration with the previous iteration sum + the new entered number in each iteration.

How do you print the sum of n numbers in Python?

Python program to find sum of first n numbers The for loop is used for iteration the range function is used to find the sum between the given range of input. The input + 1 is used for increment, to add the numbers I have used sum = sum + num. I have used print[“Result of first n numbers “,sum] to get the output.

How do you sum 1 n in Python?

If you don't want to use a formula, use the range[] class. To sum the integers from 1 to n: Pass 1 and n+1 to the range class, e.g. range[1, n + 1] . Pass the range object to the sum[] function.

Chủ Đề