generate n-bit gray code in c

#include <stdio.h>

// Function to generate n-bit gray code
void generateGrayCode(int n) {
    // Base case
    if (n <= 0) {
        return;
    }

    // Initialize the gray code sequence with 0 and 1
    printf("0\n");
    printf("1\n");

    // Each iteration generates 2^(n-1) gray codes from the previous iteration
    int count, i, j;
    for (i = 2; i < (1 << n); i = i << 1) {
        // Generate the gray codes for the current iteration
        for (j = i - 1; j >= 0; j--) {
            printf("%d\n", i + j);
        }
    }
}

int main() {
    int n;
    printf("Enter the number of bits: ");
    scanf("%d", &n);

    // Generate the n-bit gray code
    generateGrayCode(n);

    return 0;
}