write a program to print Pascal's triangle. ..15 rows.
Answers
/***********************************
* Pascal's triangle *
***********************************/
/**
* Pascal's pattern is generated from the top of the pyramid i.e. first row.
* Triangle is a perfectly symmetrical mirror image having a line down through the middle innermost digits.
* Code created by Mahnaz Hazra
**/
import java.util.*;
public class Main //Or Public class PascalTriangle
{
public static void main(String[] args)
{
Scanner Scn = new Scanner(System.in);
int mn = Scn.nextInt() + 1;
if(mn > 15)
{
System.out.println("Note : The neatnest of the format fails after 15.");
}
System.out.println(); //Print a new line
for(int n = 0; n < mn; n++)
{
System.out.format("%n");
for(int i = mn-n; i > 0; i--)
{
System.out.format("%3s", "");
}
System.out.format("1");
int t = 1;
for(int i = 1;i <= n; i++)
{
t = (t * (n +1 - i) ) /i;
System.out.format(" %5d", t);
}
}
}
}
Explanation:
C program to print pascal’s triangle
#include
int main()
{
int rows, coef = 1, space, i, j;
printf(“\nEnter the number of rows : “);
scanf(“%d”,&rows);
printf(“\n”);
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\n”);
}
return 0;
}