Qn: Write the program logic to print as follows
*               *
* *           * *
* * *       * * *
* * * *   * * * *
* * * * * * * * *
* * * *   * * * *
* * *       * * *
* *           * *
*               *
-By Mariyappan, Last Update On 30th May,2019 10:07 am

இதை கீழ்க்கண்டவாறு இரண்டாக பிரித்து. அவற்றின் logic-களை ஒன்றாக இணைத்தால் நமக்கு தேவையான output கிடைத்துவிடும்.

part-1
*               *
* *           * *
* * *       * * *
* * * *   * * * *
* * * * * * * * *


* * * * * * * * *
* * * *   * * * *
* * *       * * *
* *           * *
*               *
part-2

இவற்றை ஒன்றாக இணைக்கும்போது part-1ல் உள்ள கடைசி row மற்றும் part-2ல் உள்ள முதல் row இவற்றில் எதாவது ஒன்றை மட்டும் தான் எடுத்துகொள்ளவேண்டும்.

//part-1 logic
for(int row=1;row<=n-1;row++){
    for(int col=1;col<=row;col++){
    Logic to print stars here..
    }
    for(int s=1;s<=(2*n-2*row)-1;s++){
    Logic to print space here..
    }
    for(int col=1;col<=row;col++){
    Same Logic to print stars here..
    }
    System.out.println();
}
for(int p=1;p<=(2*n)-1;p++){
    Logic to print last row stars here..
}

//part-2 logic
for(int row=n-1;row>=1;row--){
    for(int col=1;col<=row;col++){
    Logic to print stars here..
    }
    for(int s=1;s<=(2*n-2*row)-1;s++){
    Logic to print space here..
    }
    for(int col=1;col<=row;col++){
    Same Logic to print stars here..
    }
    System.out.println();
}

Complete Program

import java.util.Scanner;
public class PrintPattern{
    public static void main(String args[]){
        Scanner sn=new Scanner(System.in);
        System.out.print("Enter how many rows you want: ");
        int n=sn.nextInt();
        //row should end with n-1 its important
        for(int row=1;row<=n-1;row++){
            //print stars
            for(int col=1;col<=row;col++){
                System.out.print("*");
            }
            //print space
            for(int s=1;s<=(2*n-2*row)-1;s++){
                System.out.print(" ");
            }
            //print stars
            for(int col=1;col<=row;col++){
                System.out.print("*");
            }
            System.out.println();
        }
        //print last row stars
        for(int p=1;p<=(2*n)-1;p++){
            System.out.print("*");
        }
        System.out.println();
        //row should start with  n-1 end with 1. its important
        for(int row=n-1;row>=1;row--){
            //print stars
            for(int col=1;col<=row;col++){
                System.out.print("*");
            }
            //print space
            for(int s=1;s<=(2*n-2*row)-1;s++){
                System.out.print(" ");
            }
            //print stars
            for(int col=1;col<=row;col++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Output:
Enter how many rows you want: 5
*               *
* *           * *
* * *       * * *
* * * *   * * * *
* * * * * * * * *
* * * *   * * * *
* * *       * * *
* *           * *
*               *

Pgcomments

Comments