Print even and odd numbers in given range using recursion

Here given code implementation process.

// C program for 
// Print even and odd numbers in given range using recursion
#include <stdio.h>

// Recursivaly print given number
void printValue(int num, int last)
{
	if (num <= last)
	{
		// Display number
		printf(" %d", num);
		// Recursively call
		printValue(num + 2, last);
	}
}
void printEvenOdd(int first, int last)
{
	if (first > last)
	{
		// Range are invalid
		return;
	}
	// Display the value of given range
	printf("\n Given Range (%d-%d)", first, last);
	// Check that given range first value is even or odd
	if (first % 2 == 0)
	{
		printf("\n Even Number :");
		// Print all even number
		printValue(first, last);
		printf("\n Odd Number :");
		// Print all odd number
		printValue(first + 1, last);
	}
	else
	{
		printf("\n Even Number :");
		// Print all even number
		printValue(first + 1, last);
		printf("\n Odd Number :");
		// Print all odd number
		printValue(first, last);
	}
}
int main(int argc, char const *argv[])
{
	// Test Case
	printEvenOdd(1, 10);
	printEvenOdd(53, 74);
	return 0;
}

input

 Given Range (1-10)
 Even Number : 2 4 6 8 10
 Odd Number : 1 3 5 7 9
 Given Range (53-74)
 Even Number : 54 56 58 60 62 64 66 68 70 72 74
 Odd Number : 53 55 57 59 61 63 65 67 69 71 73
// Java program for
// Print even and odd numbers in given range using recursion
public class Series
{
	// Recursivaly print given number
	public void printValue(int num, int last)
	{
		if (num <= last)
		{
			// Display number
			System.out.print(" " + num);
			// Recursively call
			printValue(num + 2, last);
		}
	}
	public void printEvenOdd(int first, int last)
	{
		if (first > last)
		{
			// Range are invalid
			return;
		}
		// Display the value of given range
		System.out.print("\n Given Range (" + first + "-" + last + ")");
		// Check that given range first value is even or odd
		if (first % 2 == 0)
		{
			System.out.print("\n Even Number :");
			// Print all even number
			printValue(first, last);
			System.out.print("\n Odd Number :");
			// Print all odd number
			printValue(first + 1, last);
		}
		else
		{
			System.out.print("\n Even Number :");
			// Print all even number
			printValue(first + 1, last);
			System.out.print("\n Odd Number :");
			// Print all odd number
			printValue(first, last);
		}
	}
	public static void main(String[] args)
	{
		Series task = new Series();
		// Test Case
		task.printEvenOdd(1, 10);
		task.printEvenOdd(53, 74);
	}
}

input

 Given Range (1-10)
 Even Number : 2 4 6 8 10
 Odd Number : 1 3 5 7 9
 Given Range (53-74)
 Even Number : 54 56 58 60 62 64 66 68 70 72 74
 Odd Number : 53 55 57 59 61 63 65 67 69 71 73
// Include header file
#include <iostream>
using namespace std;

// C++ program for
// Print even and odd numbers in given range using recursion
class Series
{
	public:
		// Recursivaly print given number
		void printValue(int num, int last)
		{
			if (num <= last)
			{
				// Display number
				cout << " " << num;
				// Recursively call
				this->printValue(num + 2, last);
			}
		}
	void printEvenOdd(int first, int last)
	{
		if (first > last)
		{
			// Range are invalid
			return;
		}
		// Display the value of given range
		cout << "\n Given Range (" << first << "-" << last << ")";
		// Check that given range first value is even or odd
		if (first % 2 == 0)
		{
			cout << "\n Even Number :";
			// Print all even number
			this->printValue(first, last);
			cout << "\n Odd Number :";
			// Print all odd number
			this->printValue(first + 1, last);
		}
		else
		{
			cout << "\n Even Number :";
			// Print all even number
			this->printValue(first + 1, last);
			cout << "\n Odd Number :";
			// Print all odd number
			this->printValue(first, last);
		}
	}
};
int main()
{
	Series *task = new Series();
	// Test Case
	task->printEvenOdd(1, 10);
	task->printEvenOdd(53, 74);
	return 0;
}

input

 Given Range (1-10)
 Even Number : 2 4 6 8 10
 Odd Number : 1 3 5 7 9
 Given Range (53-74)
 Even Number : 54 56 58 60 62 64 66 68 70 72 74
 Odd Number : 53 55 57 59 61 63 65 67 69 71 73
// Include namespace system
using System;
// Csharp program for
// Print even and odd numbers in given range using recursion
public class Series
{
	// Recursivaly print given number
	public void printValue(int num, int last)
	{
		if (num <= last)
		{
			// Display number
			Console.Write(" " + num);
			// Recursively call
			this.printValue(num + 2, last);
		}
	}
	public void printEvenOdd(int first, int last)
	{
		if (first > last)
		{
			// Range are invalid
			return;
		}
		// Display the value of given range
		Console.Write("\n Given Range (" + first + "-" + last + ")");
		// Check that given range first value is even or odd
		if (first % 2 == 0)
		{
			Console.Write("\n Even Number :");
			// Print all even number
			this.printValue(first, last);
			Console.Write("\n Odd Number :");
			// Print all odd number
			this.printValue(first + 1, last);
		}
		else
		{
			Console.Write("\n Even Number :");
			// Print all even number
			this.printValue(first + 1, last);
			Console.Write("\n Odd Number :");
			// Print all odd number
			this.printValue(first, last);
		}
	}
	public static void Main(String[] args)
	{
		Series task = new Series();
		// Test Case
		task.printEvenOdd(1, 10);
		task.printEvenOdd(53, 74);
	}
}

input

 Given Range (1-10)
 Even Number : 2 4 6 8 10
 Odd Number : 1 3 5 7 9
 Given Range (53-74)
 Even Number : 54 56 58 60 62 64 66 68 70 72 74
 Odd Number : 53 55 57 59 61 63 65 67 69 71 73
<?php
// Php program for
// Print even and odd numbers in given range using recursion
class Series
{
	// Recursivaly print given number
	public	function printValue($num, $last)
	{
		if ($num <= $last)
		{
			// Display number
			echo(" ".$num);
			// Recursively call
			$this->printValue($num + 2, $last);
		}
	}
	public	function printEvenOdd($first, $last)
	{
		if ($first > $last)
		{
			// Range are invalid
			return;
		}
		// Display the value of given range
		echo("\n Given Range (".$first.
			"-".$last.
			")");
		// Check that given range first value is even or odd
		if ($first % 2 == 0)
		{
			echo("\n Even Number :");
			// Print all even number
			$this->printValue($first, $last);
			echo("\n Odd Number :");
			// Print all odd number
			$this->printValue($first + 1, $last);
		}
		else
		{
			echo("\n Even Number :");
			// Print all even number
			$this->printValue($first + 1, $last);
			echo("\n Odd Number :");
			// Print all odd number
			$this->printValue($first, $last);
		}
	}
}

function main()
{
	$task = new Series();
	// Test Case
	$task->printEvenOdd(1, 10);
	$task->printEvenOdd(53, 74);
}
main();

input

 Given Range (1-10)
 Even Number : 2 4 6 8 10
 Odd Number : 1 3 5 7 9
 Given Range (53-74)
 Even Number : 54 56 58 60 62 64 66 68 70 72 74
 Odd Number : 53 55 57 59 61 63 65 67 69 71 73
// Node JS program for
// Print even and odd numbers in given range using recursion
class Series
{
	// Recursivaly print given number
	printValue(num, last)
	{
		if (num <= last)
		{
			// Display number
			process.stdout.write(" " + num);
			// Recursively call
			this.printValue(num + 2, last);
		}
	}
	printEvenOdd(first, last)
	{
		if (first > last)
		{
			// Range are invalid
			return;
		}
		// Display the value of given range
		process.stdout.write("\n Given Range (" + first + "-" + last + ")");
		// Check that given range first value is even or odd
		if (first % 2 == 0)
		{
			process.stdout.write("\n Even Number :");
			// Print all even number
			this.printValue(first, last);
			process.stdout.write("\n Odd Number :");
			// Print all odd number
			this.printValue(first + 1, last);
		}
		else
		{
			process.stdout.write("\n Even Number :");
			// Print all even number
			this.printValue(first + 1, last);
			process.stdout.write("\n Odd Number :");
			// Print all odd number
			this.printValue(first, last);
		}
	}
}

function main()
{
	var task = new Series();
	// Test Case
	task.printEvenOdd(1, 10);
	task.printEvenOdd(53, 74);
}
main();

input

 Given Range (1-10)
 Even Number : 2 4 6 8 10
 Odd Number : 1 3 5 7 9
 Given Range (53-74)
 Even Number : 54 56 58 60 62 64 66 68 70 72 74
 Odd Number : 53 55 57 59 61 63 65 67 69 71 73
#  Python 3 program for
#  Print even and odd numbers in given range using recursion
class Series :
	#  Recursivaly print given number
	def printValue(self, num, last) :
		if (num <= last) :
			#  Display number
			print(" ", num, end = "")
			#  Recursively call
			self.printValue(num + 2, last)
		
	
	def printEvenOdd(self, first, last) :
		if (first > last) :
			#  Range are invalid
			return
		
		#  Display the value of given range
		print("\n Given Range (", first ,"-", last ,")", end = "")
		#  Check that given range first value is even or odd
		if (first % 2 == 0) :
			print("\n Even Number :", end = "")
			#  Print all even number
			self.printValue(first, last)
			print("\n Odd Number :", end = "")
			#  Print all odd number
			self.printValue(first + 1, last)
		else :
			print("\n Even Number :", end = "")
			#  Print all even number
			self.printValue(first + 1, last)
			print("\n Odd Number :", end = "")
			#  Print all odd number
			self.printValue(first, last)
		
	

def main() :
	task = Series()
	#  Test Case
	task.printEvenOdd(1, 10)
	task.printEvenOdd(53, 74)

if __name__ == "__main__": main()

input

 Given Range ( 1 - 10 )
 Even Number :  2  4  6  8  10
 Odd Number :  1  3  5  7  9
 Given Range ( 53 - 74 )
 Even Number :  54  56  58  60  62  64  66  68  70  72  74
 Odd Number :  53  55  57  59  61  63  65  67  69  71  73
#  Ruby program for
#  Print even and odd numbers in given range using recursion
class Series 
	#  Recursivaly print given number
	def printValue(num, last) 
		if (num <= last) 
			#  Display number
			print(" ", num)
			#  Recursively call
			self.printValue(num + 2, last)
		end

	end

	def printEvenOdd(first, last) 
		if (first > last) 
			#  Range are invalid
			return
		end

		#  Display the value of given range
		print("\n Given Range (", first ,"-", last ,")")
		#  Check that given range first value is even or odd
		if (first % 2 == 0) 
			print("\n Even Number :")
			#  Print all even number
			self.printValue(first, last)
			print("\n Odd Number :")
			#  Print all odd number
			self.printValue(first + 1, last)
		else 
			print("\n Even Number :")
			#  Print all even number
			self.printValue(first + 1, last)
			print("\n Odd Number :")
			#  Print all odd number
			self.printValue(first, last)
		end

	end

end

def main() 
	task = Series.new()
	#  Test Case
	task.printEvenOdd(1, 10)
	task.printEvenOdd(53, 74)
end

main()

input

 Given Range (1-10)
 Even Number : 2 4 6 8 10
 Odd Number : 1 3 5 7 9
 Given Range (53-74)
 Even Number : 54 56 58 60 62 64 66 68 70 72 74
 Odd Number : 53 55 57 59 61 63 65 67 69 71 73
// Scala program for
// Print even and odd numbers in given range using recursion
class Series()
{
	// Recursivaly print given number
	def printValue(num: Int, last: Int): Unit = {
		if (num <= last)
		{
			// Display number
			print(" " + num);
			// Recursively call
			printValue(num + 2, last);
		}
	}
	def printEvenOdd(first: Int, last: Int): Unit = {
		if (first > last)
		{
			// Range are invalid
			return;
		}
		// Display the value of given range
		print("\n Given Range (" + first + "-" + last + ")");
		// Check that given range first value is even or odd
		if (first % 2 == 0)
		{
			print("\n Even Number :");
			// Print all even number
			printValue(first, last);
			print("\n Odd Number :");
			// Print all odd number
			printValue(first + 1, last);
		}
		else
		{
			print("\n Even Number :");
			// Print all even number
			printValue(first + 1, last);
			print("\n Odd Number :");
			// Print all odd number
			printValue(first, last);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Series = new Series();
		// Test Case
		task.printEvenOdd(1, 10);
		task.printEvenOdd(53, 74);
	}
}

input

 Given Range (1-10)
 Even Number : 2 4 6 8 10
 Odd Number : 1 3 5 7 9
 Given Range (53-74)
 Even Number : 54 56 58 60 62 64 66 68 70 72 74
 Odd Number : 53 55 57 59 61 63 65 67 69 71 73
// Swift 4 program for
// Print even and odd numbers in given range using recursion
class Series
{
	// Recursivaly print given number
	func printValue(_ num: Int, _ last: Int)
	{
		if (num <= last)
		{
			// Display number
			print(" ", num, terminator: "");
			// Recursively call
			self.printValue(num + 2, last);
		}
	}
	func printEvenOdd(_ first: Int, _ last: Int)
	{
		if (first > last)
		{
			// Range are invalid
			return;
		}
		// Display the value of given range
		print("\n Given Range (", first ,"-", last ,")", terminator: "");
		// Check that given range first value is even or odd
		if (first % 2 == 0)
		{
			print("\n Even Number :", terminator: "");
			// Print all even number
			self.printValue(first, last);
			print("\n Odd Number :", terminator: "");
			// Print all odd number
			self.printValue(first + 1, last);
		}
		else
		{
			print("\n Even Number :", terminator: "");
			// Print all even number
			self.printValue(first + 1, last);
			print("\n Odd Number :", terminator: "");
			// Print all odd number
			self.printValue(first, last);
		}
	}
}
func main()
{
	let task: Series = Series();
	// Test Case
	task.printEvenOdd(1, 10);
	task.printEvenOdd(53, 74);
}
main();

input

 Given Range ( 1 - 10 )
 Even Number :  2  4  6  8  10
 Odd Number :  1  3  5  7  9
 Given Range ( 53 - 74 )
 Even Number :  54  56  58  60  62  64  66  68  70  72  74
 Odd Number :  53  55  57  59  61  63  65  67  69  71  73
// Kotlin program for
// Print even and odd numbers in given range using recursion
class Series
{
	// Recursivaly print given number
	fun printValue(num: Int, last: Int): Unit
	{
		if (num <= last)
		{
			// Display number
			print(" " + num);
			// Recursively call
			this.printValue(num + 2, last);
		}
	}
	fun printEvenOdd(first: Int, last: Int): Unit
	{
		if (first > last)
		{
			// Range are invalid
			return;
		}
		// Display the value of given range
		print("\n Given Range (" + first + "-" + last + ")");
		// Check that given range first value is even or odd
		if (first % 2 == 0)
		{
			print("\n Even Number :");
			// Print all even number
			this.printValue(first, last);
			print("\n Odd Number :");
			// Print all odd number
			this.printValue(first + 1, last);
		}
		else
		{
			print("\n Even Number :");
			// Print all even number
			this.printValue(first + 1, last);
			print("\n Odd Number :");
			// Print all odd number
			this.printValue(first, last);
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Series = Series();
	// Test Case
	task.printEvenOdd(1, 10);
	task.printEvenOdd(53, 74);
}

input

 Given Range (1-10)
 Even Number : 2 4 6 8 10
 Odd Number : 1 3 5 7 9
 Given Range (53-74)
 Even Number : 54 56 58 60 62 64 66 68 70 72 74
 Odd Number : 53 55 57 59 61 63 65 67 69 71 73


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved