C Program to find sum of numbers in a given interval using recursion

Category: C ProgramTags: cprogram function

C Program to find the sum of numbers in a given interval using recursion

C Program to find the sum of numbers in a given interval using recursion

#include <stdio.h>

int sumOfNumbers(int, int);

void main()
{
    int start, end;

    printf("Enter the number to start: ");
    scanf("%d", &start);
    printf("Enter the number to end: ");
    scanf("%d", &end);

    printf("Sum: %d", sumOfNumbers(start, end));
}

int sumOfNumbers(int start, int end)
{
    if (start >= end)
        return start;
    else
        return start + sumOfNumbers(start + 1, end);
}

Output

Output

Enter the number to start: 1
Enter the number to end: 20
Sum: 210