Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java Exercises: Print out the first 10 Catalan numbers by extracting them from Pascal's triangle

Java Numbers: Exercise-8 with Solution

Write a Java program to print out the first 10 Catalan numbers by extracting them from Pascal's triangle.

In combinatorial mathematics, the Catalan numbers form a sequence of natural numbers that occur in various counting problems, often involving recursively-defined objects. They are named after the Belgian mathematician Eugène Charles Catalan.
The first Catalan numbers for n = 0, 1, 2, 3, … are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452,

Sample Solution:

Java Code:

import java.util.Scanner;
public class Example8 {
   public static void main(String[] args) {
        int num = 10;
        int[] t = new int[num + 2];
        t[1] = 1;
        System.out.printf("\nList 10 Catalan numbers:-\n"); 
        for (int i = 1; i <= num; i++) {
 
            for (int j = i; j > 1; j--)
                t[j] = t[j] + t[j - 1];
 
            t[i + 1] = t[i];
            
            for (int j = i + 1; j > 1; j--)
                t[j] = t[j] + t[j - 1];
             System.out.printf("\n%d ", t[i + 1] - t[i]);
        }
		System.out.printf("\n"); 
    }
}

Sample Output:

List 10 Catalan numbers:-                                                                                                  
1                                                                                                  
2                                                                                                  
5                                                                                                  
14                                                                                                  
42                                                                                                  
132                                                                                                  
429                                                                                                  
1430                                                                                                  
4862                                                                                                  
16796 

Flowchart:

Flowchart: Print out the first 10  Catalan numbers by extracting them from Pascal's triangle

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to display first 10 lucus numbers.
Next: Write a Java program to find and print the first 10 happy numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe