This will convert a decimal number into Gray code .
Gray Code Conversion
/////////////////////////////////////////////////////////////
//// Coverting a decimal number to Gray code /////
///////////////////////////////////////////////////////////
# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
static int a[8],b[8],k=1,i;
void main()
{ int n1;
clrscr();
printf("Enter any Numbers :");
scanf("%d",&n1);
while(n1!=0) /* converting number to its binary equivalent */
{ a[i]=n1 % 2;
n1/=2;
i++;
}
/* printing binary equivalent */
printf("\nThe binary code of the given number is :");
for(i=7;i>=0;i--)
printf("%d",a[i]);
/* gray code conversion */
b[0]=a[7];
for(i=7;i>=0;i--)
{ if(a[i]==0 && a[i-1]==0)
b[k]=0;
if(a[i]==1 && a[i-1]==1)
b[k]=0;
if(a[i]==0 && a[i-1]==1)
b[k]=1;
if(a[i]==1 && a[i-1]==0)
b[k]=1;
k++;
}
/* printing the gray code */
printf("\nThe gray code of the given number is :");
for(i=0;i<8;i++)
printf("%d",b[i]);
}
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
balajisankar 0 Newbie Poster
folderol 0 Newbie Poster
v3ga
sunktugg 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.