Q:

C# program to create gray code

belongs to collection: C# Basic Programs | basics

0

C# program to create gray code

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to create a gray code is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to create gray code.

using System;

class Program
{
    public static int CreateGraycode(int num)
    {
        int gray = 0;

        gray = num ^ (num >> 1);

        return gray;
    }

    static void Main(string[] args)
    {
        int loop = 0;

        Console.WriteLine("Number\tGray Code");

        for (loop = 0; loop < 5; loop++)
        {
            Console.WriteLine(string.Format("{0}\t{1}", loop, Convert.ToString((int)CreateGraycode(loop), 2)));
        }
    }
}

Output:

Number  Gray Code
0       0
1       1
2       11
3       10
4       110
Press any key to continue . . .

Explanation:

In the above program, we created a class Program that contains two static methods CreateGrayCode() and Main(). The CreateGrayCode() method is used to convert a number into a gray code.

In the Main() method, we convert numbers into gray code using for loop and print them on the console screen.

for (loop = 0; loop < 5; loop++)
{
    Console.WriteLine(string.Format("{0}\t{1}", loop, Convert.ToString((int)CreateGraycode(loop), 2)));
}

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | basics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to change the case of entered character... >>
<< C# program to convert a temperature from Fahrenhei...