Print all N digit jumping numbers
Here given code implementation process.
/*
C program for
Print all N digit jumping numbers
*/
#include <stdio.h>
// Display result
void printSequence(int result[], int k)
{
for (int i = 0; i < k; ++i)
{
printf("%d", result[i]);
}
printf("\n ");
}
void findCombination(int result[], int index, int n)
{
if (index == n)
{
// Display calculated result
printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
findCombination(result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
findCombination(result, index + 1, n);
}
}
else
{
// This is used to setup first element
for (int i = 1; i <= 9; ++i)
{
result[index] = i;
findCombination(result, index + 1, n);
}
}
}
// Handles the request of find combination of given n
void jumpingNumber(int n)
{
if (n <= 0)
{
return;
}
printf("\n Given n : %d \n ", n);
// Collect result
int result[n];
if (n == 1)
{
printf("\n 0 \n ");
}
// Test
findCombination(result, 0, n);
}
int main(int argc, char
const *argv[])
{
// Test
jumpingNumber(4);
return 0;
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
/*
Java program
Print all N digit jumping numbers
*/
public class JumpingNumber
{
// Display result
public void printSequence(int[] result, int k)
{
for (int i = 0; i < k; ++i)
{
System.out.print(result[i]);
}
System.out.print("\n ");
}
public void findCombination(int[] result, int index, int n)
{
if (index == n)
{
// Display calculated result
printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
findCombination(result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
findCombination(result, index + 1, n);
}
}
else
{
// This is used to setup first element
for (int i = 1; i <= 9; ++i)
{
result[index] = i;
findCombination(result, index + 1, n);
}
}
}
// Handles the request of find combination of given n
public void jumpingNo(int n)
{
if (n <= 0)
{
return;
}
System.out.print("\n Given n : " + n + " \n ");
// Collect result
int[] result = new int[n];
if (n == 1)
{
System.out.print("\n 0 \n ");
}
// Test
findCombination(result, 0, n);
}
public static void main(String[] args)
{
JumpingNumber task = new JumpingNumber();
// Test
task.jumpingNo(4);
}
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Print all N digit jumping numbers
*/
class JumpingNumber
{
public:
// Display result
void printSequence(int result[], int k)
{
for (int i = 0; i < k; ++i)
{
cout << result[i];
}
cout << "\n ";
}
void findCombination(int result[], int index, int n)
{
if (index == n)
{
// Display calculated result
this->printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
this->findCombination(result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
this->findCombination(result, index + 1, n);
}
}
else
{
// This is used to setup first element
for (int i = 1; i <= 9; ++i)
{
result[index] = i;
this->findCombination(result, index + 1, n);
}
}
}
// Handles the request of find combination of given n
void jumpingNo(int n)
{
if (n <= 0)
{
return;
}
cout << "\n Given n : " << n << " \n ";
// Collect result
int result[n];
if (n == 1)
{
cout << "\n 0 \n ";
}
// Test
this->findCombination(result, 0, n);
}
};
int main()
{
JumpingNumber *task = new JumpingNumber();
// Test
task->jumpingNo(4);
return 0;
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
// Include namespace system
using System;
/*
Csharp program
Print all N digit jumping numbers
*/
public class JumpingNumber
{
// Display result
public void printSequence(int[] result, int k)
{
for (int i = 0; i < k; ++i)
{
Console.Write(result[i]);
}
Console.Write("\n ");
}
public void findCombination(int[] result, int index, int n)
{
if (index == n)
{
// Display calculated result
this.printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
this.findCombination(result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
this.findCombination(result, index + 1, n);
}
}
else
{
// This is used to setup first element
for (int i = 1; i <= 9; ++i)
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
}
// Handles the request of find combination of given n
public void jumpingNo(int n)
{
if (n <= 0)
{
return;
}
Console.Write("\n Given n : " + n + " \n ");
// Collect result
int[] result = new int[n];
if (n == 1)
{
Console.Write("\n 0 \n ");
}
// Test
this.findCombination(result, 0, n);
}
public static void Main(String[] args)
{
JumpingNumber task = new JumpingNumber();
// Test
task.jumpingNo(4);
}
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
package main
import "fmt"
/*
Go program
Print all N digit jumping numbers
*/
type JumpingNumber struct {}
func getJumpingNumber() * JumpingNumber {
var me *JumpingNumber = &JumpingNumber {}
return me
}
// Display result
func(this JumpingNumber) printSequence(result[] int, k int) {
for i := 0 ; i < k ; i++ {
fmt.Print(result[i])
}
fmt.Print("\n ")
}
func(this JumpingNumber) findCombination(result[] int, index int, n int) {
if index == n {
// Display calculated result
this.printSequence(result, index)
return
}
if index > n {
return
}
if index > 0 {
if result[index - 1] != 0 {
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1
this.findCombination(result, index + 1, n)
}
if result[index - 1] != 9 {
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1
// Find next digit
this.findCombination(result, index + 1, n)
}
} else {
// This is used to setup first element
for i := 1 ; i <= 9 ; i++ {
result[index] = i
this.findCombination(result, index + 1, n)
}
}
}
// Handles the request of find combination of given n
func(this JumpingNumber) jumpingNo(n int) {
if n <= 0 {
return
}
fmt.Print("\n Given n : ", n, " \n ")
// Collect result
var result = make([] int, n)
if n == 1 {
fmt.Print("\n 0 \n ")
}
// Test
this.findCombination(result, 0, n)
}
func main() {
var task * JumpingNumber = getJumpingNumber()
// Test
task.jumpingNo(4)
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
<?php
/*
Php program
Print all N digit jumping numbers
*/
class JumpingNumber
{
// Display result
public function printSequence($result, $k)
{
for ($i = 0; $i < $k; ++$i)
{
echo($result[$i]);
}
echo("\n ");
}
public function findCombination($result, $index, $n)
{
if ($index == $n)
{
// Display calculated result
$this->printSequence($result, $index);
return;
}
if ($index > $n)
{
return;
}
if ($index > 0)
{
if ($result[$index - 1] != 0)
{
// Set new digit by reduce previous digit by one
$result[$index] = $result[$index - 1] - 1;
$this->findCombination($result, $index + 1, $n);
}
if ($result[$index - 1] != 9)
{
// Set new digit by increase previous digit by one
$result[$index] = $result[$index - 1] + 1;
// Find next digit
$this->findCombination($result, $index + 1, $n);
}
}
else
{
// This is used to setup first element
for ($i = 1; $i <= 9; ++$i)
{
$result[$index] = $i;
$this->findCombination($result, $index + 1, $n);
}
}
}
// Handles the request of find combination of given n
public function jumpingNo($n)
{
if ($n <= 0)
{
return;
}
echo("\n Given n : ".$n.
" \n ");
// Collect result
$result = array_fill(0, $n, 0);
if ($n == 1)
{
echo("\n 0 \n ");
}
// Test
$this->findCombination($result, 0, $n);
}
}
function main()
{
$task = new JumpingNumber();
// Test
$task->jumpingNo(4);
}
main();
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
/*
Node JS program
Print all N digit jumping numbers
*/
class JumpingNumber
{
// Display result
printSequence(result, k)
{
for (var i = 0; i < k; ++i)
{
process.stdout.write("" + result[i]);
}
process.stdout.write("\n ");
}
findCombination(result, index, n)
{
if (index == n)
{
// Display calculated result
this.printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
this.findCombination(result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
this.findCombination(result, index + 1, n);
}
}
else
{
// This is used to setup first element
for (var i = 1; i <= 9; ++i)
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
}
// Handles the request of find combination of given n
jumpingNo(n)
{
if (n <= 0)
{
return;
}
process.stdout.write("\n Given n : " + n + " \n ");
// Collect result
var result = Array(n).fill(0);
if (n == 1)
{
process.stdout.write("\n 0 \n ");
}
// Test
this.findCombination(result, 0, n);
}
}
function main()
{
var task = new JumpingNumber();
// Test
task.jumpingNo(4);
}
main();
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
# Python 3 program
# Print all N digit jumping numbers
class JumpingNumber :
# Display result
def printSequence(self, result, k) :
i = 0
while (i < k) :
print(result[i], end = "")
i += 1
print("\n ", end = "")
def findCombination(self, result, index, n) :
if (index == n) :
# Display calculated result
self.printSequence(result, index)
return
if (index > n) :
return
if (index > 0) :
if (result[index - 1] != 0) :
# Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1
self.findCombination(result, index + 1, n)
if (result[index - 1] != 9) :
# Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1
# Find next digit
self.findCombination(result, index + 1, n)
else :
i = 1
# This is used to setup first element
while (i <= 9) :
result[index] = i
self.findCombination(result, index + 1, n)
i += 1
# Handles the request of find combination of given n
def jumpingNo(self, n) :
if (n <= 0) :
return
print("\n Given n : ", n ," \n ", end = "")
# Collect result
result = [0] * (n)
if (n == 1) :
print("\n 0 \n ", end = "")
# Test
self.findCombination(result, 0, n)
def main() :
task = JumpingNumber()
# Test
task.jumpingNo(4)
if __name__ == "__main__": main()
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
# Ruby program
# Print all N digit jumping numbers
class JumpingNumber
# Display result
def printSequence(result, k)
i = 0
while (i < k)
print(result[i])
i += 1
end
print("\n ")
end
def findCombination(result, index, n)
if (index == n)
# Display calculated result
self.printSequence(result, index)
return
end
if (index > n)
return
end
if (index > 0)
if (result[index - 1] != 0)
# Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1
self.findCombination(result, index + 1, n)
end
if (result[index - 1] != 9)
# Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1
# Find next digit
self.findCombination(result, index + 1, n)
end
else
i = 1
# This is used to setup first element
while (i <= 9)
result[index] = i
self.findCombination(result, index + 1, n)
i += 1
end
end
end
# Handles the request of find combination of given n
def jumpingNo(n)
if (n <= 0)
return
end
print("\n Given n : ", n ," \n ")
# Collect result
result = Array.new(n) {0}
if (n == 1)
print("\n 0 \n ")
end
# Test
self.findCombination(result, 0, n)
end
end
def main()
task = JumpingNumber.new()
# Test
task.jumpingNo(4)
end
main()
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
/*
Scala program
Print all N digit jumping numbers
*/
class JumpingNumber()
{
// Display result
def printSequence(result: Array[Int], k: Int): Unit = {
var i: Int = 0;
while (i < k)
{
print(result(i));
i += 1;
}
print("\n ");
}
def findCombination(result: Array[Int], index: Int, n: Int): Unit = {
if (index == n)
{
// Display calculated result
printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result(index - 1) != 0)
{
// Set new digit by reduce previous digit by one
result(index) = result(index - 1) - 1;
findCombination(result, index + 1, n);
}
if (result(index - 1) != 9)
{
// Set new digit by increase previous digit by one
result(index) = result(index - 1) + 1;
// Find next digit
findCombination(result, index + 1, n);
}
}
else
{
var i: Int = 1;
// This is used to setup first element
while (i <= 9)
{
result(index) = i;
findCombination(result, index + 1, n);
i += 1;
}
}
}
// Handles the request of find combination of given n
def jumpingNo(n: Int): Unit = {
if (n <= 0)
{
return;
}
print("\n Given n : " + n + " \n ");
// Collect result
var result: Array[Int] = Array.fill[Int](n)(0);
if (n == 1)
{
print("\n 0 \n ");
}
// Test
findCombination(result, 0, n);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: JumpingNumber = new JumpingNumber();
// Test
task.jumpingNo(4);
}
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
/*
Swift 4 program
Print all N digit jumping numbers
*/
class JumpingNumber
{
// Display result
func printSequence(_ result: [Int], _ k: Int)
{
var i: Int = 0;
while (i < k)
{
print(result[i], terminator: "");
i += 1;
}
print("\n ", terminator: "");
}
func findCombination(_ result: inout[Int], _ index: Int, _ n: Int)
{
if (index == n)
{
// Display calculated result
self.printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
self.findCombination(&result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
self.findCombination(&result, index + 1, n);
}
}
else
{
var i: Int = 1;
// This is used to setup first element
while (i <= 9)
{
result[index] = i;
self.findCombination(&result, index + 1, n);
i += 1;
}
}
}
// Handles the request of find combination of given n
func jumpingNo(_ n: Int)
{
if (n <= 0)
{
return;
}
print("\n Given n : ", n ," \n ", terminator: "");
// Collect result
var result: [Int] = Array(repeating: 0, count: n);
if (n == 1)
{
print("\n 0 \n ", terminator: "");
}
// Test
self.findCombination(&result, 0, n);
}
}
func main()
{
let task: JumpingNumber = JumpingNumber();
// Test
task.jumpingNo(4);
}
main();
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
/*
Kotlin program
Print all N digit jumping numbers
*/
class JumpingNumber
{
// Display result
fun printSequence(result: Array < Int > , k: Int): Unit
{
var i: Int = 0;
while (i < k)
{
print(result[i]);
i += 1;
}
print("\n ");
}
fun findCombination(result: Array < Int > , index: Int, n: Int): Unit
{
if (index == n)
{
// Display calculated result
this.printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
this.findCombination(result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
this.findCombination(result, index + 1, n);
}
}
else
{
var i: Int = 1;
// This is used to setup first element
while (i <= 9)
{
result[index] = i;
this.findCombination(result, index + 1, n);
i += 1;
}
}
}
// Handles the request of find combination of given n
fun jumpingNo(n: Int): Unit
{
if (n <= 0)
{
return;
}
print("\n Given n : " + n + " \n ");
// Collect result
val result: Array < Int > = Array(n)
{
0
};
if (n == 1)
{
print("\n 0 \n ");
}
// Test
this.findCombination(result, 0, n);
}
}
fun main(args: Array < String > ): Unit
{
val task: JumpingNumber = JumpingNumber();
// Test
task.jumpingNo(4);
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
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