In the program below, we've used an Show
Source Code
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 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. Python Program to Find the Sum of Natural NumbersNatural numbers: As the name specifies, a natural number is the number that occurs commonly and obviously in the nature. It is a whole, non-negative number. Some mathematicians think that a natural number must contain 0 and some don't believe this theory. So, a list of natural number can be defined as: See this example: This example shows the sum of the first 100 positive numbers (0-100) Output: For Videos Join Our Youtube Channel: Join Now Feedback
Help Others, Please ShareThis is a Python Program to find the sum of first N Natural Numbers. Problem Description The program takes in the the number of terms and finds the sum of first N Natural Numbers. Problem Solution 1. Take in the number of natural numbers to find the sum of and store it in a separate variable. Program/Source Code Here is source code of the Python Program to find the sum of first N Natural Numbers. The program output is also shown below. n=int(input("Enter a number: ")) sum1 = 0 while(n > 0): sum1=sum1+n n=n-1 print("The sum of first n natural numbers is",sum1) Program Explanation 1.
User must enter the number of natural numbers to find the sum of. Runtime Test Cases Case 1: Enter a number: 18 The sum of first n natural numbers is 171 Case 2: Enter a number: 167 The sum of first n natural numbers is 14028 Sanfoundry Global Education & Learning Series – Python Programs. To practice all Python programs, here is complete set of 150+ Python Problems and Solutions. Next Steps:
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn. Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses. How do you write a program to sum the first n natural numbers in Python?27 comments on “Python Program to Find the Sum of First N Natural Numbers”. Fahad. def sum(n): result=0. for i in range(n+1): ... . Kiran. num=int(input(“enter a number”)) sum=(num*(num+1)/2) print(“the sum of “,num, “natural numbers is : {}”.format(sum)) ... . pavanicherla62. Simple program for this quetion is: n= int(input(“”)) i=1.. How do you find the sum of 5 numbers in Python?“program for sum of five numbers in python” Code Answer's. a = int(input("Enter first number:")). b = int(input("Enter second number:")). sum = a+b.. print(sum). How do you find the sum of a natural number in Python?See this example:. num = int(input("Enter a number: ")). if num < 0:. print("Enter a positive number"). sum = 0.. # use while loop to iterate un till zero.. while(num > 0):. sum += num.. What is the sum of first five natural numbers?So, the set of natural numbers will be { 1, 2, 3, 4, 5, 6, …………. }. So, now from the set of natural numbers. First five numbers will be 1, 2, 3, 4 and 5. So, the sum of these five numbers is 1 + 2 + 3 + 4 + 5 = 15.
|