pascal's triangle java 2d array
Java
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:
- Title
- how to print a 2d array in java
- Category
- Java
- Title
- how to get token oauth2.0 java example response
- Category
- Java
- Title
- abs in java
- Category
- Java
- Title
- reading string after double in java
- Category
- Java
- Title
- hashmaps java
- Category
- Java
- Title
- reverse number in java
- Category
- Java
- Title
- iterate through hashtable java
- Category
- Java
- Title
- java find if element of list in present in another list
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- java double 2 decimal
- Category
- Java
- Title
- java shuffle list
- Category
- Java
- Title
- java foreach
- Category
- Java
- Title
- java android edit text set value
- Category
- Java
- Title
- java console text color
- Category
- Java
- Title
- annotation spring notnull
- Category
- Java
- Title
- java "->"
- Category
- Java
- Title
- java hashmap example
- Category
- Java
- Title
- thymeleaf Expression Object dialects
- Category
- Java
- Title
- how to check the lines in a file java scanner
- Category
- Java
- Title
- could not initialize defaultkotlinsourcesetkt intelij
- Category
- Java
- Title
- uppercase java
- Category
- Java
- Title
- int java
- Category
- Java
- Title
- java how to call getReader twice
- Category
- Java
- Title
- link to method javadoc
- Category
- Java
- Title
- bootstrap button bigger
- Category
- Java
- Title
- color from hex code flutter
- Category
- Java
- Title
- java string to integer
- Category
- Java
- Title
- convert date to calendar java
- Category
- Java
- Title
- load contents of file into string java
- Category
- Java
- Title
- create object of static class in java
- Category
- Java
- Title
- java script how to not allow soace
- Category
- Java
- Title
- remove first character from string
- Category
- Java
- Title
- 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
- Category
- Java
- Title
- get host from request object java
- Category
- Java
- Title
- java fx display simple rectangle
- Category
- Java
- Title
- formartted string java
- Category
- Java
- Title
- loop through array java
- Category
- Java
- Title
- java insert into arraylist
- Category
- Java
- Title
- how to interrupt a void java
- Category
- Java
- Title
- determine if a given binary tree is a valid bst
- Category
- Java
- Title
- java connect to mysql
- Category
- Java
- Title
- java stack methods
- Category
- Java
- Title
- converting char array to string
- Category
- Java
- Title
- java setter
- Category
- Java
- Title
- expression régulière téléphone java
- Category
- Java
- Title
- replace regex group() java
- Category
- Java
- Title
- remove item from arraylist in java
- Category
- Java
- Title
- java djikstra's algorithm
- Category
- Java
- Title
- string to arraylist convert java
- Category
- Java
- Title
- how to declare list of object in java as constant
- Category
- Java
- Title
- how to do substring java
- Category
- Java
- Title
- sqrt in java
- Category
- Java
- Title
- java xml element get attribute value
- Category
- Java
- Title
- same method name with different arguments
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- java loop hashmap
- Category
- Java
- Title
- initialize arraylist
- Category
- Java
- Title
- java swing change label icon
- Category
- Java
- Title
- remove last character from string java
- Category
- Java
- Title
- java read file text
- Category
- Java
- Title
- teimpo en segundos java
- Category
- Java
- Title
- get long from date java
- Category
- Java
- Title
- how to make an arraylist java
- Category
- Java
- Title
- android java shared preferences remove key
- Category
- Java
- Title
- list in java
- Category
- Java
- Title
- string method example in java
- Category
- Java
- Title
- how to remove a certain string in a arraylist java
- Category
- Java
- Title
- java function without return
- Category
- Java
- Title
- java import
- Category
- Java
- Title
- primitive and non primitive data types in java
- Category
- Java
- Title
- como saber de que tipo es una variable en java
- Category
- Java
- Title
- java empty array
- Category
- Java
- Title
- java builder pattern example
- Category
- Java
- Title
- one line if statement java
- Category
- Java
- Title
- java create a set with values
- Category
- Java
- Title
- summary of operator java
- Category
- Java
- Title
- setbackground java
- Category
- Java
- Title
- array null pointer java
- Category
- Java
- Title
- java sort method
- Category
- Java
- Title
- summary of operators java
- Category
- Java
- Title
- how to check if an arraylist contains a value in java recursion
- Category
- Java
- Title
- java 8 find in list by property
- Category
- Java
- Title
- java method
- Category
- Java
- Title
- how to get the dimensions of a 2d array in java
- Category
- Java
- Title
- java check if file exists
- Category
- Java
- Title
- function in java
- Category
- Java
- Title
- check if string is null or empty java
- Category
- Java
- Title
- How to chage font progrmatically
- Category
- Java
- Title
- java get current milliseconds
- Category
- Java
- Title
- how to write sensor data into file android studio
- Category
- Java
- Title
- convert from integer to character java
- Category
- Java
- Title
- how to create a method java
- Category
- Java
- Title
- multiple string java
- Category
- Java
- Title
- find the triplet sum in java linked list
- Category
- Java
- Title
- prime factorization java
- Category
- Java
- Title
- how to declare string array in java
- Category
- Java
- Title
- java math.random
- Category
- Java
- Title
- system.out.println shortcut
- Category
- Java
- Title
- find length of array java
- Category
- Java
- Title
- jtable font size
- Category
- Java