Python/C/C++/JAVA

Moderate Practice Programs with Code and Concept

By D.S

Pascal's Traingle

If you want to write a program that displays Pascal's Triangle, it's actually not too complicated. First off, you'll need to understand what Pascal's Triangle is - essentially, it's a geometric arrangement of binomial coefficients in a triangle. Each number is the sum of the two directly above it. Once you've got that down pat, you can start coding. Typically this involves using loops and variables to generate each row of the triangle until your program reaches the specified number of rows. Whether you're working in a language like Python or C++, there are plenty of resources out there with sample code and tutorials to help guide you through the process. All in all, writing a Pascal's Triangle program can be a fun challenge that pays off with some interesting mathematical patterns!

(a.) C Code

#include <stdio.h>
      int binomialCoefficient(int n, int k) {
        if (k == 0 || k == n) 
            return 1;
        return binomialCoefficient(n - 1, k - 1) + binomialCoefficient(n - 1, k);
    }
void printPascalTriangle(int numRows) {
      for (int i = 0; i < numRows; i++) {
          for (int j = 0; j <= i; j++) {
              printf("%d ", binomialCoefficient(i, j));
          }
          printf("
");
      }
  }
int main() {
  int numRows = 5;
  printPascalTriangle(numRows);
  return 0;
}
Output:-
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

(b.) C++ Code

#include <iostream>
using namespace std;

int binomialCoefficient(int n, int k) {
  if (k == 0 || k == n) 
      return 1;
  return binomialCoefficient(n - 1, k - 1) + binomialCoefficient(n - 1, k);
}

void printPascalTriangle(int numRows) {
  for (int i = 0; i < numRows; i++) {
      for (int j = 0; j <= i; j++) {
          cout << binomialCoefficient(i, j) << " ";
      }
      cout << endl;
  }
}
int main() {
  int numRows = 5;
  printPascalTriangle(numRows);
  return 0;
}
Output:-
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

(c.) Python Code

def binomial_coefficient(n, k):
      if k == 0 or k == n:
          return 1
      return binomial_coefficient(n - 1, k - 1) + binomial_coefficient(n - 1, k)
  
  def print_pascal_triangle(num_rows):
      for i in range(num_rows):
          for j in range(i + 1):
              print(binomial_coefficient(i, j), end=" ")
          print()
  
  num_rows = 5
  print_pascal_triangle(num_rows)
Output:-
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

(d.) Java Code

public class PascalTriangle {
      static int binomialCoefficient(int n, int k) {
          if (k == 0 || k == n)
              return 1;
          return binomialCoefficient(n - 1, k - 1) + binomialCoefficient(n - 1, k);
      }
  
      static void printPascalTriangle(int numRows) {
          for (int i = 0; i < numRows; i++) {
              for (int j = 0; j <= i; j++) {
                  System.out.print(binomialCoefficient(i, j) + " ");
              }
              System.out.println();
          }
      }
  
      public static void main(String[] args) {
          int numRows = 5;
          printPascalTriangle(numRows);
      }
  }
Output:-
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1