Gray Code Conversion

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
harshchandra harshchandra is offline Offline Nov 21st, 2004, 9:43 am |
0
This will convert a decimal number into Gray code .
Quick reply to this message  
C Syntax
  1. /////////////////////////////////////////////////////////////
  2. //// Coverting a decimal number to Gray code /////
  3. ///////////////////////////////////////////////////////////
  4.  
  5.  
  6.  
  7. # include<stdio.h>
  8. # include<conio.h>
  9. # include<stdlib.h>
  10. static int a[8],b[8],k=1,i;
  11. void main()
  12. { int n1;
  13. clrscr();
  14. printf("Enter any Numbers :");
  15. scanf("%d",&n1);
  16. while(n1!=0) /* converting number to its binary equivalent */
  17. { a[i]=n1 % 2;
  18. n1/=2;
  19. i++;
  20. }
  21. /* printing binary equivalent */
  22. printf("\nThe binary code of the given number is :");
  23. for(i=7;i>=0;i--)
  24. printf("%d",a[i]);
  25.  
  26.  
  27. /* gray code conversion */
  28. b[0]=a[7];
  29.  
  30. for(i=7;i>=0;i--)
  31. { if(a[i]==0 && a[i-1]==0)
  32. b[k]=0;
  33. if(a[i]==1 && a[i-1]==1)
  34. b[k]=0;
  35. if(a[i]==0 && a[i-1]==1)
  36. b[k]=1;
  37. if(a[i]==1 && a[i-1]==0)
  38. b[k]=1;
  39. k++;
  40. }
  41.  
  42. /* printing the gray code */
  43. printf("\nThe gray code of the given number is :");
  44. for(i=0;i<8;i++)
  45. printf("%d",b[i]);
  46. }
  47.  
0
cscgal cscgal is offline Offline | Apr 27th, 2005
What is grey code?
 
 

Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC