Discussion: Need help in solving pascal triangle problem
In the previous quiz, C++ online test, we tested our experience gained from the course.
2 messages from 2 displayed.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
In the previous quiz, C++ online test, we tested our experience gained from the course.
Hi!
I tried googling a bit ( https://www.geeksforgeeks.org/…al-triangle/ ) and got this result, with a decent explanation:
#include <stdio.h>
// See https://www.geeksforgeeks.org/archives/25621
// for details of this function
int binomialCoeff(int n, int k);
// Function to print first
// n lines of Pascal's
// Triangle
void printPascal(int n)
{
// Iterate through every line and
// print entries in it
for (int line = 0; line < n; line++)
{
// Every line has number of
// integers equal to line
// number
for (int i = 0; i <= line; i++)
printf("%d ",
binomialCoeff(line, i));
printf("\n");
}
}
// See https://www.geeksforgeeks.org/archives/25621
// for details of this function
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;
}
// Driver program
int main()
{
int n = 7;
printPascal(n);
return 0;
}
Hope I helped!
2 messages from 2 displayed.