You are here: Home / Topics / Square Pattern of star logic in Java

Square Pattern of star logic in Java

Filed under: Java on 2023-08-18 06:42:50

//pattern using for and while

public class ForPattern{

   public static void main(String[] args) {
       int i, j;
       for (i = 1; i <= 5; i++) {
           for (j = 1; j <= 5; j++) {
               if (i == 1 || i == 5 || j == 1 || j == 5) {
                   System.out.print(" * ");
               } else {
                   System.out.print("   ");
               }
               if (j == 5) {
                   System.out.println();
               }
           }
       }
   }
}

 

Output:

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

About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.