- Java Basic Programs
- Java Programming Examples
- Java Print Hello World
- Java Get Input from User
- Java Print Integer
- Java Add two Numbers
- Java Check Even or Odd
- Java Check Prime or Not
- Java Check Alphabet or Not
- Java Check Vowel or Not
- Check Reverse equal Original
- Java Fahrenheit to Celsius
- Java Celsius to Fahrenheit
- Java Perfect Number Program
- Java Find Quotient Remainder
- Java Days to Seconds
- Java Count Digits in Number
- Java Binary Number Addition
- Java Discount Program
- Java Compute Courier Charge
- Java Find Telephone Bill
- Java Print ASCII Values
- Java Check Palindrome or Not
- Java Check Armstrong or Not
- Generate Armstrong Numbers
- Add two Numbers using Pointers
- Java Mathematical Programs
- Add Subtract Multiply & Divide
- Java Make Calculator
- Java Add Digits of Number
- Java Check Leap Year or Not
- Java Check Divisibility
- Java Find Simple Interest
- Java Find Compound Interest
- Java Print Fibonacci Series
- Java Find nCr nPr
- Calculate Average & Percentage
- Java Calculate Arithmetic Mean
- Java Calculate Student Grade
- Java Print Table of Number
- Java Print Prime Numbers
- Java Add n Numbers
- Java Interchange two Numbers
- Java Reverse Numbers
- Java Swap two Numbers
- Count Positive Negative & Zero
- Find Largest of two Numbers
- Find Largest of three Numbers
- Java Find Factorial of Number
- Java Find HCF & LCM
- Area & Perimeter of Square
- Area & Perimeter of Rectangle
- Area & Circumference of Circle
- Java Conversion Programs
- Java Decimal to Binary
- Java Decimal to Octal
- Java Decimal to Hexadecimal
- Java Binary to Decimal
- Java Binary to Octal
- Java Binary to Hexadecimal
- Java Octal to Decimal
- Java Octal to Binary
- Java Octal to Hexadecimal
- Java Hexadecimal to Decimal
- Java Hexadecimal to Binary
- Java Hexadecimal to Octal
- Java Pattern Programs
- Java Pattern of Stars
- Java Pattern of Alphabets
- Java Pattern of Numbers
- Java Pyramid of Stars
- Java Pyramid of Alphabets
- Java Pyramid of Numbers
- Java Print Diamond Pattern
- Java Print Floyd Triangle
- Java Print Pascal Triangle
- Java Array Programs
- One Dimensional Array Program
- Java Linear Search
- Java Binary Search
- Find Largest Element in Array
- Find Smallest Element in Array
- Java Reverse Array
- Insert Element in Array
- Delete Element from Array
- Java Merge two Array
- Java Bubble Sort
- Java Selection Sort
- Java Insertion Sort
- Java Find Common Elements
- Java Count Even/Odd Number
- Two Dimensional Array Program
- Java Add two Matrices
- Java Subtract two Matrices
- Java Transpose Matrix
- Multiply two Matrices
- Three Dimension Array Program
- Java String Programs
- Java Print String
- Find Length of String
- Java Compare two String
- Java Copy String
- Java Concatenate String
- Java Reverse String
- Delete Vowels from String
- Delete Words from Sentence
- Find Occurrence of a Character
- Java Find Occurrence of a Word
- Occurrence of Each Character
- Java Occurrence of Each Word
- Java Count Repeated Characters
- Java Count Repeated Words
- Java Capitalize Each Word
- Java Count Vowels/Consonants
- Java Extract Numbers
- Java Count Word in String
- Remove Spaces from String
- Java Sort a String
- Java Uppercase to Lowercase
- Java Lowercase to Uppercase
- Java Swap two Strings
- Java Check Anagram or Not
- Java Check Balance Parentheses
- Java Check Password Strength
- Java File Programs
- Java Read File
- Java Write to File
- Read & Display File Content
- Java Copy File
- Java Append Text to File
- Java Merge two File
- List files in Directory
- Java Delete File
- Java Miscellaneous Programs
- Generate Random Numbers
- Java Print Time & Date
- Java Get IP Address
- Java Shutdown Computer
- Java Programming Tutorial
- Java Tutorial
Java Program to Print Star Pattern, Pattern of Stars (*)
This article is created to cover a lot of pattern programs using star (*) in Java. This article covers all the famous pattern programs using star. More than 15 pattern programs using star, are included in this article.
Print Star Pattern in Java - Pattern No.1
The question is, write a Java program to print star pattern. The program given below is its answer:
public class fresherearth { public static void main(String[] args) { int i, j; for(i=0; i<5; i++) { for(j=0; j<=i; j++) { System.out.print("* "); } System.out.print("\n"); } } }
The snapshot given below shows the sample output produced by above program in Java, on printing of star (*) pattern:
The dry run of above program goes like:
- Starting directly from the execution of for loop
- That is, i=0 and the condition i<5 or 0<5 evaluates to be True
- Therefore program flow goes inside the loop
- Inside the loop, there is another for loop
- Therefore the execution of internal for loop begins. That is, j=0, and the condition j<=i or 0<=0 evaluates to be True
- Therefore program flow goes inside this loop
- And using the statement:
System.out.print("* ");
a single * gets printed followed by a single white space - Now the value of j gets incremented using j++ (the third statement of internal loop)
- Now j=1. And the condition j<=i or 1<=0 evaluates to be False this time
- Therefore, program flow does not goes inside the internal loop, this time
- And the last statement of external loop, that is:
System.out.print("\n");
gets executed. That inserts a newline to the output screen. So that the next * will get printed from next line - Since all the statements of outer loop, gets executed. Therefore, its time to increment its variable, i.e., i
- So i=1, and the condition i<5 or 1<5 again evaluates to be True
- Therefore program flow again goes inside the loop
- This process continues until the condition evaluates to be False
- In this way, the pattern gets printed
The above program can also be created as:
public class fresherearth { public static void main(String[] args) { for(int i=0; i<5; i++) { for(int j=0; j<=i; j++) System.out.print("* "); System.out.print("\n"); } } }
You'll get the exact output as of previous program. Since the program given above, prints pattern of stars with limited number of lines or rows. Therefore let's modify the program and create a new one that allows user to define the row size of star pattern:
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { int row, i, j; Scanner s = new Scanner(System.in); System.out.print("Enter the Number of Lines (Row): "); row = s.nextInt(); for(i=0; i<row; i++) { for(j=0; j<=i; j++) System.out.print("* "); System.out.print("\n"); } } }
The sample run of above program with user input 10 as number of lines/rows to expand the pattern, is given in the following snapshot:
You can use the same, to allow user to define the size of row/line to expand the pattern, in any program given here.
Print Star Pattern in Java - Pattern No.2
public class fresherearth { public static void main(String[] args) { int row=5, i, j, space, spaceLimit; spaceLimit = (row*2) - 2; for(i=0; i<row; i++) { for(space=0; space<spaceLimit; space++) System.out.print(" "); for(j=0; j<=i; j++) System.out.print("* "); System.out.print("\n"); spaceLimit = spaceLimit-2; } } }
The output of above program is:
* * * * * * * * * * * * * * *
Note - To increase the size, just increase the value of row from 5 to more say 8, 10, 11 etc.
Also the above program can also be created in this way, to allow user to define the size of the pattern:
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { int row, i, j, space, spaceLimit; Scanner s = new Scanner(System.in); System.out.print("Enter the Row Size of Pattern: "); row = s.nextInt(); spaceLimit = (row*2) - 2; for(i=0; i<row; i++) { for(space=0; space<spaceLimit; space++) System.out.print(" "); for(j=0; j<=i; j++) System.out.print("* "); System.out.print("\n"); spaceLimit = spaceLimit-2; } } }
The sample run of above program with user input 15 is shown in the snapshot given below:
You can apply the same in other programs too, to print the specified size of pattern by user at run-time of the program.
Print Star Pattern in Java - Pattern No.3
public class fresherearth { public static void main(String[] args) { int row=5, i, j; for(i=0; i<row; i++) { for(j=i; j<row; j++) System.out.print("* "); System.out.print("\n"); } } }
Output of Pattern No.3 Program
* * * * * * * * * * * * * * *
Print Star Pattern in Java - Pattern No.4
public class fresherearth { public static void main(String[] args) { int row=5, i, j, space, spaceLimit=0; for(i=0; i<row; i++) { for(space=0; space<spaceLimit; space++) System.out.print(" "); for(j=i; j<row; j++) System.out.print("* "); System.out.print("\n"); spaceLimit = spaceLimit + 2; } } }
Output of Pattern No.4 Program
* * * * * * * * * * * * * * *
Print Star Pattern in Java - Pattern No.5
public class fresherearth { public static void main(String[] args) { int i, j, row=10; for(i=0; i<row; i++) { for(j=0; j<=i; j++) { if(i==0 || i==1) System.out.print("* "); else if(i==(row-1)) System.out.print("* "); else { if(j==0 || j==i) System.out.print("* "); else System.out.print(" "); } } System.out.print("\n"); } } }
Output of Pattern No.5 Program
* * * * * * * * * * * * * * * * * * * * * * * * * * *
Print Star Pattern in Java - Pattern No.6
public class fresherearth { public static void main(String[] args) { int i, j, row=10, space, spaceLimit; spaceLimit = (row*2) - 2; for(i=0; i<row; i++) { for(space=0; space<spaceLimit; space++) System.out.print(" "); for(j=0; j<=i; j++) { if(i==0 || i==1) System.out.print("* "); else if(i==(row-1)) System.out.print("* "); else { if(j==0 || j==i) System.out.print("* "); else System.out.print(" "); } } System.out.print("\n"); spaceLimit = spaceLimit - 2; } } }
Output of Pattern No.6 Program
* * * * * * * * * * * * * * * * * * * * * * * * * * *
Print Star Pattern in Java - Pattern No.7
public class fresherearth { public static void main(String[] args) { int i, j, row=10; for(i=0; i<row; i++) { for(j=i; j<row; j++) { if(i==0 || i==(row-1) || i==(row-2)) System.out.print("* "); else { if(j==i || j==(row-1)) System.out.print("* "); else System.out.print(" "); } } System.out.print("\n"); } } }
Output of Pattern No.7 Program
* * * * * * * * * * * * * * * * * * * * * * * * * * *
Print Star Pattern in Java - Pattern No.8
public class fresherearth { public static void main(String[] args) { int i, j, row=10, space, spaceLimit=0; for(i=0; i<row; i++) { for(space=0; space<spaceLimit; space++) System.out.print(" "); for(j=i; j<row; j++) { if(i==0 || i==(row-1) || i==(row-2)) System.out.print("* "); else { if(j==i || j==(row-1)) System.out.print("* "); else System.out.print(" "); } } System.out.print("\n"); spaceLimit += 2; } } }
Output of Pattern No.8 Program
* * * * * * * * * * * * * * * * * * * * * * * * * * *
Print Star Pattern in Java - Pattern No.9
public class fresherearth { public static void main(String[] args) { int i, j, row=10, space; for(i=0; i<row; i++) { for(space=i; space<row; space++) System.out.print(" "); for(j=0; j<=i; j++) System.out.print("* "); System.out.print("\n"); } } }
Output of Pattern No.9 Program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Print Star Pattern in Java - Pattern No.10
public class fresherearth { public static void main(String[] args) { int i, j, row=10, space; for(i=0; i<row; i++) { for(space=i; space<row; space++) System.out.print(" "); for(j=0; j<=i; j++) { if(i==0 || i==1) System.out.print("* "); else if(i==(row-1)) System.out.print("* "); else { if(j==0 || j==i) System.out.print("* "); else System.out.print(" "); } } System.out.print("\n"); } } }
Output of Pattern No.10 Program
* * * * * * * * * * * * * * * * * * * * * * * * * * *
Print Star Pattern in Java - Pattern No.11
public class fresherearth { public static void main(String[] args) { int i, j, row=10, space; for(i=0; i<row; i++) { for(space=0; space<i; space++) System.out.print(" "); for(j=i; j<row; j++) System.out.print("* "); System.out.print("\n"); } } }
Output of Pattern No.11 Program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Print Star Pattern in Java - Pattern No.12
public class fresherearth { public static void main(String[] args) { int i, j, row=10, space; for(i=0; i<row; i++) { for(space=0; space<i; space++) System.out.print(" "); for(j=i; j<row; j++) { if(i==0 || i==(row-1) || i==(row-2)) System.out.print("* "); else { if(j==i || j==(row-1)) System.out.print("* "); else System.out.print(" "); } } System.out.print("\n"); } } }
Output of Pattern No.12 Program
* * * * * * * * * * * * * * * * * * * * * * * * * * *
Print Star Pattern in Java - Pattern No.13
public class fresherearth { public static void main(String[] args) { int i, j, row=7; for(i=0; i<row; i++) { for(j=i; j>=0; j--) System.out.print("* "); System.out.print("\n"); } for(i=0; i<(row-1); i++) { for(j=(row-1); j>i; j--) System.out.print("* "); System.out.print("\n"); } } }
Output of Pattern No.13 Program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Above program can also be written as following program. This program shows the actual use of row. That is, in above program, the value of row is given 7, but the pattern expanded upto 13 lines. Therefore, we need to modify that program:
public class fresherearth { public static void main(String[] args) { int i, j, row=13, decider; if(row%2==0) decider = row/2; else decider = (row/2) + 1; for(i=0; i<row; i++) { if(i<decider) { for(j=i; j>=0; j--) System.out.print("* "); } else { for(j=i; j<row; j++) System.out.print("* "); } System.out.print("\n"); } } }
It will produces the same output as shown in the snapshot given below:
Note - You can do a lot of modification in any of the program given here, to improve your concept. Because, the same program can be created in multiple ways.
Print Star Pattern in Java - Pattern No.14
public class fresherearth { public static void main(String[] args) { int i, j, row=13, decider; if(row%2==0) decider = row/2; else decider = (row/2) + 1; for(i=0; i<row; i++) { if(i<decider) { for(j=i; j>=0; j--) { if(i==0 || i==1) System.out.print("* "); else if(j==i || j==0) System.out.print("* "); else System.out.print(" "); } } else { for(j=i; j<row; j++) { if(i==(row-1) || i==(row-2)) System.out.print("* "); else if(j==i || j==(row-1)) System.out.print("* "); else System.out.print(" "); } } System.out.print("\n"); } } }
Output of Pattern No.14 Program
* * * * * * * * * * * * * * * * * * * * * * * *
Print Star Pattern in Java - Pattern No.15
public class fresherearth { public static void main(String[] args) { int i, j, row=7, space, spaceLimit; spaceLimit = (row*2) - 2; for(i=0; i<row; i++) { for(space=0; space<spaceLimit; space++) System.out.print(" "); for(j=0; j<=i; j++) System.out.print("* "); System.out.print("\n"); spaceLimit -= 2; } spaceLimit = 0; for(i=0; i<(row-1); i++) { for(space=i; space<=spaceLimit; space++) System.out.print(" "); for(j=(row-1); j>i; j--) System.out.print("* "); System.out.print("\n"); spaceLimit += 2; } } }
Output of Pattern No.15 Program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Print Star Pattern in Java - Pattern No.16
public class fresherearth { public static void main(String[] args) { int i, j, row=7, space, spaceLimit; spaceLimit = (row*2) - 2; for(i=0; i<row; i++) { for(space=0; space<spaceLimit; space++) System.out.print(" "); for(j=0; j<=i; j++) { if(i==0 || i==1) System.out.print("* "); else { if(j==0 || j==i) System.out.print("* "); else System.out.print(" "); } } System.out.print("\n"); spaceLimit -= 2; } spaceLimit = 0; for(i=0; i<(row-1); i++) { for(space=i; space<=spaceLimit; space++) System.out.print(" "); for(j=(row-1); j>i; j--) { if(i==(row-2) || i==(row-3)) System.out.print("* "); else { if(j==(row-1) || j==(i+1)) System.out.print("* "); else System.out.print(" "); } } System.out.print("\n"); spaceLimit += 2; } } }
Output of Pattern No.16 Program
* * * * * * * * * * * * * * * * * * * * * * * *
Print Star Pattern in Java - Pattern No.17
From any pattern given above, you can also combine any two to create another pattern, like shown in the program and its sample output given below:
public class fresherearth { public static void main(String[] args) { int i, j, row=10, space; for(i=0; i<row; i++) { for(space=0; space<i; space++) System.out.print(" "); for(j=i; j<row; j++) System.out.print("* "); System.out.print("\n"); } for(i=0; i<row; i++) { for(space=(i+1); space<row; space++) System.out.print(" "); for(j=0; j<=i; j++) System.out.print("* "); System.out.print("\n"); } } }
Output of Pattern No.17 Program
Print Star Pattern in Java - Pattern No.18
Here is another and last pattern of stars, I'm going to create for this article.
public class fresherearth { public static void main(String[] args) { int i, j, k=1, row=5; for(i=0; i<row; i++) { for(j=0; j<k; j++) System.out.print("* "); k += 2; System.out.print("\n"); } } }
Output of Pattern No.18 Program
* * * * * * * * * * * * * * * * * * * * * * * * *
Note - There are tons of pattern you can try with. These programs are just demo, to show you, how the code can be written in Java, to design different - different star pattern. Still you can design the pattern in your mind and implement that pattern in programming world. Do it yourself. For sure, it will boost your logic.
« Previous Program Next Program »