program to print pascal triangle
import java.util.Scanner;
public class PascalsTriangleJava
{
static int findFactorial(int number)
{
int factorial;
for(factorial = 1; number > 1; number--)
{
factorial *= number;
}
return factorial;
}
// here's the function to display pascal's triangle
static int printPascalTraingle(int num, int p)
{
return findFactorial(num) / (findFactorial(num - p) * findFactorial(p));
}
public static void main(String[] args)
{
int row, a, b;
System.out.println("Please enter number of rows: ");
Scanner sc = new Scanner(System.in);
row = sc.nextInt();
System.out.println("Here's is pascal's triangle: ");
for(a = 0; a < row; a++)
{
for(b = 0; b < row - a; b++)
{
System.out.print(" ");
}
for(b = 0; b <= a; b++)
{
System.out.print(" " + printPascalTraingle(a, b));
}
System.out.println();
}
sc.close();
}
}
4
1
#include <stdio.h>
int main() {
int rows, coef = 1, space, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 0; i < rows; i++) {
for (space = 1; space <= rows - i; space++)
printf(" ");
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
Thank you!
1
0
Are there any code examples left?
New code examples in category Pascal
-
Pascal 2022-03-04 02:30:03 pascal special characters
-
Pascal 2022-01-31 12:07:02 comment in pascal
-
Pascal 2021-11-17 05:12:13 pascal comments
-
Pascal 2021-11-17 00:52:17 pascal modifiers
-
Pascal 2021-11-05 05:21:10 pascal cheat sheet pdf
-
Pascal 2021-10-05 12:04:04 pascal cheat sheet for programmers
-
Pascal 2021-10-01 16:03:02 comments in pascal
-
Pascal 2021-09-29 20:08:02 Kth Row Of Pascal's Triangle
-
Pascal 2021-09-20 13:21:06 pascal online compiler