Javatpoint Logo
Javatpoint Logo

Sum of first N natural numbers in C

As we know natural numbers contain all the positive numbers starting from 1, 2, 3, to n numbers or infinity. For example, suppose when we calculate the sum of the first 25 numbers. That means we start adding the numbers from 1 to the given number 25, and the process is called the sum of the first N natural number. In this topic, we will learn how to find the sum of first n numbers using a C program.

Sum of first N natural numbers in C

Mathematical Formula

Following is the representation to find the sum of n natural numbers using the mathematical formula:

Where n defines the natural number.

Suppose, we want to calculate the sum of the first 20 natural number, we need to put in a mathematical formula to get the sum:

Pseudo code

  1. int i, sum = 0, num
  2. input positive number
  3. i = 0
  4. do
  5. sum = sum + i
  6. i = i + 1
  7. iterate the value of i < = num
  8. display the sum of the first natural number.

Using for Loop

Let's create a C program that determines the sum of n natural numbers using for loop.

SumOfNaturalNumber1.c

Output:

Enter a positive number: 25
Sum of the first 25 number is: 325

Using while Loop

Let's create a C program that determines the sum of n natural numbers using while loop.

SumOfNaturalNumber2.c

Output:

Enter a positive number: 20
Sum of the first 20 natural number is: 210

In the above example, when we enter a positive number 20, the while loop continuously iterates over the counter value between i = 0 to 20. At each iteration, the value of i is added to the variable sum, and i is incremented by 1. When the while condition becomes false, it exits the loop and prints the sum of the first 20 natural numbers.

Using do-while Loop

Let us consider the following example to calculate the sum of natural number using Do while loop.

SumOfNaturalNumber3.c

Output:

Enter a positive number: 30
Sum of the first 30 natural number is: 465

In the above example, when we enter a positive number 30, the do loop continuously iterates the counter value between i = 0 to 30. At each iteration, the value of i is added to the variable sum, and i is incremented by 1. When the while condition becomes false, it exits from the while loop and prints the sum of the first 30 natural numbers.

Using the Mathematical Formula

Let's write a program to print the sum of n natural number using the mathematical formula.

SumOfNaturalNumber4.c

Output:

Sum of 40 natural number is = 840

Using Function

Let's consider the following example to calculate the sum of natural number using function in C.

SumOfNaturalNumber5.c

Output:

Enter a natural number: 100
Sum of the 100 natural number is: 5050

Sum of n natural numbers Between a Given Range

Calculate the sum of n natural number from any starting number to the specify last number.

SumOfNaturalNumber6.c

Output:

Enter the first number: 1
Up to the last number natural number: 25
Sum of natural number is = 325

Using Recursion

Let's consider the following example to calculate the sum of natural number using recursion.

SumOfNaturalNumber7.c

Output:

Enter any positive number to calculate the sum of natural no. 50
Sum of the first 50 natural number is: 1275

Using an Array

SumOfNaturalNumber8.c

Output:

Enter a positive number as we want to sum the natural number: 5
 Enter the number one by one: 
2
4
5
6
7
Sum of the given number is = 24






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA