C Program to Calculate the Sum of Natural Numbers #16
C Program to Calculate the Sum of Natural Numbers
To compute the sum of natural numbers from 1 to n (entered by the user), loops can be used. You will learn how to use for loop and while loop to solve this problem.

To understand this example, you should have the knowledge of following C programming topics:
- C Programming for Loop
- C Programming while and do...while Loop
The positive numbers 1, 2, 3... are known as natural numbers. The programs below takes a positive integer (let say n) as an input from the user and calculates the sum up to n.
Example #1: Sum of Natural Numbers Using for Loop
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=1; i <= n; ++i)
{
sum += i; // sum = sum+i;
}
printf("Sum = %d",sum);
return 0;
}
The above program takes the input from the user and stores in variable n. Then, for loop is used to calculate the sum up to the given number.
Example #2: Sum of Natural Numbers Using while Loop
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
i = 1;
while ( i <=n )
{
sum += i;
++i;
}
printf("Sum = %d",sum);
return 0;
}
Output
Enter a positive integer: 100 Sum = 5050
In both programs, the loop is iterated n number of times. And, in each iteration, the value of i is added to sum and i is incremented by 1.
Though both programs are technically correct, it is better to use for loop in this case. It's because the number of iteration is known.
The above programs doesn't work properly if the user enters a negative integer. Here's a little modification of the above program to take input from the user until positive integer is entered.
Example #3: Program to Read Input Until User Enters a Positive Integer
#include <stdio.h>
int main()
{
int n, i, sum = 0;
do {
printf("Enter a positive integer: ");
scanf("%d",&n);
}
while (n <= 0);
for(i=1; i <= n; ++i)
{
sum += i; // sum = sum+i;
}
printf("Sum = %d",sum);
return 0;
}
Visit this page to learn how to find the sum of natural number using recursion.
C Program to Find the Sum of Natural Numbers using Recursion
Example to find the sum of natural numbers by using a recursive function.
To understand this example, you should have the knowledge of following C programming topics:
- C Programming User-defined functions
- C Programming Recursion
The positive numbers 1, 2, 3... are known as natural numbers. The program below takes a positive integer from the user and calculates the sum up to the given number.
You can find the sum of natural numbers using loop as well. However, you will learn to solve this problem using recursion here.
Example #4: Sum of Natural Numbers Using Recursion
#include <stdio.h>
int addNumbers(int n);
int main()
{
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d",addNumbers(num));
return 0;
}
int addNumbers(int n)
{
if(n != 0)
return n + addNumbers(n-1);
else
return n;
}
Output
Enter a positive integer: 20 Sum = 210
Suppose the user entered 20.
Initially, the
addNumbers()
is called from the main()
function with 20 passed as an argument.
The number (20) is added to the result of
addNumbers(19)
.
In the next function call from
addNumbers()
to addNumbers()
, 19 is passed which is added to the result of addNumbers(18)
. This process continues until n is equal to 0.
When n is equal to 0, there is no recursive call and this returns the sum of integers to the
main()
function.
Comments
Post a Comment