
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Downward Triangle Star Pattern in Java
In this article, we will understand how to print a downward triangle star pattern using Java. This pattern is created with multiple for loops and print statements. We will learn through two examples: one where the user inputs the number of rows, and another where the number of rows are predefined in the program.
Problem Statement
Write a Java program to print downward triangle star pattern. Below is a demonstration of the same ?
Input
Enter the number of rows : 8
Output
The downward triangle star pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Using user based input
Below are the steps to print a downward triangle star pattern using user-based input ?
- Import the Scanner class by using java.util package.
- Create a scanner object to read the number of rows from the user.
- Prompt the user to input the number of rows (denoted as my_input).
- Loop to print spaces and stars:
- The outer loop (i) runs for each row.
- The first inner loop (j) prints spaces.
- The second inner loop (k) prints stars with a space between them.
- Display the downward triangle based on user input.
Example
Here, the input is being entered by the user based on a prompt ?
import java.util.Scanner; public class DownwardTriangle{ public static void main(String args[]){ int i, j, k, my_input; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the number of my_input : "); my_input = my_scanner.nextInt(); System.out.println("The downward triangle star pattern : "); for ( i= 0; i<= my_input-1; i++){ for ( j=0; j<=i; j++){ System.out.print(" "); } for ( k=0; k<=my_input-1-i; k++){ System.out.print("*" + " "); } System.out.println(); } } }
Output
Required packages have been imported A reader object has been defined Enter the number of my_input : 8 The downward triangle star pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Using previously defined input
Following are the steps to print a downward triangle star pattern ?
- Define an integer variable (my_input) for the number of rows.
- Display the number of rows and the downward triangle star pattern on the console.
- Loop to print spaces and stars:
- The outer loop (i) runs for each row.
- The first inner loop (j) prints spaces.
- The second inner loop (k) prints stars with a space between them.
- Display the downward triangle based on predefined input.
Example
Here, the integer has been previously defined, and its value is accessed and displayed on the console ?
public class DownwardTriangle{ public static void main(String args[]){ int i, j, k, my_input; my_input = 8; System.out.println("The number of rows is defined as " +my_input); System.out.println("The downward triangle star pattern : "); for ( i= 0; i<= my_input-1; i++){ for ( j=0; j<=i; j++){ System.out.print(" "); } for ( k=0; k<=my_input-1-i; k++){ System.out.print("*" + " "); } System.out.println(); } } }
Output
The number of rows is defined as 8 The downward triangle star pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Advertisements