Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python Program to Find the Sum of First N Natural Numbers

Find Sum of N natural numbers​

This python program will find the sum of n natural numbers. First, the user have to insert any integer value. We are not using the natural number addition formula n(n+1)/2, instead we are adding the natural numbers using for loop. Then user is asked to enter a value of n and run for loop to the range of n. Inside for loop for every cycle value will be incremented by i.
N natural numbers means 1,2,3,4,5,6,7,8…….infinite
To perform the arithmetic addition operation of n numbers where n will be entered by the user.


For Example:-
Enter number : 8
Where first 8 number is 1,2,3,4,5,6,7,8

Sum of numbers = 1+2+3+4+5+6+7+8 = 36

Working:

Step 1. Start

Step 2. Initialize the value variable to 0.

Step 3. Take a one input from the user and store it into variable num.

Step 4. Start a for loop from range starts from 1 to num +1 .

Step 5. In for loop for every cycle value will be incremented by i.

Step 6.When condition gets false print value.

Step 7. Stop 

Python Program:

Method 1:

num = int(input("Enter the Number:"))
value = 0
for i in range(1, num+1):
    value = value + i

print("Sum of N natural numbers:", value)


Method 2:

num = int(input("Enter the Number:"))
sum = (num * (num+1))/2
print("The Sum of N natural Number is {}".format(sum))

# This code is contributed by Shubhanshu Arya (Prepinsta Placement Cell Student) 

Output:

Enter the Number: 8
Sum of N natural numbers: 36

One comment on “Python Program to Find the Sum of First N Natural Numbers”


  • K NITHISH

    Here this Code is for the sum of n Natural Numbers .But in the Previous page .There is no program on the Sum of N Natural Numbers Between the Previous Range.
    start =int(input(‘Enter a Start Value:’));
    end =int (input(‘Enter a End Value:’));

    print(“The Entered {} and{} values are .”.format(start,end));

    value=0;

    for i in range(start,end+1):
    value = value+i;
    print(“The value is :”,value);

    This is the code for the N natural Numbers in Given Range.Thank You.