Friday, January 2, 2015

Binary Code of a Equivalent Gray’s in c pgm



Convert Binary Code of a Number into its Equivalent Gray’s Code without using Recursion

#include 
#include 
#include 
 
int bintogray(int);
 
int main ()
{
    int bin, gray;
    clrscr();
    printf("Enter a binary number: ");
    scanf("%d", &bin);
    gray = bintogray(bin);
    printf("The gray code of %d is %d\n", bin, gray);
    return 0;
}
 
int bintogray(int bin)
{
    int a, b, result = 0, i = 0;
 
    while (bin != 0)
    {
        a = bin % 10;
        bin = bin / 10;
        b = bin % 10;
        if ((a && !b) || (!a && b))
        {
            result = result + pow(10, i);
        }
        i++;
    }
    return result;
}

Output
 
Enter a binary number: 1111001010
The gray code of 1111001010 is 1000101111

No comments:

Post a Comment