Pyramid of numbers

Need to bring out a pyramid

             1
          3     5
       7     9    11
   13    15    17    19
21    23    25    27    29

I think it needs to be composed using a two-dimensional array, but I can’t. I would be glad if someone helps to compile the code. In the code below, I tried to make a pyramid, but with other numbers everything turned out very clumsy, and as a result, even the pyramid did not work

int[][] primed = new int[4][];
    primed[0] = new int[1];
    primed[1] = new int[2];
    primed[2] = new int[3];
    primed[3] = new int[4];
    int k = 0, i, j;
    for (i = 0; i < 4; i++)
        for (j = 0; j < i + 1; j++) {
            primed[i][j] = k;
            k++;
        }
    for (i = 0; i < 4; i++){
        for (j = 0; j < i + 1; j++)
            System.out.print(primed[i][j] + "  ");
        System.out.println();



    }
}

}

Asked on December 17, 2019 in Programming.
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.