Skip to content

Commit

Permalink
Hacktoberfest 2021
Browse files Browse the repository at this point in the history
added gray_code.c
  • Loading branch information
ishangoyal13 committed Oct 1, 2021
2 parents db3bfd3 + 3afe133 commit 8c1202a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions C/gray_code.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>
#define DEBUG(x); printf("%d------\n",x);
#define ll long long
int power(int a,int b){
int ans=1;
for(int i=0;i<b;i++)ans*=a;
return ans;
}
int main(){
int n,len;
printf("ENter the length for binary numbers\n");
scanf("%d",&n);
int end=power(2,n);
for(int k=0;k<end;k++){
int a[32]={0},num=k;
len=0;
while(num>0){
a[len++]=num%2;
num/=2;
}

for(int i=0;i<n-len;i++)printf("0");
if(len!=0)printf("%d",a[len-1]);
for(int i=len-1;i>0;i--){
printf("%d",(a[i]+a[i-1])%2);
}
printf("\n");
}
return 0;
}

0 comments on commit 8c1202a

Please sign in to comment.