c code recursive function to print numbers between two numbers

#include<stdio.h>

void printNumbers(int start, int end) {
    if (start > end) {
        return;
    }

    printf("%d ", start);
    printNumbers(start + 1, end);
}

int main() {
    int start, end;

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

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

    printf("Numbers between %d and %d are: ", start, end);
    printNumbers(start, end);

    return 0;
}

The above code is a C program that uses a recursive function to print numbers between two given numbers. Here is a step-by-step explanation of how the code works:

  1. The code starts with the inclusion of the standard input/output library stdio.h.

  2. The printNumbers function is defined, which takes two integer parameters: start and end. This function is responsible for printing the numbers between start and end recursively.

  3. Inside the printNumbers function, there is a base case check. If start is greater than end, it means we have reached the end of the range, so the function returns without doing anything.

  4. If the base case check fails, the code proceeds to the next step, which is printing the current start value using printf. The %d format specifier is used to print an integer.

  5. After printing the current start value, the printNumbers function is called recursively with start + 1 as the new start value. This will print the next number in the range.

  6. The main function is defined, which is the entry point of the program. It declares two integer variables start and end.

  7. The user is prompted to enter the starting number using printf and scanf functions. The "%d" format specifier is used to read an integer value from the user and store it in the start variable.

  8. Similarly, the user is prompted to enter the ending number, which is also stored in the end variable.

  9. After getting the input from the user, the program prints a message indicating the range of numbers being printed.

  10. Finally, the printNumbers function is called with the start and end values provided by the user, and the numbers between the given range are printed recursively.

  11. The main function returns 0, indicating successful execution of the program.