Program to Print Down triangle Star Pattern
In this article we are going to see how to print the down triangle star pattern
Example-1 For rows = 5 ********* * * * * * * *
Example-2 For rows = 6 *********** * * * * * * * * *
Now, let’s see the actual program to print it.
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
Approach:
- Enter total row and store it in an integer variable
rows
. - Take one outer for loop to iterate the rows.
- Take two inner for loops, one to print the space and the other one to print the star.
- After every iteration print a new line.
JAVA Code:
Method-1 : Static Star Character
import java.util.Scanner; class pattern { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Rows : "); //Taking input from user int r, c, k, rows=scan.nextInt(); //Outer loop for (r=rows; r>= 1 ; r--) {//Inner loop for (c = r; c < rows ; c++) { System.out.print(" "); }//Inner loop 2 for (k = 1; k <= (2*r -1) ;k++) { if(k==1 || r == rows || k==(2*r-1)) { System.out.print("*"); } else { System.out.print(" "); } } //To print a newline System.out.println(""); } } }
Output: Rows : 5 ********* * * * * * * *
Method-2 : User Input Character
import java.util.Scanner; class pattern { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Rows : "); //Taking input from user int r, c, k, rows=scan.nextInt(); System.out.print("Symbol : "); //Taking input from user char s=scan.next().charAt(0); //Outer loop for (r=rows; r>= 1 ; r--) {//Inner loop for (c = r; c < rows ; c++) { System.out.print(" "); }//Inner loop 2 for (k = 1; k <= (2*r -1) ;k++) { if(k==1 || r == rows || k==(2*r-1)) { System.out.print(s); } else { System.out.print(" "); } } //To print a newline System.out.println(""); } } }
Output: Rows : 5 Symbol : @ @@@@@@@@@ @ @ @ @ @ @ @
Explanation :
Let’s understand the program by going through the detailed explanation.
We have taken rows value as 5.
Iteration-1
r=5, goes out of the first inner loop as c>rows
.
It prints 9 stars as r == rows
*********
Iteration-2
r=4, goes into the first inner loop and prints 1 space as c<rows
.
It prints two star k==1
and
2k == (
1 * r -
))
and rest spaces.
* *
Iteration-3
r=3, goes into the first inner loop and prints 2 space as c<rows
.
It prints two star k==1 and
2k == (
1 * r -
))
and rest spaces.
* *
Iteration-4
r=2, goes into the first inner loop and prints 3 space as c<rows.
It prints two stars for k==1
and
2k == (
1 * r -
))
and rest spaces.
* *
Iteration-5
r=1, goes into the first inner loop and prints 4 space as c<rows
.
It prints one star k==1
and rest space.
*
After this r < 1 i.e. 0 so program will come out of the loop.
Now, after end of all iteration we will see the complete pattern is printed on the output screen like this.
*********** * * * * * * *
C Code:
#include <stdio.h> int main(int argc, char const *argv[]) { printf("Enter rows : "); int rows, r, c, k; //Taking row as input from user scanf("%d", &rows); //Outer loop for (r = rows; r >= 1; r--) { //Inner loop for (c = r; c < rows; c++) { printf(" "); } //Inner loop 2 for (k = 1; k <= (2 * r - 1); k++) { if (k == 1 || r == rows || k == (2 * r - 1)) { printf("*"); } else { printf(" "); } } //To print a newline printf("\n"); } return 0; }
Output: Enter rows : 5 ********* * * * * * * *
C++ Code:
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { cout << "Enter rows : "; int rows, r, c, k; //Taking row as input from user cin >> rows; //Outer loop for (r = rows; r >= 1; r--) { //Inner loop for (c = r; c < rows; c++) { cout << " "; } //Inner loop 2 for (k = 1; k <= (2 * r - 1); k++) { if (k == 1 || r == rows || k == (2 * r - 1)) { cout << "*"; } else { cout << " "; } } //To print a newline cout << endl; } return 0; }
Output: Enter rows : 5 ********* * * * * * * *
Related Java Star Pattern Programs: