pascal's triangle java 2d array
import java.util.Scanner;
class Pascal_Triangle
{//opening of class
public static void main(String args[])
{//opening of main
Scanner sc=new Scanner(System.in);
int n,i,j,a[][],s;
//taking user's input.
System.out.println("HOW MANY STEPS?");
n=sc.nextInt();
s=n-1;
a=new int[n][n];
//filling the 2D matrix.
for(i=0;i<n;i++){
for(j=0;j<=i;j++)
if(j==0 || j==i)
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
//displaying the Pascal's Triangle as the output.
System.out.println("\nOUTPUT:\n");
for(i=0;i<n;i++)
{
for(j=0;j<=s;j++)
System.out.print(" ");//printing blank spaces at the beginning of rows
s--;
for(j=0;j<=i;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}//clossing of main
}//closing of classimport java.util.Scanner;
class Pascal_Triangle
{//opening of class
public static void main(String args[])
{//opening of main
Scanner sc=new Scanner(System.in);
int n,i,j,a[][];
//taking user's input.
System.out.println("HOW MANY STEPS?");
n=sc.nextInt();
a=new int[n][n];
//filling the 2D matrix.
for(i=0;i<n;i++){
for(j=0;j<=i;j++)
if(j==0 || j==i)
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
//displaying the Pascal's Triangle as the output.
System.out.println("\nOUTPUT:\n");
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}//clossing of main
}//closing of class
Also in Java:
- parsedouble java
- java clear console
- java split string
- how to make stage dragable in javafx
- how to get a user to add to an arraylist in java
- How to find the length of an array in java
- spigot respawn player location
- java long to int
- Java array multidimensional hashcode
- Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b, n are user inputs. For example, if a = 2, b = -12, n = 4 are entered the method should print or return
- how to create a linked list in java
- iterate through hashtable java
- creating a program about user asked to enter a number and enters 0 to stop, then calculates the sum of numbers entered, the average, the length of positive numbers and negative numbers entered by the user in java
- from file to array java
- loop through java object
- creating thread in java example
- processing sound android
- creating file in specific location in java application
- java classes and methods simple logic with comments
- how to detect device javascirpt
- java int
- convert hashset to int array java
- esponente in java
- java set value of arraylist