Pascal triangle java program
Written by
Java Program to print Pascal’s Triangle
Here, we are supposed to create a source code that prints the Pascal Triangle using any character that is given as input from the user. The Program thus prints Pascal Triangle out of that character.
The basic idea is to first implement the logic of Pascal Triangle as usual and replace the Loop Control Variable that originally prints, with the input Character.
Java Code:
/* Program to print Inverted Pascal Triangle*/
import java.util.*;
class PascalTriangle
{
public static void main()
{
Scanner inp=new Scanner(System.in);
System.out.print("\n Enter Any Character: ");
char c=((inp.nextLine()).charAt(0));
System.out.print("\n Enter Size Limit: ");
int n=inp.nextInt();
int i,j,k,x=1;
System.out.println("Pascal Triangle: \n");
for(i=0;i<n;i++)
{
for(j=n;j>i;j--)
{
System.out.print(" ");
}
x=1;
for(j=0;j<=i;j++)
{
System.out.print(c+" ");
x=x*(i-j)/(j+1);
}
System.out.println();
}
}
}
Output:
Enter Any Character: @
Enter Size Limit: 5
Pascal Triangle:
@
@ @
@ @ @
@ @ @ @
@ @ @ @ @
Enter Any Character: 5
Enter Size Limit: 5
Pascal Triangle:
5
5 5
5 5 5
5 5 5 5
5 5 5 5 5