How do you print the sum of n natural numbers in python?
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. This 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. 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. Find the Sum of The First N Natural Numbers in PythonGiven an integer input the objective is to write a code to Find the Sum of First N Natural Numbers in C++. To do so we simply keep adding the value of the iter variable using a for loop. Example Input : num = 8 Find the Sum of the First N Natural Numbers in PythonGiven an integer input of N, the objective is to find the sum of all the natural numbers until the given input integer. To do so we can use different approaches to write the Python code and some such methods are mentioned below,
We’ll discuss and learn more about each above mentioned method in detail in the sections below.
Method 1 : Using for LoopIn this method we’ll add all the natural numbers until the given integer input using for loop in Python. Python Codenum = 5 sum = 0 for i in range(num+1): sum+=i print(sum) OutputWorkingFor a user input num.
ExplanationGiven an integer input N, the objective is to calculate the sum of all the natural numbers until the integer N. To do so we iterate through all the numbers that lay within N and keep incrementing the sum value. The algorithm for the above code is as follows,
The output for the above mentioned code is the sum of all the natural numbers until the given value. Method 2 : Using Formula for the Sum of Nth TermIn this Method we use the formula for finding the sum of N term. Formula to Find the Sum of N terms Sum = ( Num * ( Num + 1 ) ) / 2 Python Codenum = 5 print(int(num*(num+1)/2)) OutputWorkingFor a user input n.
ExplanationGiven an integer input N, the objective is to calculate the sum of all the natural numbers until the integer N. To do so we iterate through all the numbers that lay within N and keep incrementing the sum value. The algorithm for the above code is as follows,
This algorithm uses the formula n(n+1)/2 that can be used to find sum of first N natural numbers. This also reduces the time complexity from O(n) to O(1). The output for the above mentioned code is the sum of all the natural numbers until the given value.
Method 3 : Using RecursionThis method uses Recursion to recursively add the natural numbers up to the given integer input using recursion in c++. Python Codedef getSum(num): if num == 1: return 1 return num + getSum(num-1) num = 5 print(getSum(num)) Output
WorkingFor a user input n.
ExplanationGiven an integer input N, the objective is to calculate the sum of all the natural numbers until the integer N. To do so we recursively call a function iterate through all the numbers that lay within N and keep incrementing the sum value. The algorithm for the above code is as follows,
The output for the above mentioned code is the sum of all the natural numbers until the given value. Prime Course Trailer
Related BannersGet PrepInsta Prime & get Access to all 150+ courses offered by PrepInsta in One Subscription
Get over 150+ course One SubscriptionCourses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Checkout list of all the video courses in PrepInsta Prime SubscriptionCheckout list of all the video courses in PrepInsta Prime SubscriptionGetting Started
How do you print the sum of natural numbers?Using the Mathematical Formula. #include int main(). int n = 40; // declare & initialize local variable n.. int sum = (n * (n + 1) ) / 2; /* define the mathematical formula to calculate the sum of given number. */. printf("Sum of %d natural number is = %d", n, sum); // print the sum of natural number.. return 0;. How do you print the sum of the first n natural numbers in Python?num = 5 sum = 0 for i in range(num+1): sum+=i print(sum). num = 5 print(int(num*(num+1)/2)). def getSum(num): if num == 1: return 1 return num + getSum(num-1) num = 5 print(getSum(num)). How do you find the sum of n in python?Follow the steps:. Take a input from user in your python program using input() function.. Convert a user inputted number to an integer using int() function.. Calculates sum of number by using this formula n * (n+1) / 2 in your python program.. After that, the print name sum variable.. How do you find the sum of n natural numbers?The formula of the sum of first n natural numbers is S=n(n+1)2 .
|