Generate Pascal's Triangle with For loops in R
Write a R program to generate the Pascal's triangle up to a given number of rows using nested for loops.
Sample Solution :
R Programming Code :
# Define a function to generate Pascal's triangle up to a given number of rows
generate_pascals_triangle <- function(num_rows) {
    # Initialize an empty list to store the triangle
    pascals_triangle <- list()
    
    # Iterate through rows
    for (row in 1:num_rows) {
        # Initialize a list to store the current row
        current_row <- c()
        
        # Iterate through columns in the current row
        for (col in 1:row) {
            # Calculate the value for the current cell using binomial coefficient formula
            value <- choose(row - 1, col - 1)
            
            # Append the value to the current row
            current_row <- c(current_row, value)
        }
        
        # Append the current row to the Pascal's triangle list
        pascals_triangle <- append(pascals_triangle, list(current_row))
    }
    
    return(pascals_triangle)
}
# Define a function to print Pascal's triangle
print_pascals_triangle <- function(pascals_triangle) {
    for (row in pascals_triangle) {
        cat(row, "\n")
    }
}
# Test the functions with an example input
num_rows <- 7
pascals_triangle <- generate_pascals_triangle(num_rows)
cat("Pascal's Triangle up to", as.character(num_rows), "rows:\n")
print_pascals_triangle(pascals_triangle)
Output:
Pascal's Triangle up to 7 rows: 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
Explatnaion:
In the exercise above,
- generate_pascals_triangle function:
 - This function generates Pascal's triangle up to a given number of rows.
 - It takes one parameter 'num_rows', which represents the number of rows in the triangle.
 - Inside the function, it initializes an empty list 'pascals_triangle' to store the triangle.
 - It iterates through each row from 1 to 'num_rows'.
 - For each row, it initializes an empty list 'current_row' to store the values of that row.
 - It iterates through each column in the current row using another loop.
 - For each cell in the current row, it calculates the value using the binomial coefficient formula (choose function) and appends it to 'current_row'.
 - After completing the row, it appends 'current_row' to 'pascals_triangle'.
 - Finally, it returns the generated Pascal's triangle.
 - print_pascals_triangle function:
 - This function is responsible for printing the Pascal's triangle neatly.
 - It takes one parameter 'pascals_triangle', which is the Pascal's triangle generated by "generate_pascals_triangle()" function.
 - It iterates through each row in 'pascals_triangle' and prints it.
 - Test the functions:
 - An example input num_rows is defined as 7.
 - The generate_pascals_triangle function is called with num_rows to generate the Pascal's triangle.
 
Go to:
PREV : Find GCD with Recursion in R.
NEXT : Calculate Factorial with Recursion & Iteration in R.
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
