# Generate all n digit numbers whose consecutive odd and even digit

Here given code implementation process.

``````/*
C program for
Generate all n digit numbers whose consecutive odd and even 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 )
{
// When index is Even
// Test condition
// ➀ number is Odd
// Condition true then enter
if( i % 2 != 0 && !(index==0 && i == 0))
{
result[index] = i;
findCombination(result,index+1,n);
}
}
else
{
// When index is Odd
// Check number is Even
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
10  12  14  16  18  30  32  34  36  38  50  52  54  56  58  70  72  74  76  78  90  92  94  96  98
Given n : 3
101  103  105  107  109  121  123  125  127  129  141  143  145  147  149  161  163  165  167  169  181  183  185  187  189  301  303  305  307  309  321  323  325  327  329  341  343  345  347  349  361  363  365  367  369  381  383  385  387  389  501  503  505  507  509  521  523  525  527  529  541  543  545  547  549  561  563  565  567  569  581  583  585  587  589  701  703  705  707  709  721  723  725  727  729  741  743  745  747  749  761  763  765  767  769  781  783  785  787  789  901  903  905  907  909  921  923  925  927  929  941  943  945  947  949  961  963  965  967  969  981  983  985  987  989``````
``````// Java Program for
// Generate all n digit numbers whose consecutive odd and even digit
public class OddEvenCombination
{
// 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)
{
// When index is Even
// Test condition
// ➀ number is Odd
// Condition true then enter
if (i % 2 != 0 && !(index == 0 && i == 0))
{
result[index] = i;
findCombination(result, index + 1, n);
}
}
else
{
// When index is Odd
// Check number is Even
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
10 12 14 16 18 30 32 34 36 38 50 52 54 56 58 70 72 74 76 78 90 92 94 96 98
Given n : 3
101 103 105 107 109 121 123 125 127 129 141 143 145 147 149 161 163 165 167 169 181 183 185 187 189 301 303 305 307 309 321 323 325 327 329 341 343 345 347 349 361 363 365 367 369 381 383 385 387 389 501 503 505 507 509 521 523 525 527 529 541 543 545 547 549 561 563 565 567 569 581 583 585 587 589 701 703 705 707 709 721 723 725 727 729 741 743 745 747 749 761 763 765 767 769 781 783 785 787 789 901 903 905 907 909 921 923 925 927 929 941 943 945 947 949 961 963 965 967 969 981 983 985 987 989``````
``````// Include header file
#include <iostream>
using namespace std;
// C++ Program for
// Generate all n digit numbers whose consecutive odd and even digit
class OddEvenCombination
{
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)
{
// When index is Even
// Test condition
// ➀ number is Odd
// Condition true then enter
if (i % 2 != 0 && !(index == 0 && i == 0))
{
result[index] = i;
this->findCombination(result, index + 1, n);
}
}
else
{
// When index is Odd
// Check number is Even
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
10 12 14 16 18 30 32 34 36 38 50 52 54 56 58 70 72 74 76 78 90 92 94 96 98
Given n : 3
101 103 105 107 109 121 123 125 127 129 141 143 145 147 149 161 163 165 167 169 181 183 185 187 189 301 303 305 307 309 321 323 325 327 329 341 343 345 347 349 361 363 365 367 369 381 383 385 387 389 501 503 505 507 509 521 523 525 527 529 541 543 545 547 549 561 563 565 567 569 581 583 585 587 589 701 703 705 707 709 721 723 725 727 729 741 743 745 747 749 761 763 765 767 769 781 783 785 787 789 901 903 905 907 909 921 923 925 927 929 941 943 945 947 949 961 963 965 967 969 981 983 985 987 989``````
``````package main
import "fmt"
// Go Program for
// Generate all n digit numbers whose consecutive odd and even digit
type OddEvenCombination struct {}
func getOddEvenCombination() * OddEvenCombination {
var me *OddEvenCombination = &OddEvenCombination {}
return me
}
// Display result
func(this OddEvenCombination) printSequence(result[] int, k int) {
for i := 0 ; i < k ; i++ {
fmt.Print(result[i])
}
fmt.Print(" ")
}
func(this OddEvenCombination) 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 {
// When index is Even
// Test condition
// ➀ number is Odd
// Condition true then enter
if i % 2 != 0 && !(index == 0 && i == 0) {
result[index] = i
this.findCombination(result, index + 1, n)
}
} else {
// When index is Odd
// Check number is Even
if (i % 2) == 0 {
result[index] = i
this.findCombination(result, index + 1, n)
}
}
}
}
// Handles the request of find combination of given n
func(this OddEvenCombination) 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 * OddEvenCombination = getOddEvenCombination()
// Test
}``````

#### Output

`````` Given n : 2
10 12 14 16 18 30 32 34 36 38 50 52 54 56 58 70 72 74 76 78 90 92 94 96 98
Given n : 3
101 103 105 107 109 121 123 125 127 129 141 143 145 147 149 161 163 165 167 169 181 183 185 187 189 301 303 305 307 309 321 323 325 327 329 341 343 345 347 349 361 363 365 367 369 381 383 385 387 389 501 503 505 507 509 521 523 525 527 529 541 543 545 547 549 561 563 565 567 569 581 583 585 587 589 701 703 705 707 709 721 723 725 727 729 741 743 745 747 749 761 763 765 767 769 781 783 785 787 789 901 903 905 907 909 921 923 925 927 929 941 943 945 947 949 961 963 965 967 969 981 983 985 987 989``````
``````// Include namespace system
using System;
// Csharp Program for
// Generate all n digit numbers whose consecutive odd and even digit
public class OddEvenCombination
{
// 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)
{
// When index is Even
// Test condition
// ➀ number is odd
// Condition true then enter
if (i % 2 != 0 && !(index == 0 && i == 0))
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
else
{
// When index is Odd
// Check number is Even
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
10 12 14 16 18 30 32 34 36 38 50 52 54 56 58 70 72 74 76 78 90 92 94 96 98
Given n : 3
101 103 105 107 109 121 123 125 127 129 141 143 145 147 149 161 163 165 167 169 181 183 185 187 189 301 303 305 307 309 321 323 325 327 329 341 343 345 347 349 361 363 365 367 369 381 383 385 387 389 501 503 505 507 509 521 523 525 527 529 541 543 545 547 549 561 563 565 567 569 581 583 585 587 589 701 703 705 707 709 721 723 725 727 729 741 743 745 747 749 761 763 765 767 769 781 783 785 787 789 901 903 905 907 909 921 923 925 927 929 941 943 945 947 949 961 963 965 967 969 981 983 985 987 989``````
``````<?php
// Php Program for
// Generate all n digit numbers whose consecutive odd and even digit
class OddEvenCombination
{
// 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)
{
// When index is Even
// Test condition
// ➀ number is Odd
// Condition true then enter
if (\$i % 2 != 0 && !(\$index == 0 && \$i == 0))
{
\$result[\$index] = \$i;
\$this->findCombination(\$result, \$index + 1, \$n);
}
}
else
{
// When index is Odd
// Check number is Even
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
10 12 14 16 18 30 32 34 36 38 50 52 54 56 58 70 72 74 76 78 90 92 94 96 98
Given n : 3
101 103 105 107 109 121 123 125 127 129 141 143 145 147 149 161 163 165 167 169 181 183 185 187 189 301 303 305 307 309 321 323 325 327 329 341 343 345 347 349 361 363 365 367 369 381 383 385 387 389 501 503 505 507 509 521 523 525 527 529 541 543 545 547 549 561 563 565 567 569 581 583 585 587 589 701 703 705 707 709 721 723 725 727 729 741 743 745 747 749 761 763 765 767 769 781 783 785 787 789 901 903 905 907 909 921 923 925 927 929 941 943 945 947 949 961 963 965 967 969 981 983 985 987 989``````
``````// Node JS Program for
// Generate all n digit numbers whose consecutive odd and even digit
class OddEvenCombination
{
// 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)
{
// When index is Even
// Test condition
// ➀ number is Odd
// Condition true then enter
if (i % 2 != 0 && !(index == 0 && i == 0))
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
else
{
// When index is Odd
// Check number is Even
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
10 12 14 16 18 30 32 34 36 38 50 52 54 56 58 70 72 74 76 78 90 92 94 96 98
Given n : 3
101 103 105 107 109 121 123 125 127 129 141 143 145 147 149 161 163 165 167 169 181 183 185 187 189 301 303 305 307 309 321 323 325 327 329 341 343 345 347 349 361 363 365 367 369 381 383 385 387 389 501 503 505 507 509 521 523 525 527 529 541 543 545 547 549 561 563 565 567 569 581 583 585 587 589 701 703 705 707 709 721 723 725 727 729 741 743 745 747 749 761 763 765 767 769 781 783 785 787 789 901 903 905 907 909 921 923 925 927 929 941 943 945 947 949 961 963 965 967 969 981 983 985 987 989``````
``````#  Python 3 Program for
#  Generate all n digit numbers whose consecutive odd and even digit
class OddEvenCombination :
#  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) :
#  When index is Even
#  Test condition
#  ➀ number is Odd
#  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 :
#  When index is Odd
#  Check number is Even
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
10 12 14 16 18 30 32 34 36 38 50 52 54 56 58 70 72 74 76 78 90 92 94 96 98
Given n :  3
101 103 105 107 109 121 123 125 127 129 141 143 145 147 149 161 163 165 167 169 181 183 185 187 189 301 303 305 307 309 321 323 325 327 329 341 343 345 347 349 361 363 365 367 369 381 383 385 387 389 501 503 505 507 509 521 523 525 527 529 541 543 545 547 549 561 563 565 567 569 581 583 585 587 589 701 703 705 707 709 721 723 725 727 729 741 743 745 747 749 761 763 765 767 769 781 783 785 787 789 901 903 905 907 909 921 923 925 927 929 941 943 945 947 949 961 963 965 967 969 981 983 985 987 989``````
``````#  Ruby Program for
#  Generate all n digit numbers whose consecutive odd and even digit
class OddEvenCombination
#  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)
#  When index is Even
#  Test condition
#  ➀ number is Odd
#  Condition true then enter
if (i % 2 != 0 && !(index == 0 && i == 0))
result[index] = i
self.findCombination(result, index + 1, n)
end

else

#  When index is Odd
#  Check number is Even
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
10 12 14 16 18 30 32 34 36 38 50 52 54 56 58 70 72 74 76 78 90 92 94 96 98
Given n : 3
101 103 105 107 109 121 123 125 127 129 141 143 145 147 149 161 163 165 167 169 181 183 185 187 189 301 303 305 307 309 321 323 325 327 329 341 343 345 347 349 361 363 365 367 369 381 383 385 387 389 501 503 505 507 509 521 523 525 527 529 541 543 545 547 549 561 563 565 567 569 581 583 585 587 589 701 703 705 707 709 721 723 725 727 729 741 743 745 747 749 761 763 765 767 769 781 783 785 787 789 901 903 905 907 909 921 923 925 927 929 941 943 945 947 949 961 963 965 967 969 981 983 985 987 989 ``````
``````// Scala Program for
// Generate all n digit numbers whose consecutive odd and even digit
class OddEvenCombination()
{
// 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)
{
// When index is Even
// Test condition
// ➀ number is Odd
// Condition true then enter
if (i % 2 != 0 && !(index == 0 && i == 0))
{
result(index) = i;
findCombination(result, index + 1, n);
}
}
else
{
// When index is Odd
// Check number is Even
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: OddEvenCombination = new OddEvenCombination();
// Test
}
}``````

#### Output

`````` Given n : 2
10 12 14 16 18 30 32 34 36 38 50 52 54 56 58 70 72 74 76 78 90 92 94 96 98
Given n : 3
101 103 105 107 109 121 123 125 127 129 141 143 145 147 149 161 163 165 167 169 181 183 185 187 189 301 303 305 307 309 321 323 325 327 329 341 343 345 347 349 361 363 365 367 369 381 383 385 387 389 501 503 505 507 509 521 523 525 527 529 541 543 545 547 549 561 563 565 567 569 581 583 585 587 589 701 703 705 707 709 721 723 725 727 729 741 743 745 747 749 761 763 765 767 769 781 783 785 787 789 901 903 905 907 909 921 923 925 927 929 941 943 945 947 949 961 963 965 967 969 981 983 985 987 989``````
``````// Swift 4 Program for
// Generate all n digit numbers whose consecutive odd and even digit
class OddEvenCombination
{
// 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)
{
// When index is Even
// Test condition
// ➀ number is Odd
// Condition true then enter
if (i % 2  != 0 && !(index == 0 && i == 0))
{
result[index] = i;
self.findCombination(&result, index + 1, n);
}
}
else
{
// When index is Odd
// Check number is Even
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
10 12 14 16 18 30 32 34 36 38 50 52 54 56 58 70 72 74 76 78 90 92 94 96 98
Given n :  3
101 103 105 107 109 121 123 125 127 129 141 143 145 147 149 161 163 165 167 169 181 183 185 187 189 301 303 305 307 309 321 323 325 327 329 341 343 345 347 349 361 363 365 367 369 381 383 385 387 389 501 503 505 507 509 521 523 525 527 529 541 543 545 547 549 561 563 565 567 569 581 583 585 587 589 701 703 705 707 709 721 723 725 727 729 741 743 745 747 749 761 763 765 767 769 781 783 785 787 789 901 903 905 907 909 921 923 925 927 929 941 943 945 947 949 961 963 965 967 969 981 983 985 987 989``````
``````// Kotlin Program for
// Generate all n digit numbers whose consecutive odd and even digit
class OddEvenCombination
{
// 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)
{
// When index is Even
// Test condition
// ➀ number is Odd
// Condition true then enter
if (i % 2 != 0 && !(index == 0 && i == 0))
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
else
{
// When index is Odd
// Check number is Even
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
var result: Array < Int > = Array(n)
{
0
};
// Test
this.findCombination(result, 0, n);
}
}
fun main(args: Array < String > ): Unit
{
// Test
}``````

#### Output

`````` Given n : 2
10 12 14 16 18 30 32 34 36 38 50 52 54 56 58 70 72 74 76 78 90 92 94 96 98
Given n : 3
101 103 105 107 109 121 123 125 127 129 141 143 145 147 149 161 163 165 167 169 181 183 185 187 189 301 303 305 307 309 321 323 325 327 329 341 343 345 347 349 361 363 365 367 369 381 383 385 387 389 501 503 505 507 509 521 523 525 527 529 541 543 545 547 549 561 563 565 567 569 581 583 585 587 589 701 703 705 707 709 721 723 725 727 729 741 743 745 747 749 761 763 765 767 769 781 783 785 787 789 901 903 905 907 909 921 923 925 927 929 941 943 945 947 949 961 963 965 967 969 981 983 985 987 989``````

## 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.