Generate n bit gray code
Here given code implementation process.
/*
C Program for
Generate n bit gray code
*/
#include <stdio.h>
// Print gray code
void grayCode(int n, int *code)
{
if (n == 0)
{
// Display calculate code
printf("\n %d", *code);
return;
}
grayCode(n - 1, code);
// Calculate gray code
*code = *code ^ (1 << (n - 1));
grayCode(n - 1, code);
}
// Handles the request to find gray code
void findGraycode(int n)
{
if (n <= 0)
{
return;
}
printf("\n (%d) Bit gray code ", n);
int code = 0;
grayCode(n, & code);
printf("\n");
}
int main()
{
int n = 4;
// Test when n = 4
findGraycode(n);
n = 5;
// Test when n = 5
findGraycode(n);
return 0;
}
Output
(4) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
(5) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
24
25
27
26
30
31
29
28
20
21
23
22
18
19
17
16
/*
Java Program for
Generate n bit gray code
*/
public class GenerateGrayCode
{
public int code;
public GenerateGrayCode()
{
this.code = 0;
}
// Print gray code
public void grayCode(int n)
{
if (n == 0)
{
// Display calculate code
System.out.print("\n " + this.code );
return;
}
grayCode(n - 1);
// Calculate gray code
this.code = this.code ^ (1 << (n - 1));
grayCode(n - 1);
}
// Handles the request to find gray code
public void findGraycode(int n)
{
if (n <= 0)
{
return;
}
System.out.print("\n (" + n + ") Bit gray code ");
this.code = 0;
grayCode(n);
System.out.print("\n");
}
public static void main(String[] args)
{
GenerateGrayCode task = new GenerateGrayCode();
int n = 4;
// Test when n = 4
task.findGraycode(n);
n = 5;
// Test when n = 5
task.findGraycode(n);
}
}
Output
(4) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
(5) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
24
25
27
26
30
31
29
28
20
21
23
22
18
19
17
16
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Generate n bit gray code
*/
class GenerateGrayCode
{
public: int code;
GenerateGrayCode()
{
this->code = 0;
}
// Print gray code
void grayCode(int n)
{
if (n == 0)
{
// Display calculate code
cout << "\n " << this->code;
return;
}
this->grayCode(n - 1);
// Calculate gray code
this->code = this->code ^ (1 << (n - 1));
this->grayCode(n - 1);
}
// Handles the request to find gray code
void findGraycode(int n)
{
if (n <= 0)
{
return;
}
cout << "\n (" << n << ") Bit gray code ";
this->code = 0;
this->grayCode(n);
cout << "\n";
}
};
int main()
{
GenerateGrayCode task = GenerateGrayCode();
int n = 4;
// Test when n = 4
task.findGraycode(n);
n = 5;
// Test when n = 5
task.findGraycode(n);
return 0;
}
Output
(4) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
(5) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
24
25
27
26
30
31
29
28
20
21
23
22
18
19
17
16
// Include namespace system
using System;
/*
C# Program for
Generate n bit gray code
*/
public class GenerateGrayCode
{
public int code;
public GenerateGrayCode()
{
this.code = 0;
}
// Print gray code
public void grayCode(int n)
{
if (n == 0)
{
// Display calculate code
Console.Write("\n " + this.code);
return;
}
grayCode(n - 1);
// Calculate gray code
this.code = this.code ^ (1 << (n - 1));
grayCode(n - 1);
}
// Handles the request to find gray code
public void findGraycode(int n)
{
if (n <= 0)
{
return;
}
Console.Write("\n (" + n + ") Bit gray code ");
this.code = 0;
grayCode(n);
Console.Write("\n");
}
public static void Main(String[] args)
{
GenerateGrayCode task = new GenerateGrayCode();
int n = 4;
// Test when n = 4
task.findGraycode(n);
n = 5;
// Test when n = 5
task.findGraycode(n);
}
}
Output
(4) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
(5) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
24
25
27
26
30
31
29
28
20
21
23
22
18
19
17
16
<?php
/*
Php Program for
Generate n bit gray code
*/
class GenerateGrayCode
{
public $code;
function __construct()
{
$this->code = 0;
}
// Print gray code
public function grayCode($n)
{
if ($n == 0)
{
// Display calculate code
echo "\n ". $this->code;
return;
}
$this->grayCode($n - 1);
// Calculate gray code
$this->code = $this->code ^ (1 << ($n - 1));
$this->grayCode($n - 1);
}
// Handles the request to find gray code
public function findGraycode($n)
{
if ($n <= 0)
{
return;
}
echo "\n (". $n .") Bit gray code ";
$this->code = 0;
$this->grayCode($n);
echo "\n";
}
}
function main()
{
$task = new GenerateGrayCode();
$n = 4;
// Test when n = 4
$task->findGraycode($n);
$n = 5;
// Test when n = 5
$task->findGraycode($n);
}
main();
Output
(4) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
(5) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
24
25
27
26
30
31
29
28
20
21
23
22
18
19
17
16
/*
Node Js Program for
Generate n bit gray code
*/
class GenerateGrayCode
{
constructor()
{
this.code = 0;
}
// Print gray code
grayCode(n)
{
if (n == 0)
{
// Display calculate code
console.log(" "+this.code);
return;
}
this.grayCode(n - 1);
// Calculate gray code
this.code = this.code ^ (1 << (n - 1));
this.grayCode(n - 1);
}
// Handles the request to find gray code
findGraycode(n)
{
if (n <= 0)
{
return;
}
console.log(" (" + n + ") Bit gray code ");
this.code = 0;
this.grayCode(n);
console.log("\n");
}
}
function main()
{
var task = new GenerateGrayCode();
var n = 4;
// Test when n = 4
task.findGraycode(n);
n = 5;
// Test when n = 5
task.findGraycode(n);
}
main();
Output
(4) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
(5) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
24
25
27
26
30
31
29
28
20
21
23
22
18
19
17
16
# Python 3 Program for
# Generate n bit gray code
class GenerateGrayCode :
def __init__(self) :
self.code = 0
# Print gray code
def grayCode(self, n) :
if (n == 0) :
# Display calculate code
print("\n ", self.code, end = "")
return
self.grayCode(n - 1)
# Calculate gray code
self.code = self.code ^ (1 << (n - 1))
self.grayCode(n - 1)
# Handles the request to find gray code
def findGraycode(self, n) :
if (n <= 0) :
return
print("\n (", n ,") Bit gray code ", end = "")
self.code = 0
self.grayCode(n)
print(end = "\n")
def main() :
task = GenerateGrayCode()
n = 4
# Test when n = 4
task.findGraycode(n)
n = 5
# Test when n = 5
task.findGraycode(n)
if __name__ == "__main__": main()
Output
( 4 ) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
( 5 ) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
24
25
27
26
30
31
29
28
20
21
23
22
18
19
17
16
# Ruby Program for
# Generate n bit gray code
class GenerateGrayCode
# Define the accessor and reader of class GenerateGrayCode
attr_reader :code
attr_accessor :code
def initialize()
self.code = 0
end
# Print gray code
def grayCode(n)
if (n == 0)
# Display calculate code
print("\n ", self.code)
return
end
self.grayCode(n - 1)
# Calculate gray code
self.code = self.code ^ (1 << (n - 1))
self.grayCode(n - 1)
end
# Handles the request to find gray code
def findGraycode(n)
if (n <= 0)
return
end
print("\n (", n ,") Bit gray code ")
self.code = 0
self.grayCode(n)
print("\n")
end
end
def main()
task = GenerateGrayCode.new()
n = 4
# Test when n = 4
task.findGraycode(n)
n = 5
# Test when n = 5
task.findGraycode(n)
end
main()
Output
(4) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
(5) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
24
25
27
26
30
31
29
28
20
21
23
22
18
19
17
16
/*
Scala Program for
Generate n bit gray code
*/
class GenerateGrayCode(var code: Int)
{
def this()
{
this(0);
}
// Print gray code
def grayCode(n: Int): Unit = {
if (n == 0)
{
// Display calculate code
print("\n " + this.code);
return;
}
this.grayCode(n - 1);
// Calculate gray code
this.code = this.code ^ (1 << (n - 1));
this.grayCode(n - 1);
}
// Handles the request to find gray code
def findGraycode(n: Int): Unit = {
if (n <= 0)
{
return;
}
print("\n (" + n + ") Bit gray code ");
this.code = 0;
this.grayCode(n);
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: GenerateGrayCode = new GenerateGrayCode();
var n: Int = 4;
// Test when n = 4
task.findGraycode(n);
n = 5;
// Test when n = 5
task.findGraycode(n);
}
}
Output
(4) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
(5) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
24
25
27
26
30
31
29
28
20
21
23
22
18
19
17
16
/*
Swift 4 Program for
Generate n bit gray code
*/
class GenerateGrayCode
{
var code: Int;
init()
{
self.code = 0;
}
// Print gray code
func grayCode(_ n: Int)
{
if (n == 0)
{
// Display calculate code
print("\n ", self.code, terminator: "");
return;
}
self.grayCode(n - 1);
// Calculate gray code
self.code = self.code ^ (1 << (n - 1));
self.grayCode(n - 1);
}
// Handles the request to find gray code
func findGraycode(_ n: Int)
{
if (n <= 0)
{
return;
}
print("\n (", n ,") Bit gray code ", terminator: "");
self.code = 0;
self.grayCode(n);
print(terminator: "\n");
}
}
func main()
{
let task: GenerateGrayCode = GenerateGrayCode();
var n: Int = 4;
// Test when n = 4
task.findGraycode(n);
n = 5;
// Test when n = 5
task.findGraycode(n);
}
main();
Output
( 4 ) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
( 5 ) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
24
25
27
26
30
31
29
28
20
21
23
22
18
19
17
16
/*
Kotlin Program for
Generate n bit gray code
*/
class GenerateGrayCode
{
var code: Int;
constructor()
{
this.code = 0;
}
// Print gray code
fun grayCode(n: Int): Unit
{
if (n == 0)
{
// Display calculate code
print("\n " + this.code);
return;
}
this.grayCode(n - 1);
// Calculate gray code
this.code = this.code xor (1 shl(n - 1));
this.grayCode(n - 1);
}
// Handles the request to find gray code
fun findGraycode(n: Int): Unit
{
if (n <= 0)
{
return;
}
print("\n (" + n + ") Bit gray code ");
this.code = 0;
this.grayCode(n);
print("\n");
}
}
fun main(args: Array < String > ): Unit
{
var task: GenerateGrayCode = GenerateGrayCode();
var n: Int = 4;
// Test when n = 4
task.findGraycode(n);
n = 5;
// Test when n = 5
task.findGraycode(n);
}
Output
(4) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
(5) Bit gray code
0
1
3
2
6
7
5
4
12
13
15
14
10
11
9
8
24
25
27
26
30
31
29
28
20
21
23
22
18
19
17
16
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