Find the Sum of Numbers in a Given Range in C Program using For loop | Formula | while loop | do-while loop | Function | Recursion
ADVERTISEMENTS
Find the sum of numbers in a given range in c program.
In this article, you will learn how to make the sum of the range of a given input number by using various approaches like - standard formula, for loop, while loop, do-while loop, function, and recursion.
The range of input numbers can be negative or positive.
Example-1
Start Range: 1
End Range: 5
Total Sum: 15
Range
End Range: 5
Total Sum: 15
Range
1
to 5
== (1 + 2 + 3 + 4 + 5)
Example-2
Start Range: -5
End Range: 8
Total Sum: 21
Range
End Range: 8
Total Sum: 21
Range
-5
to 8
== (-5 -4 -3 -2 -1 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)
You should know about the following topics in C programming to understand this program:
- For loop in C
- While loop in C
- Do-while loop in C
- C Function
- C Recursion
1 - Sum of Numbers in a Given Range in C using For loop
// Sum of Numbers in a Given Range in C using For loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int range_start, range_end, sum = 0;
// Step-1 Take input from the User
printf("Start Range: ");
scanf("%d", &range_start);
printf("End Range: ");
scanf("%d", &range_end);
// Step-2 Use a loop from the start range to the end range to get the values & make the sum of each value
for (int i = range_start; i <= range_end; i++)
{
sum += i;
}
// Step-3 Final output of the program
printf("Total Sum: %d", sum);
return 0;
}
Output
Start Range: 1
End Range: 5
Total Sum: 15
2 - Sum of Numbers in a Given Range in C using Standard Formula
// Sum of Numbers in a Given Range in C using Standard Formula
#include <stdio.h>
// It's the main function of the program
int main()
{
int range_start, range_end;
// Step-1 Take input from the User
printf("Start Range: ");
scanf("%d", &range_start);
printf("End Range: ");
scanf("%d", &range_end);
// Step-2 Use the standard formula to make sum of the given range
int sum = range_end * (range_end + 1) / 2 - range_start * (range_start + 1) / 2 + range_start;
// Step-3 Final output of the program
printf("Total Sum: %d", sum);
return 0;
}
Output
Start Range: -5
End Range: 8
Total Sum: 21
3 - Sum of Numbers in a Given Range in C using While loop
// Sum of Numbers in a Given Range in C using While loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int range_start, range_end, sum = 0;
// Step-1 Take input from the User
printf("Start Range: ");
scanf("%d", &range_start);
printf("End Range: ");
scanf("%d", &range_end);
// Step-2 Use a loop from the start range to the end range to get the values & make the sum of each value
while (range_start <= range_end)
{
sum += range_start;
range_start++;
}
// Step-3 Final output of the program
printf("Total Sum: %d", sum);
return 0;
}
Output
Start Range: 10
End Range: 15
Total Sum: 75
4 - Sum of Numbers in a Given Range in C using Do-while loop
// Sum of Numbers in a Given Range in C using Do-while loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int range_start, range_end, sum = 0;
// Step-1 Take input from the User
printf("Start Range: ");
scanf("%d", &range_start);
printf("End Range: ");
scanf("%d", &range_end);
// Step-2 Use a loop from the start range to the end range to get the values & make the sum of each value
do
{
sum += range_start;
range_start++;
} while (range_start <= range_end);
// Step-3 Final output of the program
printf("Total Sum: %d", sum);
return 0;
}
5 - Sum of Numbers in a Given Range in C using Function
// Sum of Numbers in a Given Range in C using Function
#include <stdio.h>
// Function to make sum of given range number
int sum_of_range(int range_start, int range_end)
{
int sum = 0;
// Use a loop from the start range to the end range to get the values & make the sum of each value
while (range_start <= range_end)
{
sum += range_start;
range_start++;
}
return sum;
}
// It's the main function of the program
int main()
{
int range_start, range_end, sum = 0;
// Step-1 Take input from the User
printf("Start Range: ");
scanf("%d", &range_start);
printf("End Range: ");
scanf("%d", &range_end);
// Step-3 Call the sum_of_range() function to get the sum of the input range number
printf("Total Sum: %d", sum_of_range(range_start, range_end));
return 0;
}
Output
Start Range: -2
End Range: 6
Total Sum: 18
6 - Sum of Numbers in a Given Range in C using Recursion
// Sum of Numbers in a Given Range in C using Recursion
#include <stdio.h>
// Function to make the sum of given range number
int rec_sum_of_range(int range_start, int range_end, int sum)
{
sum += range_start;
range_start++;
if (range_start < range_end)
rec_sum_of_range(range_start, range_end, sum);
else
return sum + range_start;
}
// It's the main function of the program
int main()
{
int range_start, range_end, sum = 0;
// Step-1 Take input from the User
printf("Start Range: ");
scanf("%d", &range_start);
printf("End Range: ");
scanf("%d", &range_end);
// Step-3 Call the recursive function rec_sum_of_range() to get the sum of the input range number
printf("Total Sum: %d", rec_sum_of_range(range_start, range_end, sum));
return 0;
}