Write a function that prints odd numbers in a range of integers using recursion.
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
void printOddNaturalNumbers(int lowerLimit, int upperLimit)
{
if(lowerLimit <= upperLimit)
{
if(lowerLimit%2)
printOddNaturalNumbers(lowerLimit + 1, upperLimit);
else
{
printf("%d ", lowerLimit);
printOddNaturalNumbers(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("Odd Natural numbers from %d to %d are the following: ", lowerLimit, upperLimit);
printOddNaturalNumbers(lowerLimit, upperLimit);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Sorting using heap-sort algorithm