1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ICE Princess25 [194]
3 years ago
15

Pascal's Triangle is a triangular array in which every number represents the

Computers and Technology
1 answer:
Viefleur [7K]3 years ago
6 0

Answer:

I am writing a JAVA program.

import java.util.Scanner;  // this class is used here to take input from user

import java.util.ArrayList;    // used to create dynamic arrays  

public class PascalTriangle{   // class to print nth line of pascal's triangle

/* function line() that takes the line index (line_no) as parameter and finds the nth line (find line at given index line_no) in the Pascal's Triangle */

   public static ArrayList<Integer> line(int line_no) {  

//declares ArrayList of integers

       ArrayList<Integer> current_line = new ArrayList<Integer>();  

       current_line.add(1);    // sets the element of every line to  1      

/* if conditions checks if the input n-th line is the first line which means it checks if the line to be returned is the 1st one */

       if (line_no == 0) {

       return current_line;  //returns the first line/row}  

/* the following statement produces the previous line by passing the index position (line_no-1) of that line as a parameter of line() method */    

       ArrayList<Integer> previous = line(line_no - 1);    

/* the loop has i variable which is initialized to 1 and this loop continues to execute until the value of i gets greater than or equal to the size of previous line */

       for (int i = 1; i < previous.size(); i++) {  //start of the loop

/* the following statement computes the current line based on the previous line of the Pascal triangle and this is a recursive part. This will compute the elements of the current line based on previous line and store these elements in present*/

           int present = previous.get(i - 1) + previous.get(i);  

//adds the elements computed by the above line to the current_line. this means it adds each element of present to current_line. For example if the n-th  line is 5 then it the elements at 6th row of pascals triangle are found and returned considering the topmost line the "0th" line */

           current_line.add(present); }  

       current_line.add(1);  

     return current_line; } //returns current line i.e. n-th line of pascal triangle  

   public static void main(String[] args)     {  //start of main() function body

      Scanner scan = new Scanner(System.in);  // create Scanner class object

/* prompts user to enter integer n as input to return the nth line of Pascal's triangle */

       System.out.print("Enter·line·of·Pascal's·Triangle·to·print: ");

       int n = scan.nextInt();    //scans and reads the input integer

// calls line function to return the n-th line of Pascal triangle

       ArrayList<Integer> array = line(n);  

       for (int i = 0; i < array.size(); i++) {  /* loop iterates through the array until the loop variable i gets equal to or exceeds the array size in order to display the elements of the n-th line of pascal triangle where n is integer input */

    System.out.print(array.get(i)+ "."); }    }  } //prints the elements of n-th line

/* get() method gets the elements of nth line and . is used to seperate each element with a dot. */

Explanation:

The program is well explained in the comments mentioned with each statement of the program. The program has a function line() that takes as argument the index line_no which is the nth line of Pascal's triangle whose elements are to be displayed. The program computes the line of the previous line number (index) first using recursion. After this the values of the current line is computed with the help of the previous one. This is iterated using a loop until the nth line. In the main() method user is prompted to enter integer n and then line() method is called by passing the n integer to the function to display the elements of this n-th line in pascal's triangle. loop is used to iterate through the entire array and get() method gets the elements of nth line of array and . is used to separate each element with a dot. The program and output is attached in a screenshot.

You might be interested in
I need Help!!<br> 4. Explain the importance of including negative space as part of a design.
vova2212 [387]
Negative space is the space between, within and surrounding an object in an image. The positive space is the focus of the image, the object itself, but the negative space is just as important. It shares edges with the positive space, defining the outline of the object and creating proportion
5 0
3 years ago
In Java an abstract class cannot be sub-classed<br><br> ?? True<br><br> ?? False
mojhsa [17]

Answer:

False

Explanation:

An abstract class is a class declared abstract — it may or may not include abstract techniques. It is not possible to instantiate abstract classes, but they can be sub-classed.

<u></u>

<u>Abstract method declaration</u>

             

         abstract void moveTo(double X, double Y);

Usually the subclass offers solutions for all of the abstract techniques in its parent class when an abstract class is sub-classed. If not, however, the subclass must be declared abstract as well.

<u>Example</u>

public abstract class GraphicObject {

  // declaring fields

  // declaring non-abstract methods

  abstract void draw();

}

8 0
3 years ago
Which. Option tilts the image both horizantly and vertically be degrees
Anit [1.1K]
Aaaaaaaeeeeeeeeiiiiiioooooouuuuuu
3 0
3 years ago
An organization is using Amazon CloudWatch Logs with agents deployed on its Linux Amazon EC2 instances.
Bumek [7]

Answer:

A and B

Explanation:

EC2 run command gives a management output to review various instances and servers. Here, the logging files, collecting metrics and patches, running script installations can be achieved.

Verify the user permission and the run command, confirms if whether or not the problem of missing logging files is from the settings.

There are several AWS and customer managed policies for cloudwatch logs to give users certain levels for permission. Verifying this determines if there is full access, group logs and creation of group log events, or even a read only access.

4 0
3 years ago
PLEASE HELP ASAP what is the main purpose of electronic speed controller ESC
iragen [17]

Answer:

<em><u>An electronic speed control (ESC) is an electronic circuit that controls and regulates the speed of an electric motor. It may also provide reversing of the motor and dynamic braking.</u></em>

Explanation:

<h3><em>I </em><em>hope</em><em> this</em><em> helps</em><em>!</em></h3>
4 0
2 years ago
Read 2 more answers
Other questions:
  • Who would like to join a team devoted to decreasing spam and bullying?
    5·1 answer
  • Where does the VLookup function find its lookup values?
    14·1 answer
  • The Left Shift key is used to capitalize which keys?
    5·2 answers
  • Crashing almost always accelerates the schedule while increasing project risk, whereas fast tracking almost always accelerates t
    10·1 answer
  • What are paragraphs separated by
    9·1 answer
  • a corporation needs an operating system that allows the various teams in its office to network &amp; collaborate on projects. wh
    11·2 answers
  • In the program below, which two variables have the same scope?
    9·2 answers
  • What is a trojan horse
    8·2 answers
  • Have some points part 6<br>anyone know how to change my username?​
    11·2 answers
  • Need help plz 100 POINTS
    10·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!