Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python Program to Find the Total Sum of a Nested List Using Recursion
Next article icon

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 Loop

Below 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 Loop

In 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) 

Output
The Sum of the First 7 Natural Numbers is: 28 

Example 2: Calculate the Sum of N Numbers Using While Loop and User Input

In 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: 16
The sum of the first 16 natural numbers is: 136

Next Article
Python Program to Find the Total Sum of a Nested List Using Recursion
author
sai_teja_anantha
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 = 225 Let's discuss some of the ways to do it. Using Mathematical Formula: Most efficient solution is to u
    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 for Print Number series without using any loop
    Problem - Givens Two number N and K, our task is to subtract a number K from N until number(N) is greater than zero, once the N becomes negative or zero then we start adding K until that number become the original number(N). Note : Not allow to use any loop. Examples : Input : N = 15 K = 5 Output :
    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 to find all Strong Numbers in given list
    Given a list, write a Python program to find all the Strong numbers in a given list of numbers. A Strong Number is a number that is equal to the sum of factorial of its digits. Examples: Input : [1, 2, 5, 145, 654, 34] Output : [1, 2, 145] Input : [15, 58, 75, 675, 145, 2] Output : [145, 2] Explanat
    6 min read
  • Python Program for Find minimum sum of factors of number
    Given a number, find minimum sum of its factors.Examples: Input : 12Output : 7Explanation: Following are different ways to factorize 12 andsum of factors in different ways.12 = 12 * 1 = 12 + 1 = 1312 = 2 * 6 = 2 + 6 = 812 = 3 * 4 = 3 + 4 = 712 = 2 * 2 * 3 = 2 + 2 + 3 = 7Therefore minimum sum is 7Inp
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences