Generate all n digit numbers whose consecutive even and odd digit

Here given code implementation process.

``````/*
C program for
Generate all n digit numbers whose consecutive even and odd digit
*/
#include <stdio.h>

// Display result
void printSequence(int result[], int k)
{
for (int i = 0; i < k; ++i)
{
printf("%d", result[i]);
}
printf("  ");
}

void findCombination(int result[], int index, int n)
{
if (index == n )
{
// Display calculated result
printSequence(result, index);
}
if(index >= n)
{
return;
}
for (int i = 0; i <= 9; ++i)
{
if(index%2==0 )
{
// Test condition
// ➀ number is even
// Condition true then enter
if( i % 2 == 0 && !(index==0 && i == 0))
{
result[index] = i;
findCombination(result,index+1,n);
}
}
else
{
// Check number is Odd
if( (i % 2) != 0 )
{
result[index] = i;
findCombination(result,index+1,n);
}
}
}
}
// Handles the request of find combination of given n
void combination(int n)
{
if (n <= 0)
{
return;
}
printf("\n Given n : %d  \n ", n);
// Use to collect result
int result[n];
// Test
findCombination(result, 0, n);
}
int main(int argc, char const *argv[])
{
// Test
combination(2);
combination(3);
return 0;
}``````

Output

`````` Given n : 2
21  23  25  27  29  41  43  45  47  49  61  63  65  67  69  81  83  85  87  89
Given n : 3
210  212  214  216  218  230  232  234  236  238  250  252  254  256  258  270  272  274  276  278  290  292  294  296  298  410  412  414  416  418  430  432  434  436  438  450  452  454  456  458  470  472  474  476  478  490  492  494  496  498  610  612  614  616  618  630  632  634  636  638  650  652  654  656  658  670  672  674  676  678  690  692  694  696  698  810  812  814  816  818  830  832  834  836  838  850  852  854  856  858  870  872  874  876  878  890  892  894  896  898``````
``````// Java Program for
// Generate all n digit numbers whose consecutive even and odd digit

public class EvenOddCombination
{

// Display result
public void printSequence(int[] result, int k)
{
for (int i = 0; i < k; ++i)
{
System.out.print(result[i]);
}
System.out.print(" ");
}
public void findCombination(int[] result, int index, int n)
{
if (index == n)
{
// Display calculated result
printSequence(result, index);
}
if (index >= n)
{
return;
}
for (int i = 0; i <= 9; ++i)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result[index] = i;
findCombination(result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2) != 0)
{
result[index] = i;
findCombination(result, index + 1, n);
}
}
}
}
// Handles the request of find combination of given n
public void combination(int n)
{
if (n <= 0)
{
return;
}
System.out.println("\n Given n : " + n );
// Use to collect result
int[] result = new int[n];
// Test
findCombination(result, 0, n);
}
public static void main(String[] args)
{

// Test
}
}``````

Output

`````` Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898``````
``````// Include header file
#include <iostream>
using namespace std;
// C++ Program for
// Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination
{
public:
// Display result
void printSequence(int result[], int k)
{
for (int i = 0; i < k; ++i)
{
cout << result[i];
}
cout << " ";
}
void findCombination(int result[], int index, int n)
{
if (index == n)
{
// Display calculated result
this->printSequence(result, index);
}
if (index >= n)
{
return;
}
for (int i = 0; i <= 9; ++i)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result[index] = i;
this->findCombination(result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2) != 0)
{
result[index] = i;
this->findCombination(result, index + 1, n);
}
}
}
}
// Handles the request of find combination of given n
void combination(int n)
{
if (n <= 0)
{
return;
}
cout << "\n Given n : " << n << endl;
// Use to collect result
int result[n];
// Test
this->findCombination(result, 0, n);
}
};
int main()
{
// Test
return 0;
}``````

Output

`````` Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898``````
``````package main
import "fmt"
// Go Program for
// Generate all n digit numbers whose consecutive even and odd digit
type EvenOddCombination struct {}
func getEvenOddCombination() * EvenOddCombination {
var me *EvenOddCombination = &EvenOddCombination {}
return me
}
// Display result
func(this EvenOddCombination) printSequence(result[] int, k int) {
for i := 0 ; i < k ; i++ {
fmt.Print(result[i])
}
fmt.Print(" ")
}
func(this EvenOddCombination) findCombination(result[] int,
index int, n int) {
if index == n {
// Display calculated result
this.printSequence(result, index)
}
if index >= n {
return
}
for i := 0 ; i <= 9 ; i++ {
if index % 2 == 0 {
// Test condition
// ➀ number is even
// Condition true then enter
if i % 2 == 0 && !(index == 0 && i == 0) {
result[index] = i
this.findCombination(result, index + 1, n)
}
} else {
// Check number is Odd
if (i % 2) != 0 {
result[index] = i
this.findCombination(result, index + 1, n)
}
}
}
}
// Handles the request of find combination of given n
func(this EvenOddCombination) combination(n int) {
if n <= 0 {
return
}
fmt.Println("\n Given n : ", n)
// Use to collect result
var result = make([] int, n)
// Test
this.findCombination(result, 0, n)
}
func main() {
var task * EvenOddCombination = getEvenOddCombination()
// Test
}``````

Output

`````` Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898``````
``````// Include namespace system
using System;
// Csharp Program for
// Generate all n digit numbers whose consecutive even and odd digit
public class EvenOddCombination
{
// Display result
public void printSequence(int[] result, int k)
{
for (int i = 0; i < k; ++i)
{
Console.Write(result[i]);
}
Console.Write(" ");
}
public void findCombination(int[] result, int index, int n)
{
if (index == n)
{
// Display calculated result
this.printSequence(result, index);
}
if (index >= n)
{
return;
}
for (int i = 0; i <= 9; ++i)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2) != 0)
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
}
}
// Handles the request of find combination of given n
public void combination(int n)
{
if (n <= 0)
{
return;
}
Console.WriteLine("\n Given n : " + n);
// Use to collect result
int[] result = new int[n];
// Test
this.findCombination(result, 0, n);
}
public static void Main(String[] args)
{
// Test
}
}``````

Output

`````` Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898``````
``````<?php
// Php Program for
// Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination
{
// Display result
public	function printSequence(\$result, \$k)
{
for (\$i = 0; \$i < \$k; ++\$i)
{
echo(\$result[\$i]);
}
echo(" ");
}
public	function findCombination(\$result, \$index, \$n)
{
if (\$index == \$n)
{
// Display calculated result
\$this->printSequence(\$result, \$index);
}
if (\$index >= \$n)
{
return;
}
for (\$i = 0; \$i <= 9; ++\$i)
{
if (\$index % 2 == 0)
{
// Test condition
// ➀ number is even
// Condition true then enter
if (\$i % 2 == 0 && !(\$index == 0 && \$i == 0))
{
\$result[\$index] = \$i;
\$this->findCombination(\$result, \$index + 1, \$n);
}
}
else
{
// Check number is Odd
if ((\$i % 2) != 0)
{
\$result[\$index] = \$i;
\$this->findCombination(\$result, \$index + 1, \$n);
}
}
}
}
// Handles the request of find combination of given n
public	function combination(\$n)
{
if (\$n <= 0)
{
return;
}
echo("\n Given n : ".\$n.
"\n");
// Use to collect result
\$result = array_fill(0, \$n, 0);
// Test
\$this->findCombination(\$result, 0, \$n);
}
}

function main()
{
// Test
}
main();``````

Output

`````` Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898``````
``````// Node JS Program for
// Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination
{
// Display result
printSequence(result, k)
{
for (var i = 0; i < k; ++i)
{
process.stdout.write("" + result[i]);
}
process.stdout.write(" ");
}
findCombination(result, index, n)
{
if (index == n)
{
// Display calculated result
this.printSequence(result, index);
}
if (index >= n)
{
return;
}
for (var i = 0; i <= 9; ++i)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2) != 0)
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
}
}
// Handles the request of find combination of given n
combination(n)
{
if (n <= 0)
{
return;
}
console.log("\n Given n : " + n);
// Use to collect result
var result = Array(n).fill(0);
// Test
this.findCombination(result, 0, n);
}
}

function main()
{
// Test
}
main();``````

Output

`````` Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898``````
``````#  Python 3 Program for
#  Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination :
#  Display result
def printSequence(self, result, k) :
i = 0
while (i < k) :
print(result[i], end = "")
i += 1

print(" ", end = "")

def findCombination(self, result, index, n) :
if (index == n) :
#  Display calculated result
self.printSequence(result, index)

if (index >= n) :
return

i = 0
while (i <= 9) :
if (index % 2 == 0) :
#  Test condition
#  ➀ number is even
#  Condition true then enter
if (i % 2 == 0 and not(index == 0 and i == 0)) :
result[index] = i
self.findCombination(result, index + 1, n)

else :
#  Check number is Odd
if ((i % 2) != 0) :
result[index] = i
self.findCombination(result, index + 1, n)

i += 1

#  Handles the request of find combination of given n
def combination(self, n) :
if (n <= 0) :
return

print("\n Given n : ", n)
#  Use to collect result
result = [0] * (n)
#  Test
self.findCombination(result, 0, n)

def main() :
#  Test

if __name__ == "__main__": main()``````

Output

`````` Given n :  2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n :  3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898``````
``````#  Ruby Program for
#  Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination
#  Display result
def printSequence(result, k)
i = 0
while (i < k)
print(result[i])
i += 1
end

print(" ")
end

def findCombination(result, index, n)
if (index == n)
#  Display calculated result
self.printSequence(result, index)
end

if (index >= n)
return
end

i = 0
while (i <= 9)
if (index % 2 == 0)
#  Test condition
#  ➀ number is even
#  Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
result[index] = i
self.findCombination(result, index + 1, n)
end

else

#  Check number is Odd
if ((i % 2) != 0)
result[index] = i
self.findCombination(result, index + 1, n)
end

end

i += 1
end

end

#  Handles the request of find combination of given n
def combination(n)
if (n <= 0)
return
end

print("\n Given n : ", n, "\n")
#  Use to collect result
result = Array.new(n) {0}
#  Test
self.findCombination(result, 0, n)
end

end

def main()
#  Test
end

main()``````

Output

`````` Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898 ``````
``````// Scala Program for
// Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination()
{
// Display result
def printSequence(result: Array[Int], k: Int): Unit = {
var i: Int = 0;
while (i < k)
{
print(result(i));
i += 1;
}
print(" ");
}
def findCombination(result: Array[Int], index: Int, n: Int): Unit = {
if (index == n)
{
// Display calculated result
printSequence(result, index);
}
if (index >= n)
{
return;
}
var i: Int = 0;
while (i <= 9)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result(index) = i;
findCombination(result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2) != 0)
{
result(index) = i;
findCombination(result, index + 1, n);
}
}
i += 1;
}
}
// Handles the request of find combination of given n
def combination(n: Int): Unit = {
if (n <= 0)
{
return;
}
println("\n Given n : " + n);
// Use to collect result
var result: Array[Int] = Array.fill[Int](n)(0);
// Test
findCombination(result, 0, n);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: EvenOddCombination = new EvenOddCombination();
// Test
}
}``````

Output

`````` Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898``````
``````// Swift 4 Program for
// Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination
{
// Display result
func printSequence(_ result: [Int], _ k: Int)
{
var i: Int = 0;
while (i < k)
{
print(result[i], terminator: "");
i += 1;
}
print(" ", terminator: "");
}
func findCombination(_ result: inout[Int], _ index: Int, _ n: Int)
{
if (index == n)
{
// Display calculated result
self.printSequence(result, index);
}
if (index >= n)
{
return;
}
var i: Int = 0;
while (i <= 9)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result[index] = i;
self.findCombination(&result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2)  != 0)
{
result[index] = i;
self.findCombination(&result, index + 1, n);
}
}
i += 1;
}
}
// Handles the request of find combination of given n
func combination(_ n: Int)
{
if (n <= 0)
{
return;
}
print("\n Given n : ", n);
// Use to collect result
var result: [Int] = Array(repeating: 0, count: n);
// Test
self.findCombination(&result, 0, n);
}
}
func main()
{
// Test
}
main();``````

Output

`````` Given n :  2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n :  3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898``````
``````// Kotlin Program for
// Generate all n digit numbers whose consecutive even and odd digit
class EvenOddCombination
{
// Display result
fun printSequence(result: Array < Int > , k: Int): Unit
{
var i: Int = 0;
while (i < k)
{
print(result[i]);
i += 1;
}
print(" ");
}
fun findCombination(result: Array < Int > , index: Int, n: Int): Unit
{
if (index == n)
{
// Display calculated result
this.printSequence(result, index);
}
if (index >= n)
{
return;
}
var i: Int = 0;
while (i <= 9)
{
if (index % 2 == 0)
{
// Test condition
// ➀ number is even
// Condition true then enter
if (i % 2 == 0 && !(index == 0 && i == 0))
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
else
{
// Check number is Odd
if ((i % 2) != 0)
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
i += 1;
}
}
// Handles the request of find combination of given n
fun combination(n: Int): Unit
{
if (n <= 0)
{
return;
}
println("\n Given n : " + n);
// Use to collect result
val result: Array < Int > = Array(n)
{
0
};
// Test
this.findCombination(result, 0, n);
}
}
fun main(args: Array < String > ): Unit
{
// Test
}``````

Output

`````` Given n : 2
21 23 25 27 29 41 43 45 47 49 61 63 65 67 69 81 83 85 87 89
Given n : 3
210 212 214 216 218 230 232 234 236 238 250 252 254 256 258 270 272 274 276 278 290 292 294 296 298 410 412 414 416 418 430 432 434 436 438 450 452 454 456 458 470 472 474 476 478 490 492 494 496 498 610 612 614 616 618 630 632 634 636 638 650 652 654 656 658 670 672 674 676 678 690 692 694 696 698 810 812 814 816 818 830 832 834 836 838 850 852 854 856 858 870 872 874 876 878 890 892 894 896 898``````

Comment

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.