Simple C program to convert gray code to binary and vice versa????

2 Answers

Relevance
  • 4 years ago

    Converting Binary To Gray Code

    • Commenter avatarLog in to reply to the answers
  • 7 years ago

    Conversion of Gray code to Binary Code :-

    #include<stdio.h>

    #include<conio.h>

    void main()

    {int a[10],i=0,c=0,n;

    printf("\n enter the gray code");

    scanf("%d",&n);

    while(n!=0)

    {a[i]=n%10;

    n/=10;

    i++;

    c++;

    }

    for(i=c-1;i>=0;i--)

    {

    if(a[i]==1)

    {

    if(a[i-1]==1)

    a[i-1]=0;

    else

    a[i-1]=1;

    }

    }

    printf("\n the binary code is");

    for(i=c-1;i>=0;i--)

    printf("%d",a[i]);

    getch();

    }

    Conversion of Binary Code to Gray Code :-

    #include<stdio.h>

    #include<conio.h>

    void main()

    {int a[10],i=0,c=0,n,b[10];

    printf("\n enter the binary code");

    scanf("%d",&n);

    while(n!=0)

    {a[i]=n%10;

    n/=10;

    i++;

    c++;

    }

    for(i=c-1;i>=0;i--)

    {

    b[i]=a[i];

    }

    for(i=c-1;i>=0;i--)

    {

    if(b[i]==1)

    {

    if(a[i-1]==1)

    a[i-1]=0;

    else

    a[i-1]=1;

    }

    }

    printf("\n the gray code is");

    for(i=c-1;i>=0;i--)

    printf("%d",a[i]);

    getch();

    }

    • Commenter avatarLog in to reply to the answers
Still have questions? Get answers by asking now.