Qn: Write the program logic to print the stars as follows using java
Type-1

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

Type-2

  * * * * * * * * *   
*   * * * * * * *   * 
* *   * * * * *   * * 
* * *   * * *   * * * 
* * * *   *   * * * * 
* * * * *   * * * * * 
* * * *   *   * * * * 
* * *   * * *   * * * 
* *   * * * * *   * * 
*   * * * * * * *   * 
  * * * * * * * * * 
-By Admin, Last Update On 30th May,2019 10:30 am

Type-1

இதில் முதல் row-வில் முதல் மற்றும் கடைசியாக , stars print செய்யப்பட்டுள்ளது. இரண்டாவது row-வில், இரண்டாவது position மற்றும் கடைசியிலிருந்து இரண்டாவது position-னில் stars print செய்யப்பட்டுள்ளது . மூன்றாவது row-வில் மூன்றாவது position மற்றும் கடைசியிலிருந்து மூன்றாவது position-னில் stars print செய்யப்பட்டுள்ளது. இதே போல் ஒவ்வொரு row-விலும் stars print செய்யபட்டுள்ளது

ஆகவே row-ன் value-ம் column value-ம் எப்பொழுது சமமாக வருகின்றதோ அப்பொழுது star print செய்தால் போதும். சமமாக இல்லாத பொழுது space print செய்யவேண்டும். அதற்காகதான் இரண்டு loop-ன் values (-n)ல் துவங்கி n-ல் முடியவேண்டும்.

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();
        for (int row = -n; row <= n; row++) {
            for (int col = -n; col <= n; col++) {
                if (row * row == col * col) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}
Output:
Enter how many rows you want: 5
*               *
  *           *  
    *       *    
      *   *      
        *        
      *   *      
    *       *    
  *           *  
*               *

Type-2

இதுவும் type-1 logic-ஐ போன்று row-ன் value-ம் column value-ம் எப்பொழுது சமமாக வருகின்றதோ அப்பொழுது space print செய்தால் போதும். சமமாக இல்லாத பொழுது star print செய்யவேண்டும்.

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();
        for (int row = -n; row <= n; row++) {
            for (int col = -n; col <= n; col++) {
                if (row * row == col * col) {
                    System.out.print(" ");
                } else {
                    System.out.print("*");
                }
            }
            System.out.println();
        }
    }
}
Output:
Enter how many rows you want: 5

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

Pgcomments

Comments