Calculating the sum of natural numbers is a fundamental task in programming. This article will cover various C Program to Compute the Sum of Natural Numbers. We will explore multiple examples, each demonstrating a different approach to solve the problem. Additionally, we will discuss the prerequisites, provide detailed explanations for each example, and conclude with a summary of what we’ve learned.
Prerequisites
Before we delve into the examples, ensure you have the following prerequisites:
- A C compiler (such as GCC)
- A text editor or IDE for writing your C code
- Basic understanding of C programming concepts, especially loops and functions
1. Calculating the Sum of Natural Numbers
In this section, we will look at several methods to compute the sum of natural numbers in C.
1.1 Using a for Loop
Example 1: Compute the Sum Using a for Loop
This method uses a for
loop to iterate through natural numbers and compute the sum of natural numbers.
Code
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
sum += i;
}
printf("The sum of the first %d natural numbers is %d.\n", n, sum);
return 0;
}
Explanation
- Include necessary header:
#include <stdio.h>
for input/output functions. - Input the number:
scanf
reads a positive integer from the user. - Iterate using a
for
loop: The loop runs from 1 ton
, adding each number tosum
. - Output the result:
printf
displays the sum of the natural numbers.
Output
Enter a positive integer: 10
The sum of the first 10 natural numbers is 55.
1.2 Using a while Loop
Example 2: Compute the Sum Using a while Loop
This method uses a while
loop to iterate through natural numbers and compute their sum.
Code
#include <stdio.h>
int main() {
int n, sum = 0, i = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
while (i <= n) {
sum += i;
++i;
}
printf("The sum of the first %d natural numbers is %d.\n", n, sum);
return 0;
}
Explanation
- Include necessary header:
#include <stdio.h>
for input/output functions. - Input the number:
scanf
reads a positive integer from the user. - Iterate using a
while
loop: The loop runs from 1 ton
, adding each number tosum
. - Output the result:
printf
displays the sum of the natural numbers.
Output
Enter a positive integer: 5
The sum of the first 5 natural numbers is 15.
1.3 Using Recursion
Example 3: Compute the Sum Using Recursion
This method uses recursion to compute the sum of natural numbers.
Code
#include <stdio.h>
int sumOfNaturalNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
int sum = sumOfNaturalNumbers(n);
printf("The sum of the first %d natural numbers is %d.\n", n, sum);
return 0;
}
int sumOfNaturalNumbers(int n) {
if (n == 0)
return 0;
else
return n + sumOfNaturalNumbers(n - 1);
}
Explanation
- Include necessary header:
#include <stdio.h>
for input/output functions. - Declare a recursive function:
int sumOfNaturalNumbers(int n)
to compute the sum. - Input the number:
scanf
reads a positive integer from the user. - Call the recursive function:
sumOfNaturalNumbers(n)
computes the sum recursively. - Output the result:
printf
displays the sum of the natural numbers.
Output
Enter a positive integer: 7
The sum of the first 7 natural numbers is 28.
1.4 Using Formula
Example 4: Compute the Sum Using the Formula
This method uses the formula to compute the sum of natural numbers directly which is given below.
\frac{n(n+1)}{2}
Code
#include <stdio.h>
int main() {
int n;
int sum;
printf("Enter a positive integer: ");
scanf("%d", &n);
sum = n * (n + 1) / 2;
printf("The sum of the first %d natural numbers is %d.\n", n, sum);
return 0;
}
Explanation
- Include necessary header:
#include <stdio.h>
for input/output functions. - Input the number:
scanf
reads a positive integer from the user. - Calculate the sum using the formula: n(n+1)2
- Output the result:
printf
displays the sum of the natural numbers.
Output
Enter a positive integer: 10
The sum of the first 10 natural numbers is 55.
1.5 Using Arrays
Example 5: Compute the Sum Using Arrays
This method stores the natural numbers in an array and computes their sum.
Code
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; ++i) {
arr[i] = i + 1;
sum += arr[i];
}
printf("The sum of the first %d natural numbers is %d.\n", n, sum);
return 0;
}
Explanation
- Include necessary header:
#include <stdio.h>
for input/output functions. - Input the number:
scanf
reads a positive integer from the user. - Store numbers in an array: A loop fills the array with natural numbers.
- Compute the sum: Sum the elements of the array.
- Output the result:
printf
displays the sum of the natural numbers.
Output
Enter a positive integer: 6
The sum of the first 6 natural numbers is 21.
2. Conclusion
In this article, we explored various methods to compute the sum of natural numbers in C: using a for
loop, using a while
loop, using recursion, using the formula, and using arrays. Each method demonstrates different aspects of handling loops, functions, and arrays in C programming. By understanding these methods, you can choose the one that best fits your specific needs and enhance your skills in basic arithmetic operations and programming logic in C.