Users Online

· Guests Online: 139

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

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

This C program using recursion, evaluates the gray code equivalent of a binary number. A gray is also represented using 0s and 1s. The speciality of gray code is that only one bit is changed in 2 consecutive numbers, say 3 and 4.
Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
* C Program to Convert Binary Code of a Number into its Equivalent
* Gray's Code using Recursion
*/
#include <stdio.h>

int bintogray(int);

int main ()
{
int bin, gray;

printf("Enter a binary number: ");
scanf("%d", &bin);
gray = bintogray(bin);
printf("The gray code of %d is %d\n", bin, gray);
return 0;
}

int bintogray(int bin)
{
int a, b, result = 0, i = 0;

if (!bin)
{
return 0;
}
else
{
a = bin % 10;
bin = bin / 10;
b = bin % 10;
if ((a && !b) || (!a && b))
{
return (1 + 10 * bintogray(bin));
}
else
{
return (10 * bintogray(bin));
}
}
}

$ cc pgm21.c
$ a.out
Enter a binary number: 1011101
The gray code of 1011101 is 1110011

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 1.07 seconds
10,815,811 unique visits