C program to convert Binary code to gray code

Simple C program to convert Binary code to gray code

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();
}

Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

Servlet Program to Print Today’s Date and Time using refresh header

Java Program to Implement sorting algorithm using TCP on Server application