Programming Problems - Number Pattern

Observe the pattern of numbers given below.

      1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1
 1 5 10 10 5 1
1 6 15 20 15 6 1


The pattern has the following properties.

  1. Assuming that the row and the column numbers start from 1, the number of columns in a row is equal to the row number. (No. of Columns = RowNum).
  2. In a row, the value of the first and last columns are always 1.
  3. In any row, the value of a cell is the sum of its adjacent cells in the previous row. CellVal(RowNum, ColNum) = CellVal(RowNum - 1, ColNum - 1) + CellVal(RowNum - 1, ColNum)

The problem is to find the cell value, given a row number and a column number.

NumberPattern

Implement the class NumberPattern with the following public method.

public int getCellVal(int nRowNum, int nColNum);

I strongly urge the readers to make a sincere attempt in implementing the solution. I am providing my implementation so that you can compare it with yours. Looking straightaway at my solution may not be as helpful as making a physical attempt to solve the problem even if your solution is incomplete or not perfect.

You could also carry your source code in a public repository hosted on GitHub and if you prefer you could share the link to your source code in the comment section so that other readers could take a look at it and provide their comments.

Please provide your support and express your encouragement by using the social buttons provided below and share this effort with others.

The solution to this exercise is given in the next exercise.


Post a Comment

Subscribe through Email