Find the Sum of n Natural Numbers in C
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.
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;}
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
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
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;}
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 sumint 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;}
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!!