The Wayback Machine - https://web.archive.org/web/20241126193206/https://www.geeksforgeeks.org/java-program-to-print-upper-star-triangle-pattern/
Open In App

Java Program to Print Upper Star Triangle Pattern

Last Updated : 11 Oct, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The upper star triangle pattern means the base has to be at the bottom and there will only be one star to be printed in the first row. Here is the issue that will arise is what way we traverse be it forward or backward we can’t ignore the white spaces, so we are not able to print a star in the first row using only two nested for loops. The answer to this is very simple as we will break this problem into two halves, we will be running two nested for loops, one managing white spaces and the other managing pattern printing rest remains the same.

Example

Java




// Java Program to  Print Upper Star Triangle Pattern
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring and initializing variable representing
        // number of rows to be printed
        int k = 9;
 
        // Nested 2 for loops for iterating over the matrix
 
        // Outer for loop for iterating over rows
        for (int a = 0; a <= k; a++) {
 
            // Inner for loop for iterating over columns
            // where we are printing white spaces
            for (int b = 1; b <= k - a; b++) {
 
                // Print the white space
                System.out.print(" ");
            }
 
            // Inner for loop for iterating over columns
            // where we are printing white spaces
            for (int l = 0; l <= a; l++) {
 
                // Print the star pattern
                System.out.print("*");
            }
 
            // By now we are done with one row so
            // next line
            System.out.println("");
        }
    }
}


Output

         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********

Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.



Previous Article
Next Article

Similar Reads

