Write a to print a Pascal triangle in Java..
import java.util.*;
public class Pascal
{
static int Fact(int n)
{
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}
public static void main(String a[])
{
int l,k;
Scanner sc= new Scanner(System.in); //THIS THE SCANNER CLASS FOR USE THE SCAN VALUE FROM THE KEYBORD....
System.out.println("Enter the no. Of line");
l=sc.nextInt();
for(int i=0;i<=l;i++)
{
for(int j=0;j<=i;j++)
{
k=(Fact(i))/(Fact(j)*Fact(i-j));
System.out.print(k+" ");
}
System.out.println("\n\n");
}
}
}
Run this program on cmd ..
javac pascal.java
java pascal
The Output is....
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
import java.util.*;
public class Pascal
{
static int Fact(int n)
{
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}
public static void main(String a[])
{
int l,k;
Scanner sc= new Scanner(System.in); //THIS THE SCANNER CLASS FOR USE THE SCAN VALUE FROM THE KEYBORD....
System.out.println("Enter the no. Of line");
l=sc.nextInt();
for(int i=0;i<=l;i++)
{
for(int j=0;j<=i;j++)
{
k=(Fact(i))/(Fact(j)*Fact(i-j));
System.out.print(k+" ");
}
System.out.println("\n\n");
}
}
}
Run this program on cmd ..
javac pascal.java
java pascal
The Output is....
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
No comments:
Post a Comment