JavaScript Program to Find the Sum of Natural Numbers

To understand this example, you should have the knowledge of the following JavaScript programming topics:


The positive integers 1, 2, 3, ... are known as natural numbers.

Example 1: Sum of Natural Numbers Using for Loop

Output

Enter a positive integer: 100
The sum of natural numbers: 5050

In the above program, the user is prompted to enter a number.

The parseInt() converts the numeric string value to an integer value.

The for loop is used to find the sum of natural numbers up to the number provided by the user.

  • The value of sum is 0 initially.
  • Then, a for loop is used to iterate from i = 1 to 100.
  • In each iteration, i is added to sum and the value of i is increased by 1.
  • When i becomes 101, the test condition is false and sum will be equal to 0 + 1 + 2 + ... + 100.

Example 2: Sum of Natural Numbers Using while Loop

Output

Enter a positive integer: 100
The sum of natural numbers: 5050

In the above program, the user is prompted to enter a number.

The while loop is used to find the sum of natural numbers.

  • The while loop continues until the number is less than or equal to 100.
  • During each iteration, i is added to the sum variable and the value of i is increased by 1.
  • When i becomes 101, the test condition is false and sum will be equal to 0 + 1 + 2 + ... + 100.