C Program to Convert Binary Code of a Number into its Equivalent Gray’s Code using Recursion

C Program to Convert Binary Code of a Number into its Equivalent Gray’s Code using Recursion.

  1. #include <stdio.h>
  2.  
  3. int bintogray(int);
  4.  
  5. int main ()
  6. {
  7.     int bin, gray;
  8.  
  9.     printf("Enter a binary number: ");
  10.     scanf("%d", &bin);
  11.     gray = bintogray(bin);
  12.     printf("The gray code of %d is %d\n", bin, gray);
  13.     return 0;
  14. }
  15.  
  16. int bintogray(int bin)
  17. {
  18.     int a, b, result = 0, i = 0;
  19.  
  20.     if (!bin)
  21.     {
  22.         return 0;
  23.     }
  24.     else
  25.     {
  26.         a = bin % 10;
  27.         bin = bin / 10;
  28.         b = bin % 10;
  29.         if ((a && !b) || (!a && b))
  30.         {
  31.             return (1 + 10 * bintogray(bin));
  32.         }
  33.         else
  34.         {
  35.             return (10 * bintogray(bin));
  36.         }
  37.     }
  38. }

 

Comments

Popular posts from this blog

C Program to Display the ATM Transaction

Java programming language

Python Program to Print all Prime Numbers in an Interval