Help me in solving BMCCP02 problem

My issue

My code

//Solution as follows
#include <stdio.h>

int main() {
    int t,s;
    scanf("%d", &t);

    for (int s=0 ; s<t ; s++){
        int N;
        scanf("%d", &N);

        int final_ans[7];
        int j = 0;

        for (int i=0 ; i<t ;i++){
            final_ans[j] = N % 10;
            N = N / 10;
            j++;
        }

        for(int i = j-1; i >= 0; i++) {
            printf("%d ", final_ans[i]);
        }

        printf("\n");
    }
    return 0;
}

Learning course: C for problem solving - 2
Problem Link: CodeChef: Practical coding for everyone

Hi @shiva12239381

  1. for (int i=0 ; i<t ;i++){ - this section of the code is incorrect. you are comparing i with t - the test cases. You ideally want to run a loop which helps you identify each digit of N - while (N > 0) or corresponding for loop syntax will make more sense

  2. for(int i = j-1; i >= 0; i++) { - here it should be ‘i–’, You are reducing i from the value (j-1) to 0.
    i++ leads to an infinite loop - hence you were getting SIGSEGV error
    The error message “Signal: SIGSEGV” means that the program has encountered a segmentation fault, which occurs when the program tries to access memory that it is not allowed to access.

thank you !