{"id":6835,"date":"2023-10-31T17:17:16","date_gmt":"2023-10-31T11:47:16","guid":{"rendered":"https:\/\/python-programs.com\/?p=6835"},"modified":"2023-11-10T12:09:55","modified_gmt":"2023-11-10T06:39:55","slug":"python-program-to-find-the-sum-of-natural-numbers","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-sum-of-natural-numbers\/","title":{"rendered":"Python Program to Find the Sum of Natural Numbers"},"content":{"rendered":"
Natural Number:<\/strong><\/p>\n A natural number, as the name implies, is a number that occurs often and clearly in nature. It is a whole number with no negative digits.<\/p>\n Some mathematicians agree that a natural number must contain 0 while others do not. As a result, a list of natural numbers can be described as follows<\/p>\n Prerequisite:<\/strong><\/p>\n Explore more instances related to python concepts from Python Programming Examples<\/a> Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Example 1:<\/strong><\/p>\n Input :<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input :<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to print the sum of natural numbers in python:<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Instead of iterating till number the best and efficient solution is to use mathematical formula given below.<\/p>\n Formula:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Natural Number: A natural number, as the name implies, is a number that occurs often and clearly in nature. It is a whole number with no negative digits. Some mathematicians agree that a natural number must contain 0 while others do not. As a result, a list of natural numbers can be described as follows …<\/p>\nN= 1 , 2 , 3 , 4 , 5 etc.<\/pre>\n
\n
Examples:<\/strong><\/h3>\n
number = 5<\/pre>\n
Sum of natural numbers till 5 = 15<\/pre>\n
number = 43<\/pre>\n
Sum of natural numbers till 43 = 946<\/pre>\n
Program to Find the Sum of Natural Numbers in Python<\/h2>\n
\n
Method #1:Using for loop<\/h3>\n
\n
# given number\r\nnumber = 5\r\n# Take a variable say sum and initialize to 0.\r\nsum = 0\r\n# Iterate from 1 to number using for loop and range function\r\nfor i in range(1, number+1):\r\n # add the iterater value to sum.\r\n sum = sum + i\r\n# print the sum\r\nprint(\"Sum of natural numbers till\", number, \"=\", sum)\r\n<\/pre>\n
Sum of natural numbers till 5 = 15<\/pre>\n
Method #2:Using while loop<\/h3>\n
\n
# given number\r\nnumber = 5\r\n# Take a variable say sum and initialize to 0.\r\nsum = 0\r\n# iterate till the number becomes 0 using while loop\r\nwhile(number != 0):\r\n # add the number value to sum\r\n sum = sum+number\r\n # decrement the number\r\n number = number-1\r\n# print the sum\r\nprint(\"Sum of natural numbers\", \"=\", sum)\r\n<\/pre>\n
Sum of natural numbers = 15<\/pre>\n
Method #3:Using mathematical formula<\/h3>\n
sum = ( n * (n +1) ) \/ 2<\/pre>\n
# given number\r\nnumber = 5\r\n# Using mathematical formula\r\nsum = (number * (number+1))\/\/2\r\n# print the sum\r\nprint(\"Sum of natural numbers till\", number, \"=\", sum)\r\n<\/pre>\n
Sum of natural numbers till 5 = 15<\/pre>\n
\n