Write a function that prints even numbers in a range of integers using recursion.
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
void printEvenNaturalNumbers(int lowerLimit, int upperLimit)
{
if(lowerLimit <= upperLimit)
{
if(lowerLimit%2)
printEvenNaturalNumbers(lowerLimit + 1, upperLimit);
else
{
printf("%d ", lowerLimit);
printEvenNaturalNumbers(lowerLimit + 2, upperLimit);
}
}
}
void main()
{
int lowerLimit, upperLimit;
printf("Enter the lower limit: ");
scanf("%d", &lowerLimit);
printf("Enter the upper limit: ");
scanf("%d", &upperLimit);
printf("Even Natural numbers from %d to %d are the following: ", lowerLimit, upperLimit);
printEvenNaturalNumbers(lowerLimit, upperLimit);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Add into a table based on hash coalesced with separated zones