Here in this article, we have provided a python program that can find the sum of n natural numbers using Python loop.
Prerequisite topics to create this Program
- Python Input, Output
- Python if…else statement
- Python Data types
- Python Operators
- Python Loop
- First, we ask the user to enter a number n.
- Using the int() function we will convert the entered string into an integer value.
- Now using the for loop we will go through the range 1 to the number n+1, add all the numbers and print sum as an output.
Python Program to Find the Sum of Natural Numbers:
n = int(input("Enter the number:" )) sum=0 if n > 1: for i in range(1,n+1): sum+=i print("The sum of all natural number upto",n,"is ",sum) else: print("Please Enter a value greater than 1")
Enter the number:2 The sum of all natural numbers upto 2 is 3
Enter the number:20 The sum of all natural numbers upto 20 is 210
People are also reading:
- Python Program to Display Calendar
- Perfect Number in C
- Python Program to Check Whether a String is Palindrome or Not
- Python Program to Check if a Number is Odd or Even
- WAP to swap two numbers
- Python Program to Convert Celsius To Fahrenheit
- WAP in C++ & Python to Insert an Element in an Array
- Python Program to Solve Quadratic Equation
- WAP in C++ & Python to Calculate Compound Interest
- WAP to calculate the sum of two numbers