Python Program to Find the Sum of Natural Numbers Using While Loop Last Updated : 02 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Calculating the Sum of N numbers in Python using while loops is very easy. In this article, we will understand how we can calculate the sum of N numbers in Python using while loop.Calculate Sum of Natural Numbers in Python Using While LoopBelow are some of the examples by which we can see how we can calculate the sum of N numbers in Python:Example 1: Calculate the Sum of N Numbers Using While LoopIn this example, a Python function sum_of_natural_numbers is defined to calculate the sum of the first N natural numbers using a while loop. The function initializes variables total and count, iterates through the numbers from 1 to N, and accumulates the sum in the total variable. The result is then printed, demonstrating the sum of the first 7 natural numbers. Python def sum_of_natural_numbers(N): total = 0 count = 1 while count <= N: total += count count += 1 return total N = 7 result = sum_of_natural_numbers(N) print("The Sum of the First", N, "Natural Numbers is:", result) OutputThe Sum of the First 7 Natural Numbers is: 28 Example 2: Calculate the Sum of N Numbers Using While Loop and User InputIn this example, the user inputs a value N, and the program calculates the sum of the first N natural numbers using a while loop, efficiently decrementing 'count' from N to 1 and accumulating the sum in the 'total' variable. The final result is then printed. Python # Takes the input of N given by the user N = int(input("Enter the value of N: ")) # Initialize the variables 'count' and 'total' count = N total = 0 while count: total += count count -= 1 # Prints the sum of N natural numbers. print("The sum of the first", N, "natural numbers is:", total) Output:Enter the value of N: 16The sum of the first 16 natural numbers is: 136 Comment More infoAdvertise with us Next Article Python Program to Find the Sum of Natural Numbers Using While Loop sai_teja_anantha Follow Improve Article Tags : Python Python Programs Python loop-programs Practice Tags : python Similar Reads Python Program for cube sum of first n natural numbers We are given a number n and we need to print the sum of series 13 + 23 + 33 + 43 + .......+ n3 till the n-th term in python.Examples:Input: n = 5Output: 225Explanation: 13 + 23 + 33 + 43 + 53 = 225Let's discuss some of the ways to do it.Using Mathematical Formula: Most efficient solution is to use t 2 min read Python Program to calculate sum and average of three numbers We are given three numbers, and our task is to calculate the sum and average of these numbers. The average of the numbers is defined as the sum of the given numbers divided by the total number of elements. In this case, it will be the sum of given three numbers divided by 3. Example: Input: 10, 20, 3 min read Python Program to Find the Total Sum of a Nested List Using Recursion A nested list is given. The task is to print the sum of this list using recursion. A nested list is a list whose elements can also be a list. Examples : Input: [1,2,[3]] Output: 6 Input: [[4,5],[7,8,[20]],100] Output: 144 Input: [[1,2,3],[4,[5,6]],7] Output: 28 Recursion: In recursion, a function ca 5 min read Python Program to Print the Natural Numbers Summation Pattern Given a natural number n, the task is to write a Python program to first find the sum of first n natural numbers and then print each step as a pattern. Input: 5 Output: 1 = 11 + 2 = 31 + 2 + 3 = 61 + 2 + 3 + 4 = 101 + 2 + 3 + 4 + 5 = 15 Input: 10 Output: 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 = 2 min read Sum of squares of first N natural numbers - Python The goal here is to calculate the sum of squares of the first n natural numbers. The sum of squares is the result of adding up the squares of each number from 1 to n. For example, if n = 5, the sum of squares would be 12 + 22 + 32+ 42+52=55. Let's explore different methods to compute this sum effici 3 min read Python Program to Get Sum of N Armstrong Number Given a number N, determine the sum of the first N Armstrong numbers using Python. Example: Input : 11Output : 568First 11 Armstrong numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, lies to, 370Their summation is 578Method 1: Using Iterative methodsCreate a while loop that breaks when the desired number of Ar 3 min read Python Program To Find The Sum Of Last N Nodes Of The Given Linked List Given a linked list and a number n. Find the sum of the last n nodes of the linked list.Constraints: 0 <= n <= number of nodes in the linked list. Examples: Input: 10->6->8->4->12, n = 2 Output: 16 Sum of last two nodes: 12 + 4 = 16 Input: 15->7->9->5->16->14, n = 4 10 min read Python Program For Finding Subarray With Given Sum - Set 1 (Nonnegative Numbers) Given an unsorted array of nonnegative integers, find a continuous subarray which adds to a given number. Examples : Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33 Output: Sum found between indexes 2 and 4 Sum of elements between indices 2 and 4 is 20 + 3 + 10 = 33 Input: arr[] = {1, 4, 0, 0, 3, 10, 5 min read Python program to find the group sum till each K in a list Given a List, the task is to write a Python program to perform grouping of sum till K occurs. Examples: Input : test_list = [2, 3, 5, 6, 2, 6, 8, 9, 4, 6, 1], K = 6 Output : [10, 6, 2, 6, 21, 6, 1] Explanation : 2 + 3 + 5 = 10, grouped and cumulated before 6. Input : test_list = [2, 3, 5, 6, 2, 6, 8 3 min read Like