pascals triangle java
Java
/*
Author: Jeffrey Huang
*/
import java.util.*;
public class PascalTriangleCreator
{
public static long factorial(long n){
/*
The whole purpose of this method is to find the factorial of a number,
since java does not have a built in method for it. Calculating n choose
r is done using factorial, and since this code will be used repeatedly,
it is wise to put it in a separate method.
*/
long factorial;
if (n==0){
factorial=1;
}
else{
factorial=1;
for (int counter=1;counter<=n;counter++){
factorial=factorial*counter;
}
}
return factorial;
}
public static long FinalValue(long n, long r){
//Calculates n choose r by calling the factorial method.
return factorial(n) / ( factorial(n-r) * factorial(r) );
}
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
long rows=1;
long i,j;
while (rows!=0){
System.out.println("How many rows of Pascal's triangle would you like to print? (0 to stop; 1-20 rows)");
rows=sc.nextLong();
//The following while loop ensures that the user cannot input an invalid number.
while (rows<0||rows>20){
System.out.println("Invalid input.");
System.out.println("How many rows of Pascal's triangle would you like to print? (0 to stop; 1-20 rows)");
rows=sc.nextLong();
}
/*
The following if else block makes the code more efficient. Otherwise, if the user
enters zero at any other point than at the start of the loop, the program will go
through the long process of trying to print a triangle before terminating the
program.
Using the following method, it is true that rows==0 is tested for twice, but
it shortens the execution time immensely. And we know that when zero is true
for the if statement, it is guaranteed to be true when breaking the loop.
*/
if (rows==0){
System.out.println("Program terminated by user.");
}
else{
for(i = 0; i < rows; i++) {
//Iterates through the number of rows required.
for(j = 0; j <= rows-i; j++){
System.out.print(" ");
//Iterates the printing of spaces.
}
for(j = 0; j <= i; j++){
if ((FinalValue(i, j))>9999) {
System.out.print(" ");
}
else if ((FinalValue(i, j))>999){
System.out.print(" ");
}
else if ((FinalValue(i, j))>99){
System.out.print(" ");
}
else if ((FinalValue(i, j))>9){
System.out.print(" ");
}
else{
System.out.print(" ");
}
System.out.print(FinalValue(i, j));
//Prints a number of spaces plus a number.
}
System.out.println();
}
}
}
sc.close();
}
}
Also in Java:
- Title
- java lerp
- Category
- Java
- Title
- login and logout react native and firebase
- Category
- Java
- Title
- substring java
- Category
- Java
- Title
- Fab to CircularRevealFrameLayout example
- Category
- Java
- Title
- arraylist with values java
- Category
- Java
- Title
- how to make an arraylist java
- Category
- Java
- Title
- use regex in if statement java
- Category
- Java
- Title
- android how to split string
- Category
- Java
- Title
- map with insertion order preserved java
- Category
- Java
- Title
- what is a float java
- Category
- Java
- Title
- como detener un void java
- Category
- Java
- Title
- java string regexp replace
- Category
- Java
- Title
- iterate map in java 8 using stream
- Category
- Java
- Title
- summary of operator java
- Category
- Java
- Title
- initialize arraylist
- Category
- Java
- Title
- linux command to see all the java version installed
- Category
- Java
- Title
- time conversion solution
- Category
- Java
- Title
- android cardview dependency
- Category
- Java
- Title
- check java version cmd
- Category
- Java
- Title
- displaying an arraylist in java
- Category
- Java
- Title
- java 8 stream group by example
- Category
- Java
- Title
- make a commet in java
- Category
- Java
- Title
- JOptionPane with drop down list java
- Category
- Java
- Title
- even or odd in java
- Category
- Java
- Title
- java how to print a newline
- Category
- Java
- Title
- jbutton remove selection border
- Category
- Java
- Title
- protect java
- Category
- Java
- Title
- are strings modifiable
- Category
- Java
- Title
- java execute for cycle parallel thread
- Category
- Java
- Title
- android switch on change
- Category
- Java
- Title
- java how to compare strings
- Category
- Java
- Title
- Java int to roman
- Category
- Java
- Title
- java manacher's algorithm
- Category
- Java
- Title
- spigot spawn firework
- Category
- Java
- Title
- java loop through object
- Category
- Java
- Title
- arrays.fill java
- Category
- Java
- Title
- iterate through hashmap in java
- Category
- Java
- Title
- java convert char to int
- Category
- Java
- Title
- bukkit java set leather armor color from hex
- Category
- Java
- Title
- java split for multiple characters
- Category
- Java
- Title
- enhanced for loop java
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- multiple string java
- Category
- Java
- Title
- FORM EN JAVA SCRIPT
- Category
- Java
- Title
- how to implement a interface in java
- Category
- Java
- Title
- how to send http post create request using curl command
- Category
- Java
- Title
- how to make int array java android
- Category
- Java
- Title
- Android recycleView
- Category
- Java
- Title
- how to compare string in java
- Category
- Java
- Title
- java script num toSting syntax eror
- Category
- Java
- Title
- java identifiers
- Category
- Java
- Title
- how to convert string to array in java
- Category
- Java
- Title
- java list contains object with property
- Category
- Java
- Title
- java get first char
- Category
- Java
- Title
- android how to know when snackbar is done
- Category
- Java
- Title
- how to select a element in an arraylist java
- Category
- Java
- Title
- how to append to an array in java
- Category
- Java
- Title
- how to make a text field required in android studio
- Category
- Java
- Title
- java classes and methods simple logic with comments
- Category
- Java
- Title
- how add strings together
- Category
- Java
- Title
- Don't use a line-beased input after a token-based input.
- Category
- Java
- Title
- java stream sorted reverse
- Category
- Java
- Title
- pass list to intent in android java
- Category
- Java
- Title
- java literals w3schools
- Category
- Java
- Title
- java string indexof
- Category
- Java
- Title
- casting java
- Category
- Java
- Title
- java create unmodifiable list
- Category
- Java
- Title
- osmdroid offline map does not show
- Category
- Java
- Title
- do statement java
- Category
- Java
- Title
- read a file in java and store as integer array using buffered reader
- Category
- Java
- Title
- init admob
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- java difference ++i and i++ loop
- Category
- Java
- Title
- how to take input in java
- Category
- Java
- Title
- java map geeksforgeeks
- Category
- Java
- Title
- java rest client response json
- Category
- Java
- Title
- get tfidf score for a sentence
- Category
- Java
- Title
- java string contains char
- Category
- Java
- Title
- spigot disable join message
- Category
- Java
- Title
- what is java plug-in
- Category
- Java
- Title
- hello world java
- Category
- Java
- Title
- test function that call a function javascrip
- Category
- Java
- Title
- ceil function in java
- Category
- Java
- Title
- how to parse a string into a number in java
- Category
- Java
- Title
- java check if string is number
- Category
- Java
- Title
- processing sound library
- Category
- Java
- Title
- java map declaration
- Category
- Java
- Title
- java best way to concatenate strings
- Category
- Java
- Title
- java boolean zen
- Category
- Java
- Title
- Java app development
- Category
- Java
- Title
- indexof java
- Category
- Java
- Title
- java array check duplicates
- Category
- Java
- Title
- length of string java
- Category
- Java
- Title
- find the triplet sum in java linked list
- Category
- Java
- Title
- java get size of array
- Category
- Java
- Title
- leap year program in java
- Category
- Java
- Title
- android studio remove notification bar
- Category
- Java
- Title
- how to get orientation lock to portrait android stackoverflow
- Category
- Java
- Title
- decomposer chaine de caractère java
- Category
- Java
- Title
- java copy array
- Category
- Java