convert binary code to gray code in premutation

I have a recursive function that shows all of the n-digits binary codes. and i want to change some codes in else part of the function to convert binary code into a graycode.

can anyone help me with this?

#include <iostream>

using namespace std;
int b[]={};

void nbool(int i,int n);
int main()
{
    int n;
    cin>>n;
    nbool(0,n);
    return 0;
}
void nbool(int i,int n)
{
    if(i==n)
    {
        for(int k=0;k<n;k++)
        {
            if(k%n==0)
            {
              cout<<endl;
            }

            cout<<b[k];
        }
    }
    else
    {
        b[i] = 0;
        nbool(i+1,n);
        b[i] = 1;
        nbool(i+1,n);

    }
}