-5

Using a 50 by 70, 2-Dimensional array, write a java program that draws the figure below.?(Just make use of the given length given) but i must look something like this below:

                             *      *
                           ****    ****
                          ******  ******
                 ************************
                  ******  ******      
                   ****    ****
                     *       *

I tried my code, but i am not that familiar with 2-D arrays in java

import java.util.*; public class triangle { public static void main(String[] args) { int height = 70; int x = 1, y = 70;

    int j;

  char triangularArray[][] = new char[50][70];
  for (int i = 0; i < height; i++){
    for(j=-i; j<=i; j++){
          triangularArray[x+i][y+j]='*';
        }
  }
  for(int i=0; i<triangularArray.length; i++) {
        for( j=0; j<triangularArray.length; j++) {
           System.out.println(triangularArray[i][j]==0 ? ' ' : '*');
        }
       System.out.println("\n");
    }
  }
}

1 Answers1

0

Without mathematics you can solve it as below :)) your choice...

// 1 4 6 8
String[] triangle = new String[]{"    *  ", "  ****  ", " ****** ", "********"};

for (int i = 0; i < triangle.length; i++)
{
    System.out.print(triangle[i]);
    System.out.print(triangle[i]);
    System.out.println();
}

for (int i = 2; i >= 0; i--)
{
    System.out.print(triangle[i]);
    System.out.print(triangle[i]);
    System.out.println();
}
sgpalit
  • 2,676
  • 2
  • 16
  • 22