GoogleTag

Google Search

[Programming] Question 33: Write a Program to return the nth row of Pascal's triangle

 

#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.

 

Featured Posts

SQL Interview Questions Topics

 SQL Topics to prepare for interviews,   SQL Basics: Introduction to SQL SQL Data Types DDL (Data Definition Language): C...

Popular Posts