C Online Compiler
Example: Sum of Numbers in a Given Range in C using Recursion
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// 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; }
-2 6
Output
Clear
ADVERTISEMENTS