Rectangle star pattern


Java program to print solid rectangle star pattern

Java code to create rectangle star pattern using nested loops. 
 
Sample output:
Number of rows: 6     
Number of columns: 10
 *  *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  *      
 *  *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  *
import java.util.Scanner;

public class RectangleStarPattern {

    public static void main(String[] args) {                                                                      
        Scanner sc = new Scanner(System.in);
        int rows, columns;

        do {
            System.out.print("Number of rows: ");
            rows = sc.nextInt();
        } while (rows < 1);

        do {
            System.out.print("Number of columns: ");
            columns = sc.nextInt();
        } while (columns < 1);
        
        for (int i = 1; i <= rows; i++) { //outer loop: rows
            for (int j = 1; j <= columns; j++) { //inner loop: columns                                            
                System.out.print(" * ");
            }
            System.out.println();
        }
    }

}//Rectangle pattern

Output:

Number of rows: 6
Number of columns: 10
 *  *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  * 



No comments :

Post a Comment