• Write a method called generateTriangleNumbers(). This method will take in an integer x and will return an array of integers containing the first x triangle numbers. The nth triangle number is the sum of 1 + 2 + 3 + 4...(n − 1) + n. o generateTriangleNumbers(3) returns the array {1,3,6} o generateTriangleNumber(1) returns the array {1} o generateTriangleNumbers(7) returns the array {1, 3, 6, 10, 15, 21, 28}

Respuesta :

Answer:

Check the explanation

Explanation:

//GenerateTriangleNumbers.java

import java.util.Arrays;

import java.util.Scanner;

public class GenerateTriangleNumbers {

   public static int[] generateTriangleNumbers(int x){

       int arr[] = new int[x];

       int sum = 0;

       for(int i = 1;i<=x;i++){

           sum += i;

           arr[i-1] = sum;

       }

       return arr;

   }

   public static void main( String [] args ){

       int n;

       Scanner in = new Scanner(System.in);

       System.out.print("Enter n: ");

       n = in.nextInt();

       System.out.println(Arrays.toString(generateTriangleNumbers(n)));

   }

}

Kindly check the attached image below for the code output.

Ver imagen temmydbrain

Answer:

See explaination

Explanation:

/GenerateTriangleNumbers.java

import java.util.Arrays;

import java.util.Scanner;

public class GenerateTriangleNumbers {

public static int[] generateTriangleNumbers(int x){

int arr[] = new int[x];

int sum = 0;

for(int i = 1;i<=x;i++){

sum += i;

arr[i-1] = sum;

}

return arr;

}

public static void main( String [] args ){

int n;

Scanner in = new Scanner(System.in);

System.out.print("Enter n: ");

n = in.nextInt();

System.out.println(Arrays.toString(generateTriangleNumbers(n)));

}

}