Exercise
Rectangle
Objetive
Write a java program to ask the user for a number and then display a rectangle 3 columns wide and 5 rows tall using that digit. For example:
Enter a digit: 3
333
3 3
3 3
3 3
333
Example Code
// Importing the Scanner class to handle input from the useimport java.util.Scanner; // Import Scanner class for input
public class Program {
// Main entry point of the program
public static void main(String[] args) {
// Create a scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Ask the user to input a number
System.out.print("Enter a digit: ");
// Read the user's input as an integer
int digit = scanner.nextInt();
// Print the top row of the rectangle
System.out.println(String.format("%d%d%d", digit, digit, digit)); // 3 columns wide
// Print the middle rows of the rectangle
for (int i = 0; i < 3; i++) { // 3 rows in the middle
System.out.println(String.format("%d %d", digit, digit)); // 3 columns with space in between
}
// Print the bottom row of the rectangle
System.out.println(String.format("%d%d%d", digit, digit, digit)); // 3 columns wide
// Close the scanner to free up resources
scanner.close();
}
}