Conversion from gray to binary code

Here given code implementation process.

/*
  Java Program
  Conversion from gray to binary code
*/
public class Conversion
{

    // Convert given gray code to binary number
    public void grayToBinary(String gray)
    {
        // Get the length of given number
        int n = gray.length();
        // Used to collect result
        String binary = "";
        // Set first most significant bit
        binary = ""+gray.charAt(0);
        // Execute loop through by length
        for (int i = 1; i < n; ++i)
        {
            if (gray.charAt(i)== '0')
            {
            
                binary += binary.charAt(i-1);
            }
            else if(binary.charAt(i-1)=='0')
            {
              
                binary += "1";
            }
            else
            {
                binary += "0";
            }
        }
    
        // Display calculated result  
        System.out.print(" Gray : " + gray );
        System.out.print("\n Binary : " + binary + " \n\n");
     
    }
    public static void main(String[] args)
    {
        Conversion task = new Conversion();

        task.grayToBinary("01001111");
        task.grayToBinary("1111");
    
    }
}

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
// C Program
// Conversion from gray to binary code
#include <stdio.h>

#include <string.h>

// Convert given gray code to binary number
void grayToBinary(const char *gray)
{
	// Get the length of given gray code 
	int n = strlen(gray);
	// Used to collect result
	char binary[n + 1];
	// Set first most significant bit
	binary[0] = gray[0];
	// Execute loop through by length
	for (int i = 1; i < n; ++i)
	{
		if (gray[i] == '0')
		{
			binary[i] = binary[i - 1];
		}
		else if (binary[i - 1] == '0')
		{
			binary[i] = '1';
		}
		else
		{
			binary[i] = '0';
		}
	}
	binary[n] = '\0';
	// Display calculated result  
	printf(" Gray : %s", gray);
	printf("\n Binary : %s\n\n", binary);
}
int main()
{
	// Test Case
	grayToBinary("01001111");
	grayToBinary("1111");
	return 0;
}

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
// Include header file
#include <iostream>
#include <string.h>

using namespace std;
/*
  C++ Program
  Conversion from gray to binary code
*/
class Conversion
{
	public:
		// Convert given gray code to binary number
		void grayToBinary(string gray)
		{
			// Get the length of given number
			int n = gray.size();
			// Used to collect result
			string binary = "";
			// Set first most significant bit
			binary = gray[0];
			// Execute loop through by length
			for (int i = 1; i < n; ++i)
			{
				if (gray[i] == '0')
				{
					binary += binary[i - 1];
				}
				else if (binary[i - 1] == '0')
				{
					binary += "1";
				}
				else
				{
					binary += "0";
				}
			}
			// Display calculated result
			cout << " Gray : " << gray;
			cout << "\n Binary : " << binary << " \n\n";
		}
};
int main()
{
	Conversion task = Conversion();
	task.grayToBinary("01001111");
	task.grayToBinary("1111");
	return 0;
}

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
// Include namespace system
using System;
/*
  C# Program
  Conversion from gray to binary code
*/
public class Conversion
{
	// Convert given gray code to binary number
	public void grayToBinary(String gray)
	{
		// Get the length of given number
		int n = gray.Length;
		// Used to collect result
		String binary = "";
		// Set first most significant bit
		binary = "" + gray[0];
		// Execute loop through by length
		for (int i = 1; i < n; ++i)
		{
			if (gray[i] == '0')
			{
				binary += binary[i - 1];
			}
			else if (binary[i - 1] == '0')
			{
				binary += "1";
			}
			else
			{
				binary += "0";
			}
		}
		// Display calculated result
		Console.Write(" Gray : " + gray);
		Console.Write("\n Binary : " + binary + " \n\n");
	}
	public static void Main(String[] args)
	{
		Conversion task = new Conversion();
		task.grayToBinary("01001111");
		task.grayToBinary("1111");
	}
}

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
<?php
/*
  Php Program
  Conversion from gray to binary code
*/
class Conversion
{
	// Convert given gray code to binary number
	public	function grayToBinary($gray)
	{
		// Get the length of given number
		$n = strlen($gray);
		// Used to collect result
		$binary = "";
		// Set first most significant bit
		$binary = "". $gray[0];
		// Execute loop through by length
		for ($i = 1; $i < $n; ++$i)
		{
			if ($gray[$i] == '0')
			{
				$binary .= $binary[$i - 1];
			}
			else if ($binary[$i - 1] == '0')
			{
				$binary .= "1";
			}
			else
			{
				$binary .= "0";
			}
		}
		// Display calculated result
		echo " Gray : ". $gray;
		echo "\n Binary : ". $binary ." \n\n";
	}
}

function main()
{
	$task = new Conversion();
	$task->grayToBinary("01001111");
	$task->grayToBinary("1111");
}
main();

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
/*
  Node Js Program
  Conversion from gray to binary code
*/
class Conversion
{
	// Convert given gray code to binary number
	grayToBinary(gray)
	{
		// Get the length of given number
		var n = gray.length;
		// Used to collect result
		var binary = "";
		// Set first most significant bit
		binary = "" + gray[0];
		// Execute loop through by length
		for (var i = 1; i < n; ++i)
		{
			if (gray[i] == '0')
			{
				binary += binary[i - 1];
			}
			else if (binary[i - 1] == '0')
			{
				binary += "1";
			}
			else
			{
				binary += "0";
			}
		}
		// Display calculated result
		process.stdout.write(" Gray : " + gray);
		process.stdout.write("\n Binary : " + binary + " \n\n");
	}
}

function main()
{
	var task = new Conversion();
	task.grayToBinary("01001111");
	task.grayToBinary("1111");
}
main();

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
#   Python 3 Program
#   Conversion from gray to binary code

class Conversion :
	#  Convert given gray code to binary number
	def grayToBinary(self, gray) :
		#  Get the length of given number
		n = len(gray)
		#  Used to collect result
		binary = ""
		#  Set first most significant bit
		binary = gray[0]
		i = 1
		#  Execute loop through by length
		while (i < n) :
			if (gray[i] == '0') :
				binary += binary[i - 1]
			
			elif(binary[i - 1] == '0') :
				binary += "1"
			else :
				binary += "0"
			
			i += 1
		
		#  Display calculated result
		print(" Gray : ", gray, end = "")
		print("\n Binary : ", binary ," \n")
	

def main() :
	task = Conversion()
	task.grayToBinary("01001111")
	task.grayToBinary("1111")

if __name__ == "__main__": main()

Output

 Gray :  01001111
 Binary :  01110101

 Gray :  1111
 Binary :  1010
#   Ruby Program
#   Conversion from gray to binary code

class Conversion 
	#  Convert given gray code to binary number
	def grayToBinary(gray) 
		#  Get the length of given number
		n = gray.length()
		#  Used to collect result
		binary = ""
		#  Set first most significant bit
		binary =  gray[0]
		i = 1
		#  Execute loop through by length
		while (i < n) 
			if (gray[i] == '0') 
				binary += binary[i - 1]
			elsif(binary[i - 1] == '0') 
				binary += "1"
			else 
				binary += "0"
			end

			i += 1
		end

		#  Display calculated result
		print(" Gray : ", gray)
		print("\n Binary : ", binary ," \n\n")
	end

end

def main() 
	task = Conversion.new()
	task.grayToBinary("01001111")
	task.grayToBinary("1111")
end

main()

Output

 Gray : 01001111
 Binary : 01110101 

 Gray : 1111
 Binary : 1010 

/*
  Scala Program
  Conversion from gray to binary code
*/
class Conversion
{
	// Convert given gray code to binary number
	def grayToBinary(gray: String): Unit = {
		// Get the length of given number
		var n: Int = gray.length();
		// Used to collect result
		var binary: String = "";
		// Set first most significant bit
		binary = "" + gray(0);
		var i: Int = 1;
		// Execute loop through by length
		while (i < n)
		{
			if (gray(i) == '0')
			{
				binary += binary(i - 1);
			}
			else if (binary(i - 1) == '0')
			{
				binary += "1";
			}
			else
			{
				binary += "0";
			}
			i += 1;
		}
		// Display calculated result
		print(" Gray : " + gray);
		print("\n Binary : " + binary + " \n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Conversion = new Conversion();
		task.grayToBinary("01001111");
		task.grayToBinary("1111");
	}
}

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
/*
  Swift 4 Program
  Conversion from gray to binary code
*/
class Conversion
{
	// Convert given gray code to binary number
	func grayToBinary(_ gray: String)
	{
		// Get the length of given number
		let n: Int = gray.count;
		// Used to collect result
		var binary: String = "";
		// Set first most significant bit
		binary = String(Array(gray)[0]);
		var i: Int = 1;
		// Execute loop through by length
		while (i < n)
		{
			if (Array(gray)[i] == "0")
			{
				binary += String(Array(binary)[i-1]);
			}
			else if (Array(binary)[i-1] == "0")
			{
				binary += "1";
			}
			else
			{
				binary += "0";
			}
			i += 1;
		}
		// Display calculated result
		print(" Gray : ", gray, terminator: "");
		print("\n Binary : ", binary ," \n");
	}
}
func main()
{
	let task: Conversion = Conversion();
	task.grayToBinary("01001111");
	task.grayToBinary("1111");
}
main();

Output

 Gray :  01001111
 Binary :  01110101

 Gray :  1111
 Binary :  1010
/*
  Kotlin Program
  Conversion from gray to binary code
*/
class Conversion
{
	// Convert given gray code to binary number
	fun grayToBinary(gray: String): Unit
	{
		// Get the length of given number
		var n: Int = gray.count();
		// Used to collect result
      	// Set first most significant bit
		var binary: String = "" + gray[0];
		var i: Int = 1;
		// Execute loop through by length
		while (i < n)
		{
			if (gray[i] == '0')
			{
				binary += binary[i - 1];
			}
			else if (binary[i - 1] == '0')
			{
				binary += "1";
			}
			else
			{
				binary += "0";
			}
			i += 1;
		}
		// Display calculated result
		print(" Gray : " + gray);
		print("\n Binary : " + binary + " \n\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Conversion = Conversion();
	task.grayToBinary("01001111");
	task.grayToBinary("1111");
}

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010


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