Pascal's Triangle in Java
Pascal's Triangle Program in Java
Program and Video given below :-
import java.io.*;
import java.util.*;
class Pascal
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int i,j,k,p,n;
System.out.println("Enter Number of rows");
n = in.nextInt();
int m[]= new int[n*10];
p=n;
m[0]=1;
for(i=0;i<n;i++)
{
for(k=p;k>=1;k--)// printing whitespaces
{
System.out.print(" ");
}
for(j=0;j<=i;j++)// printing numbers
{
System.out.print(m[j]+" ");
}
System.out.println();
p--;
for(j=i+1;j>0;j--)// updating values
{
m[j]=m[j]+m[j-1];
}
}
}// end of main method
}// end of class
import java.util.*;
class Pascal
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int i,j,k,p,n;
System.out.println("Enter Number of rows");
n = in.nextInt();
int m[]= new int[n*10];
p=n;
m[0]=1;
for(i=0;i<n;i++)
{
for(k=p;k>=1;k--)// printing whitespaces
{
System.out.print(" ");
}
for(j=0;j<=i;j++)// printing numbers
{
System.out.print(m[j]+" ");
}
System.out.println();
p--;
for(j=i+1;j>0;j--)// updating values
{
m[j]=m[j]+m[j-1];
}
}
}// end of main method
}// end of class
For Complete explanation watch the video :-
Watch the video : Pascal's Triangle
Follow me on Instagram
All the best :)
Keep Learning :)
Comments
Post a Comment