Write a function that prints a range of integers using recursion.


Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>

void printNaturalNumbers(int lowerLimit, int upperLimit)
{
        if(lowerLimit == upperLimit)
		printf("%d.", lowerLimit);
    	if(lowerLimit < upperLimit)
	{
		printf("%d, ", lowerLimit);
    		printNaturalNumbers(lowerLimit + 1, upperLimit);
	}
}

void main()
{
 	int lowerLimit, upperLimit;
	 
    	printf("Enter the lower limit: ");
    	scanf("%d", &lowerLimit);
    	printf("Enter the upper limit: ");
    	scanf("%d", &upperLimit);

    	printf("Natural numbers from %d to %d are the following: ", lowerLimit, upperLimit);
    	printNaturalNumbers(lowerLimit, upperLimit);
 
	getch();
}


Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Replace all occurrences of a string in an another string