Problem statement:- Program to Print odd numbers in a given range using recursion.
Data requirement:-
Input Data:- num2
Output Data:-num1
Output Data:-num1
Program in C
Here is the source code of the C Program to Print odd numbers in a given range using recursion.
Code:
#include<stdio.h>
int odd(int num1, int num2)
{
if(num1>num2)
return;
printf("%d ",num1);
return odd(num1+2,num2);
}
int main()
{
int num1=1,num2;
printf("Enter your Limit:");
scanf("%d",&num2);
printf("All odd number given range are:");
odd(num1,num2);
}
Enter your Limit:15
All odd number given range are:1 3 5 7 9 11 13 15
Program in C++
Here is the source code of the C++ Program to Print odd numbers in a given range using recursion.
Code:
#include<iostream>
using namespace std;
int odd(int num1, int num2)
{
if(num1>num2)
return 0;
cout<<num1<<" ";
return odd(num1+2,num2);
}
int main()
{
int num1=1,num2;
cout<<"Enter your Limit:";
cin>>num2;
cout<<"All odd number given range are:";
odd(num1,num2);
}
Enter your Limit:21
All odd number given range are:1 3 5 7 9 11 13 15 17 19 21
Program in Java
Here is the source code of the Java Program to Print odd numbers in a given range using recursion.
Code:
import java.util.Scanner;
public class FindOddNumber {
static int odd(int num1, int num2)
{
if(num1>num2)
return 0;
System.out.printf(num1+" ");
return odd(num1+2,num2);
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num1=1,num2;
System.out.print("Enter your Limit:");
num2=cs.nextInt();
System.out.print("All odd number given range are:");
odd(num1,num2);
cs.close();
}
}
Enter your Limit:9
All odd number given range are:1 3 5 7 9
Program in Python
Here is the source code of the Python program to Print odd numbers in a given range using recursion.
Code:
def odd(num1,num2):
if num1>num2:
return
print(num1,end=" ")
return odd(num1+2,num2)
num1=1
print("Enter your Limit:")
num2=int(input())
print("All odd number given range are:")
odd(num1,num2)
Enter your Limit:
33
All odd number given range are:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33
More:-
0 Comments
Please do not Enter any spam link in the comment box