Question:
How to write a java program that will ask for the length and width of a rectangle and then multiply them together and create a rectangle out of astrisks?
Code:
import java.util.Scanner; public class Rectangle { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter height: "); int h = sc.nextInt(); System.out.print("Enter width: "); int w = sc.nextInt(); for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ System.out.print("*"); } System.out.println(); } } }
Output:
$ java Rectangle Enter height: 5 Enter width: 20 ******************** ******************** ******************** ******************** ********************