Java Program to Print Mirror Upper Star Triangle Pattern
The pattern has two parts - both mirror reflections of each other. The base of the triangle has to be at the bottom of the imaginary mirror and the tip at the top. Illustration: Input: size = 7 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Approach: Divide pattern into sub-pa
5 min read
Java Program to Print Right Triangle Star Pattern
In this article, we will learn about printing Right Triangle Star Pattern. Examples: Input : n = 5 Output: * * * * * * * * * * * * * * * Right Triangle Star Pattern: Java Code import java.io.*; // Java code to demonstrate right star triangle public class GeeksForGeeks { // Function to demonstrate printing pattern public static void StarRightTriangl
5 min read
Java Program to Print Left Triangle Star Pattern
In this article, we will learn about printing Left Triangle Star Pattern. Examples: Input : n = 5 Output: * * * * * * * * * * * * * * * Left Triangle Star Pattern: Java Code import java.io.*; // java program for left triangle public class GFG { // Function to demonstrate printing pattern public static void StarleftTriangle(int k) { int a, b; // 1st
5 min read
Java Program to Print Downward Triangle Star Pattern
Downward triangle star pattern means we want to print a triangle that is downward facing means base is upwards and by default, orientation is leftwards so the desired triangle to be printed should look like * * * * * * * * * * Example Java Code // Java Program to Print Lower Star Triangle Pattern // Main class public class GFG { // Main driver meth
2 min read
Java Program to Print Mirror Lower Star Triangle Pattern
In this article, we are going to learn how to print mirror lower star triangle patterns in Java. Illustration: Input: number = 7 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Methods: We can print mirror lower star triangle pattern using loops, so Using for loopUsing while lo
5 min read
Java Program to Print Star Pascal’s Triangle
Pascal’s triangle is a triangular array of the binomial coefficients. Write a function that takes an integer value n as input and prints the first n lines of Pascal’s triangle. Following are the first 6 rows of Pascal’s Triangle.In this article, we will look into the process of printing Pascal's triangle. Pascal’s triangle is a pattern of the trian
4 min read
Java Program to Print Diamond Shape Star Pattern
In this article, we are going to learn how to print diamond shape star patterns in Java. Illustration: Input: number = 7 Output: * *** ***** ******* ********* *********** ************* *********** ********* ******* ***** *** * Methods: When it comes to pattern printing we do opt for standard ways of printing them via loops only. We will try out dif
6 min read
Java Program to Print Pyramid Star Pattern
This article will guide you through the process of printing a Pyramid star pattern in Java. 1. Simple pyramid pattern Java Code import java.io.*; // Java code to demonstrate Pyramid star patterns public class GeeksForGeeks { // Function to demonstrate printing pattern public static void PyramidStar(int n) { int a, b; // outer loop to handle number
5 min read
Java Program to Print Square Star Pattern
Here, we will implement a Java program to print the square star pattern. We will print the square star pattern with diagonals and without diagonals. Example: ********************** * * * * * * * * * * * * ********************** Approach: Step 1: Input number of rows and columns. Step 2: For rows of rectangle run the outer loop from 1 to rows. for (
4 min read
Java Program to Print Reverse Pyramid Star Pattern
Approach: 1. Get the number of input rows from the user using Scanner Class or BufferedReader Class object. 2. Now run two loops Outer loop to iterate through a number of rows as initialized or input is taken from reader class object in java. Now,Run an inner loop from 1 to 'i-1'Ru another inner loop from 1 to rows * 2 – (i × 2 – 1) Illustration: I
4 min read
Program to print Hut Star pattern
Given a number N such that N >= 5. The task is to print a Hut Star Pattern with (N + 3) rows as shown in the below examples. Examples: Input: N = 5 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Input: N = 7 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10 min read
Program to print half Diamond star pattern
Given an integer N, the task is to print half-diamond-star pattern. ************************************ Examples: Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** * Approach: The idea is to break the pattern into two halves that is upper half and lower half. Then print them separately with the he
4 min read
Java Pattern Programs - Learn How to Print Pattern in Java
In many Java interviews Star, number, and character patterns are the most asked Java Pattern Programs to check your logical and coding skills. Pattern programs in Java help you to sharpen your looping concepts(for loop). Now if you are looking for a place to get all the Java pattern exercises with solutions, then stop your search here. Here we have
15+ min read
Java Program to Display Upper Triangular Matrix
Upper Triangular Matrix is a matrix in which all the elements below the principal matrix are 0. A necessary condition is that matrix must be Square matrix in nature. If the matrix is not a square matrix, it can never be called the upper triangular matrix. ExamplesInput 1: mat[][] = { {2, 1, 4}, {1, 2, 3}, {3, 6, 2} } Output : mat[][]= { {2, 1, 4},
2 min read
Java Program to check if matrix is upper triangular
Given a square matrix and the task is to check the matrix is in upper triangular form or not. A square matrix is called upper triangular if all the entries below the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 3, 5, 3}, {0, 4, 6, 2}, {0, 0, 2, 5}, {0, 0, 0, 6}}; Output : Matrix is in Upper Triangular form. Input : mat[4][4] = {{5, 6,
2 min read
Java Program to Print the Multiplication Table in a Triangle Form
In this form, a table is displayed row and column-wise, in such a way such that in every row, only the entries up to the same column number filled. Example: Input : rows = 6 Output: 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 Approach: The idea is to use nested loops. First, display the column numbers. Then, use a nested loop to fill out t
2 min read
Java Program to Print Pascal's Triangle
Pascal's triangle is a pattern of the triangle which is based on nCr.below is the pictorial representation of Pascal's triangle. Example: Input : N = 5Output: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1Approach #1: nCr formula: n ! / ( n - r ) ! r ! After using nCr formula, the pictorial representation becomes: 0C0 1C0 1C1 2C0 2C1 2C2 3C0 3C1 3C2 3C3Algorithm: T
3 min read
Determine the Upper Bound of a Two Dimensional Array in Java
Multidimensional arrays in Java are common and can be termed as an array of arrays. Data in a two-dimensional array in Java is stored in 2D tabular form. A two-dimensional array is the simplest form of a multidimensional array. A two-dimensional array can be seen as an array of the one-dimensional array for easier understanding. A two-dimensional a
3 min read
Java Program to Print a Square Pattern for given integer
Write a java program to print the given Square Pattern on taking an integer as input from commandline. Examples: Input : 3 Output :1 2 3. 7 8 9 4 5 6 Input :4 Output :1 2 3 4 9 10 11 12 13 14 15 16 5 6 7 8 Java Code // Java program to print a square pattern with command // line one argument import java.util.*; import java.lang.*; public class Squar
2 min read
Java Program to Print Alphabet Inverted Heart Pattern
Alphabet Inverted Heart Pattern consists of two parts. The upper part of the pattern is a triangle. The base of the pattern has two peaks and a gap between them. Hence, the desired pattern looks like as shown in the illustration. Illustration: A BBB CCCCC DDDDDDD EEEEEEEEE FFFFFFFFFFF GGGGGGGGGGGGG HHHHHHHHHHHHHHH IIIIIIIIIIIIIIIII JJJJJJJJJJJJJJJJ
4 min read
Java Program to Print all the Strings that Match a Given Pattern from a File
Figure a way out to print all the strings that match a given pattern from a file. To get closer to the goal, concepts of files should be crystal clear so as to think out a way to print strings later on implementing the same to reach the final goal. So prior to dealing with the strings urgency is to discuss out file concepts. Once the file concepts
5 min read
Java Program to Print Spiral Pattern of Numbers
Given a number n as the size of the matrix, the task is to print a spiral pattern of size n. Examples: Input : n = 4 Output: 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 Preview of the Spiral Pattern: Java Code public class GFG { public static void printSpiral(int size) { // Create row and col // to traverse rows and columns int row = 0, col = 0; int bou
3 min read
Java Program to Print Pyramid Number Pattern
Here we are getting a step ahead in printing patterns as in generic we usually play with columnar printing in the specific row keep around where elements in a row are either added up throughout or getting reduced but as we move forward we do start playing with rows which hold for outer loop in our program. Illustrations: A pyramid number pattern of
2 min read
Java Program to Print matrix in snake pattern
Given an n x n matrix .In the given matrix, you have to print the elements of the matrix in the snake pattern. Examples : Input :mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}}; Output : 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input :mat[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output : 1 2 3 6 5 4 7 8 9
2 min read
Pattern pattern() method in Java with Examples
The pattern() method of the Pattern class in Java is used to get the regular expression which is compiled to create this pattern. We use a regular expression to create the pattern and this method used to get the same source expression. Syntax: public String pattern() Parameters: This method does not accepts anything as parameter. Return Value: This
2 min read
Program to print interesting pattern
Program to print following pattern: ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4***** ****5*5*5*5*5**** ***6*6*6*6*6*6*** **7*7*7*7*7*7*7** Examples : Input : 4 Output : ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4***** Input :5 Output : ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4**
10 min read
Program to print numbers with diamond pattern
write a program where each column represents same number according to given example:Examples : Input : 5 Output : 1 212 32123 212 1 Input : 7 Output : 1 212 32123 4321234 32123 212 1 C/C++ Code // C++ program to print diamond pattern #include<iostream> using namespace std; void display(int n) { // sp stands for space // st stands for number i
6 min read
Program to print Inverse Diamond pattern
Given an integer n, the task is to print the Inverse Diamond Pattern in 2n-1 rows.Example: Input: n = 3 Output: *** *** ** ** * * ** ** *** *** Input: n = 7 Output: ******* ******* ****** ****** ***** ***** **** **** *** *** ** ** * * ** ** *** *** **** **** ***** ***** ****** ****** ******* ******* Approach: The full Inverse Diamond is of 2n-1 row
8 min read
Java Program for Maximum height when coins are arranged in a triangle
We have N coins which need to arrange in form of a triangle, i.e. first row will have 1 coin, second row will have 2 coins and so on, we need to tell maximum height which we can achieve by using these N coins. Examples: Input : N = 7 Output : 3 Maximum height will be 3, putting 1, 2 and then 3 coins. It is not possible to use 1 coin left. Input : N
2 min read
Java Program to Compute the Area of a Triangle Using Determinants
In order to compute the area of the triangle, two parameters are needed namely base and height. For this simply the variables are assigned with zeros. The area of the triangle can be computed by using the same third-grade mathematics formula. Area = 1/2(Base * Height) Illustration: Input: (1,3) (5,-2) (-3,6) Output: Area of triangle: 4 Processing:
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg