- Related Questions & Answers
- Python Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- C++ program to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- Sum of the series 1.2.3 + 2.3.+ … + n(n+1)(n+2) in C
- C++ program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + … + n/a^n)
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n! in C++
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + … + upto n terms in C++
- Java program to remove all numbers in a string except "1" and "2"?
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2
- Python Program to Compute the Value of Euler's Number e. Use the Formula: e = 1 + 1/1! + 1/2! + …… 1/n!
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Sum of the series Kn + ( K(n-1) * (K-1)1 ) + ( K(n-2) * (K-1)2 ) + ... (K-1)n in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

When it is required to display the sum all the natural numbers within a given range, a method can be defined that uses a loop to iterate over the elements, and returns the sum of these numbers as output.

Below is a demonstration of the same −

def sum_natural_nums(val): my_sum = 0 for i in range(1, val + 1): my_sum += i * (i + 1) / 2 return my_sum val = 9 print("The value is ") print(val) print("The sum of natural numbers upto 9 is : ") print(sum_natural_nums(val))

The value is 9 The sum of natural numbers upto 9 is : 165.0

A method named ‘sum_natural_nums’ is defined that takes a number as parameter.

A sum value is defined as 0.

A loop is iterated over the number passed as a parameter.

The sum is incremented every time a number is encountered.

This is returned as output.

The value for number of natural numbers whose sum needs to be found is defined.

The method is called by passing this number as a parameter.

The relevant output is displayed on the console.

Advertisements