Java Program to Print Right Triangle Star Pattern

by | Jan 15, 2023 | Java, Java Programs

Introduction

In this article, we will explore Java Program to Print Right Triangle Star Pattern. The program in this article prints out the right triangular star pattern as follows:

Example:
Input : n = 5
Output:

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

Here the input n to the program from the user or otherwise determines the number of rows of the pattern that will be printed out.

Sample Coe for Right Triangle Star Pattern

import java.io.*;
//Java code to demonstrate right star triangle
public class Demo
{
//Function to demonstrate printing pattern
public static void starRight (int n)
{
int a, b;
//outer loop to handle the number of rows
//k in this case
for (a = 0; a < n; a++)
{
//inner loop to handle number of columns
//values changing according to outer loop
for (b = 0; b < = a; b ++)
{
//printing stars
System.out.print (“ * ”);
}
//end line
System.out.println ();
}
}
//Driver Function
public static void main (String args [])
{
int k = 5;
starRight (k);
}
}

 

OUTPUT

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

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.