Python Program to Find Sum of n Numbers

Python Program to Find Sum of n Numbers

Python program to find sum of n numbers; In this tutorial, you will learn how to find or calculate sum of n numbers using for loop, while loop and function.

Python Program to Calculate Sum of n Numbers

  • Calculate/Find the sum of n natural numbers using loop and range function.
  • Find/Calculate Sum of n natural numbers in python using while loop
  • Python program to find/calculate the sum of numbers in a given list
  • The mathematical formula to find/calculate the sum of n numbers with python program
  • Python Program to Find/Calculate sum of n odd natural numbers.
  • Python Program to Find/Calculate sum of n even natural numbers.

1: Find/Calculate the sum of n natural numbers using loop and range function

  • First of all, you can use a python input() function in your python program that takes a user to enter the number (n) to calculate the sum.
  • Next, declare a variable that name sum, it will contain sum of n natural numbers sum.
  • Next, run loop till the entered number using the for loop and range() function.
  • Inside a loop, calculate the sum of n numbers using a sum = sum + current number formula.
  • After the loop ends, print the sum variable that contains the sum of n numbers
n = input("Enter Number to calculate sum")
n = int (n)
sum = 0
for num in range(0, n+1, 1):
    sum = sum+num
print("SUM of first ", n, "numbers is: ", sum )

Output:

Enter Number to calculate sum 5 
Sum of first 5 number is: 15

2: Find/Calculate Sum of n natural numbers in python using while loop

Also, use the while loop in python to calculate the sum of n numbers.

  • First of all, you can use a python input() function in your python program that takes a user to enter the number (n) to calculate the sum.
  • Next, declare a variable that name sum, it will contain the sum of n natural numbers sum.
  • Run while loop until n is greater than zero
  • Add the current value of n to sum variable. And, decrement n number by 1 in while loop body
  • After the loop finishes, the print sum name variable.

Python program to find the sum of n numbers using While loop:

n = input("Enter Number to calculate sum")
n = int (n)
total_numbers = n
sum=0
while (n >= 0):
    sum += n
    n-=1
print ("sum using while loop ", sum)

Output:

Enter Number to calculate sum 5 
Sum using while loop  15 

3: Python program to Find/Calculate the sum of numbers in a given list

  • Declare a variable that name sum, it will contain the sum of n natural numbers sum.
  • Next, define list and assign a value to a python list.
  • Run for a loop and Add the current value of n to sum variable.
  • After the loop finishes, the print sum name variable.
sum = 0
list = [11,4,5,7,99,10,12]
for num in list:
    sum = sum +num
print ("sum of list element is : ", sum)

Output:

sum of list element is :  148 

4: The mathematical formula to Find/Calculate the sum of n numbers with python program

In the above programs, you have learned how to calculate the sum of n numbers using for loop, while loop and range function.

Now, you will learn how to calculate/find sum of n numbers in python without for loop, while loop in python. Calculate the sum directly using a mathematical formula in python program.

The sum of the n natural number mathematical formula is = n * (n+1) / 2.

In the below python program, you will learn how to use this mathematical formula is = n * (n+1) / 2 to find/calculate sum of n numbers in python programs.

Follow the steps:

  • Take a input from user in your python program using input() function.
  • Convert a user inputted number to an integer using int() function.
  • Calculates sum of number by using this formula n * (n+1) / 2 in your python program.
  • After that, the print name sum variable.
n = input("Enter a number to calculate sum")

n = int (n)
sum = n * (n+1) / 2
print("Sum of fthe irst ", n, "natural numbers using formula is: ", sum )

Output:

Enter a number to calculate sum 5 
Sum of fthe irst  5 natural numbers using formula is:  15.0 

5: Python Program to Find/Calculate sum of n odd natural numbers

  • Take input from user using python input() function in your python program.
  • Next, declare a variable that name sum, it will contain the sum of n odd numbers.
  • Next, run loop till the entered number using the for loop and range() function.
  • Inside a loop, calculate the sum of n odd numbers using a sum = sum + current number formula with (not (num % 2) == 0).
  • After the loop ends, print the sum variable that contains the sum of n odd numbers.

Python program to find sum of n odd numbers:

n = input("Enter Number to calculate sum")
n = int (n)
sum = 0
for num in range(0, n+1, 1):
    
    if(not (num % 2) == 0):
      sum += num;
      
print("SUM of odd numbers is: ", sum )

Output:

Enter Number to calculate sum 5 
SUM of odd numbers is:  9 

6: Python Program to Find/Calculate sum of n even natural numbers

  • Take input from the user using python input() function in your python program.
  • Next, declare a variable that name sum, it will contain the sum of n even numbers.
  • Next, run loop till the entered number using the for loop and range() function.
  • Inside a loop, calculate the sum of n even numbers using a sum = sum + current number formula with if test condition ((num % 2) == 0).
  • After the loop ends, print the sum variable that contains the sum of n even numbers.

Python program to find sum of n even numbers:

n = input("Enter Number to calculate sum")
n = int (n)
sum = 0
for num in range(0, n+1, 1):
    
    if((num % 2) == 0):
      sum += num;
      
print("SUM of even numbers is: ", sum )

Output:

Enter Number to calculate sum 5 
SUM of even numbers is:  9 

Recommended Python Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to Python Program to Find Sum of n Numbers

  1. thank you.I started coding recently and I met your site from a social group.than I came to visit your site and I am done with other tutorials. Thanks lot

Leave a Reply

Your email address will not be published. Required fields are marked *