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);
}
}
}
}
Answer:
Pascal triangle in C
#include <stdio.h>
long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
for (c = 0; c <= (n - i - 2); c++)
printf(" ");
for (c = 0 ; c <= i; c++)
printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
printf("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}
Thanks they thiyo bhai