Find the Sum of n Natural Numbers in C

Find the Sum of n Natural Numbers in C

2 mins read45 Views Comment
Updated on Apr 30, 2024 09:04 IST

Got a bunch of numbers? Adding them all up can be a drag! This article will show you a super easy way to find the total of any set of counting numbers (1, 2, 3, ...) in C programming. It's like finding the grand prize of a counting game without counting everything! We'll use a special trick to get the answer fast, so you can spend less time crunching numbers and more time playing with your code.

2023_08_Sum-ofn.jpg

Natural numbers are positive integers greater than zero. In simple terms, natural numbers are the counting numbers used to represent quantities. The set of natural numbers includes 1, 2, 3, 4, …… and so on, with no upper limit.

Notation: N (capital letter)

The Formula for the Sum of N Natural Numbers

Sum = n * (n + 1) /2, where

n: number of terms

Let’s take an example to verify the above formula.

Find the sum of the first 5 natural numbers.

Method-1: Add them Manually

1 + 2 + 3 + 4 + 5 = 15

Method-2: Using Formula

Sum = 5 * (5 + 1) / 2 = 5*6/2 = 30/2 = 15
=> Sum = 15

In this article, we will learn how to find the sum of n natural numbers in C programming.

Method-1: The Iterative Approach to find the sum of first n Natural Numbers


 
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
// Iterating from 1 to N
for(int i = 1; i <= n; i++) {
sum += i; // Accumulating the sum
}
printf("The sum of first %d natural numbers is: %d\n", n, sum);
return 0;
}
Copy code

Output

Enter the value of N: 5
The sum of first 5 natural numbers is: 15

Explanation

  • Start by initializing the sum = 0 and then iterate from 1 up to the number N.
  • For each iteration, add the current number to the sum.
  • Print the computed sum.

Must Check: Difference Between Natural Number and Whole Number

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

– / –
40 hours
4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
1 month
– / –
2 months
– / –
50 hours
– / –
4 months

Method-2: The Formulaic Approach to find the sum of first n Natural Numbers


 
#include <stdio.h>
int main() {
int n;
long long sum; // To handle larger values of N
printf("Enter the value of N: ");
scanf("%d", &n);
// Using the formula to compute the sum
sum = (long long)n * (n + 1) / 2;
printf("The sum of first %d natural numbers is: %lld\n", n, sum);
return 0;
}
Copy code

Output

Enter the value of N: 10
The sum of first 10 natural numbers is: 55

Explanation

  • Here, we will use the formula of the sum of the first n natural numbers, i.e., n(n+1)/2.
  • The formula will give the sum without the need for iteration.
  • Print the computed sum.

Method-3: The Recursive Approach to find the sum of first n Natural Numbers


 
#include <stdio.h>
// Recursive function to compute the sum
int sumOfNaturalNumbers(int n) {
if(n == 0) return 0; // Base case
return n + sumOfNaturalNumbers(n - 1); // Recursive call
}
int main() {
int n;
printf("Enter the value of N: ");
scanf("%d", &n);
int sum = sumOfNaturalNumbers(n);
printf("The sum of first %d natural numbers is: %d\n", n, sum);
return 0;
}
Copy code

Output

Enter the value of N: 20
The sum of first 20 natural numbers is: 210

Explanation

  • Define a recursive function that calls itself.
  • In the base case, when n == 0, it will return 0.
  • For other values of n, the function will add n to the sum of the first n-1 natural numbers.
  • Call the defined recursive function in the main function and then print the result.

Conclusion

Finding the sum of the first n natural numbers in C is one of the basic programming problems that every beginner-level student tries to improve their coding skills. Here, in this article, we have discussed three different approaches to find the sum of first n-natural numbers with the help of examples.
Hope you will like the article.
Keep Learning!!
Keep Sharing!!

Hello World Program in C
10 Must-Read C Programming Books of All Time
C programming examples 
What is Interpreter: Types, Advantages and Disadvantages
Learn About Array of Structure in C
Explore strtok() Function in C
Guide to tolower() Function in C with Examples
How to Reverse an Array in C
Learn to Implement strstr() Function in C
Concatenate Strings with strcat() Function in C
Compare Strings with strcmp Function in C
Explore toupper() Function in C with Examples
All About Strlen() Function in C
Understanding Strchr() Function in C
strcpy() Function in C
About the Author