Pyramid Program in Java

This is the Java Program to Print 1,2,3,4…in Triangular Pattern.

Problem Description

Write a Java Program to print the values 1,2,3,4.. in a triangular pattern, upto n lines.

Problem Solution


The idea is to use nested loops.
Iterate the first loop from 1 to n. Print the required number of spaces using a nested loop and finally display the values using a nested loop.

Program/Source Code

Here is the source code of the Java Program to Print 1,2,3,4…in Triangular Pattern. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Print 1,2,3,4...in Triangular Pattern
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class Pattern1 {
  8.     // Function to display the pattern
  9.     public static void main(String[] args) {
  10.         int i,j,k,n;
  11.         System.out.println("Enter the number of lines");
  12.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  13.         try{
  14.             n = Integer.parseInt(br.readLine());
  15.         }catch (Exception e){
  16.             System.out.println("An error occurred");
  17.             return;
  18.         }
  19.         System.out.println("The triangular pattern is");
  20.         int space = n-2;
  21.         for(i=1; i<=n; i++){
  22.             for(k=space;k>=0;k--){
  23.                 System.out.print(" ");
  24.             }
  25.             space--;
  26.             for(j = 1; j<=i; j++){
  27.                 System.out.print(j + " ");
  28.             }
  29.             System.out.println();
  30.         }
  31.     }
  32. }
Program Explanation

1. In function main(), firstly the number of lines n is entered.
2. The outer loop for(i=1; i<=n; i++) is used to print n lines.
3. The nested loop for(k=space;k>=0;k–), is used to print the required spaces.
4. The nested loop for(j = 1; j<=i; j++), is used to print the values.

advertisement
advertisement

Time Complexity: O(n2).

Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the number of lines
10
The triangular pattern is
         1 
        1 2 
       1 2 3 
      1 2 3 4 
     1 2 3 4 5 
    1 2 3 4 5 6 
   1 2 3 4 5 6 7 
  1 2 3 4 5 6 7 8 
 1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 10

Sanfoundry Global Education & Learning Series – Java Programs..

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.