#include <stdio.h> int binomialCoeff(int n, int k) { int res = 1; if (k > n - k) k = n - k; for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } int main() { int n; printf("Enter the row number: "); scanf("%d", &n); for (int i = 0; i < n; ++i) { printf("%d ", binomialCoeff(n - 1, i)); } return 0; }
Inside the binomialCoeff function:
- This function calculates the binomial coefficient C(n, k) using the formula C(n, k) = n! / (k! * (n - k)!).
- Inside the main function:
- The user is prompted to enter the row number.
- A for loop iterates from 0 to n - 1, and in each iteration, it prints the binomial coefficient for that row.