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

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

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

 

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