Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Pattern problems : Write a Java program to print same character on each line and having character printed in Square shape form.

Program to Pattern problems : Write a Java program to print same character on each line and having character printed in Square Shape form. As shown below :

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

package com.hubberspot.patterns.problems;

import java.util.Scanner;

public class SquarePattern {

    public static void main(String[] args) {

        String pattern;
        int noOfTimes;

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the pattern to print : ");
        pattern = scanner.nextLine();

        System.out.print("Enter number of times it should get printed : ");
        noOfTimes = scanner.nextInt();

        for(int i=1; i<=noOfTimes; i++) {       
            
            System.out.println();

            if(i==1 || i==noOfTimes) {
      
                for(int j=1; j<=noOfTimes; j++){
                
                    System.out.print(pattern+" ");
                
                }
            } 
            else { 
                
                for(int k=1; k<=noOfTimes;k++) { 
                
                    if(k==1 || k == noOfTimes) { 
                    
                        System.out.print(pattern + " ");
                    
                    }                    
                    else { 
                     
                        System.out.print("  ");
                    
                    }
                }
            }
        }
    }
}


Output of the program : 

 
 
© 2021 Learn Java by Examples Template by Hubberspot