Java/J2EE and SOA (324 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (323 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Everything you need to know About Pascal Triangle in Java

Published on Sep 30,2019 105 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Creating Mathematical Patterns in any programming language is one of the foremost concepts. There is nothing different in the case of Java. In this article, we will understand how to implement different types of Pascal Triangle in Java.

 

What is a Pascal Triangle?

A Pascal Triangle consists of binomial coefficients stored in a triangular array. It is one of the classic and basic examples taught in any programming language. We take an input n from the user and print n lines of the pascal triangle.

Example Of a Pascal Triangle

 

 

A pascal triangle is started by first placing the number one in the first row and two ones in the second row. The later rows are bored by keeping the end elements same but the middle element is the sum of end elements from either side.

Consider the fourth row. The two middle terms are formed by adding one and two on either side and we get two three’s in between the two ones.

In other words, each number is directly added to others.

Different Forms of Pascal Triangle in Java

CONSIDER THE CODE

Method 1:

import java.util.Scanner;
public class PascalTri {
   static void printTriangle(int n)
    {
  
    for (int line = 0; line < n; line++)
    { 
        for (int i = 0; i <= line; i++)         System.out.print(findBinomial(line, i)+" ");                                   System.out.println();     }     }           static int findBinomial(int n, int j)     {         int result = 1;                   if (j > n - j)
        j = n - j;
             
        for (int i = 0; i < j; ++i)
        {
            result *= (n - i);
            result /= (i + 1);
        }
        return result;
    }
     
    public static void main(String args[])
    {
    int n;
    Scanner scn= new Scanner(System.in);
    System.out.println("enter the number of rows");
    n=scn.nextInt();
    printTriangle(n);
    }
}

OUTPUT:

Output of Pascal Triangle in Jav

EXPLANATION

In this code, we try to print a pascal triangle in java. Two methods are declared, one method to print the triangle and the second method to find the binomial coefficients.

static void printTriangle(int n)
    {
  
    for (int line = 0; line < n; line++)
    { 
        for (int i = 0; i <= line; i++)
        System.out.print(findBinomial(line, i)+" ");
                         
        System.out.println();
    }
    }

We have printTriangle function which is used to print the triangle. Two for loops are present. One for loop takes care of the spaces and the second one takes care of the printing. The findBinomial function is called to find the terms.

findBinomial is used to find all the terms in the pascal triangle. We have one for loop to perform the calculations.

This method has order O(n^3) time complexity.

 

Method 2:

import java.io.*;
import java.util.Scanner;
public class PascalTri {
             
            public static void printPascal(int n)
            {
                for(int line = 1; line <= n; line++)
                {
                     
                int C=1;
                for(int i = 1; i <= line; i++)
                { 
                 
                    System.out.print(C+" ");
                    C = C * (line - i) / i; 
                }
                System.out.println();
                }
              public static void main (String[] args) {
                int n = 5;
                printPascal(n);
            } 
            }

OUTPUT:

Pascal Triangle in Java

This code runs with time complexity of O(n^2). It is similar to the first method but is much faster. The same process is repeated.

With this, we come to an end of this Pascal Triangle in Java article. I hope you got an idea of what is a Pascal Triangle and the different ways to Implement a Pascal Triangle in Java.

Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “Pascal Triangle in Java” blog and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.