pascals triangle 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();
 
}
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
pascal's triangle code in java Write a Java program to display Pascal's triangle Printing Pascal Triangle in Java Pascal's Triangle solution java all pascal's triangle pattern java generate pascal's triangle java how to make a pascal triangle in java pascal's triangle program in java how to create Pascal's Triangle in java java pascal triangle program how to solve pascal triangle in java how to make pascal's triangle in java pascal's triangle java code pascal's pyramid java how to print pascal triangle in java pascal tree java pascal triangle java program triangle.java pascal triangle Pascal number Diamond in java Pascal Diamond in java Print Pascal's Triangle in Java pascal triangle from 0 in java pascal's triangle java wap to design pascal triangle in java pascal&rsquo;s triangle program simple code in java pascal&rsquo;s triangle program in java pascal triangle in java using array Java program to display pascal triangle python program to create a function that prints Pascal's triangle. pascal algorith in java pascal triangle geeksforgeeks pascal triangle c++ how to program pascals triangle java pascal triangle pascal triangle code print pascal's triangle subarray solve pascal triangle recursion java row of pascal's triangle in java print pascals triangle pascal triangle program in java pascal triangle code in java pascal's triangle python print pascal triangle pascal triangle print pascal trianglegfg pascal traingle java coe easy pascal's triangle code how to make Pascal&rsquo;s Triangle in java print Pascal&rsquo;s Triangle in java a Java program to display Pascal's triangle. pascal traingle java program for pascal triangle in java pascal triangle in java program pascal triangle java code pascal's triangle in java pascals triangle in java print pascals triangle in java pascals triangle code pascal's triangle string how to print pascal's triangle in java pascal triangle in java pascal traingle code geeks for geeks pascal algorithm java pascal triangle using * in java print pascal triangle in java pascal triangle pattern in java pascal triangle geeks right pascal triangle number in java right pascal triangle in java pascal triangle java pascals triangle java
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source