Write C program to print the following pattern on the output
1.Floyd's Triangle
2.Pascal Triangle
1
3.Multiplication Table
4. 1
2 3
4 5 6
Pascal Triangle:
1.Floyd's Triangle
1
2 3
4 5 6
7 8 9 102.Pascal Triangle
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
1 7 21 35 35 21 7 1
3.Multiplication Table
4. 1
2 3
4 5 6
Pascal Triangle:
#include <stdio.h> unsigned long ncr(int n, int r) { static unsigned long table[256][256] = {0}; if(r == 0 || n == r) { return table[n][r] = 1; } if(r == 1) { return table[n][r] = n; } if(r > n / 2) { return table[n][r] = ncr(n, n - r); } return table[n][r] = table[n - 1][r - 1] + table[n - 1][r]; } void printPascal(int n) { int numSpaces = n; int i, j; for(i = 0; i <= n; ++i) { for(j = 0; j < numSpaces; ++j) printf(" "); for(j = 0; j <= i; ++j) printf("%lu ", ncr(i, j)); printf("\n"); numSpaces--; } } int main() { int n; scanf("%d", &n); printPascal(n); return 0; }
Floyd's triangle:
ReplyDelete-----------------
Floyd's triangle is a right angled-triangle using the natural numbers. Examples of floyd's triangle:
Example 1:
1
2 3
4 5 6
7 8 9 10
for(i=1;i < =r;i++){
for(j=1;j < =i;j++,k++)
printf(" %d",k);
printf("\n");
}
Pascal Triangle:
-----------------
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
1 7 21 35 35 21 7 1
for(i=0;i < line;i++){
for(j=0;j < line-i-1;j++)
printf(" ");
for(j=0;j < =i;j++)
printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
printf("\n");
}
Multiplication Table in C:
--------------------------
for(i=1;i < =r;i++){
for(j=1;j < =10;j++)
printf("%d*%d=%d ",i,j,i*j);
printf("\n");
}