In: Computer Science
1. Write a program that prints a set of sqaures based on the widths stored in an array. Below are the specifications: Ask the user for the number of sqaures to be printed Create an array (based on the number of sqaures) to hold the widths Ask the user for widths and store in the array Print the sqaures (using any character (e.g. asterisk)) based on the widths stored in the array. (Java language)
import java.util.Scanner;
class Squares
{
public static void main (String[] args)
{
Scanner input = new
Scanner(System.in);
System.out.println("Enter the
number of squares to be printed : ");
int n = input.nextInt();
int[] arr = new int[n];
System.out.println("Enter "+ n + "
widths : ");
for(int i=0;i<n;i++)
{
arr[i] =
input.nextInt();
}
for(int i=0;i<n;i++)
{
for(int
j=0;j<arr[i];j++)
{
for(int k=0;k<arr[i];k++)
{
System.out.print("*");
}
System.out.println();
}
System.out.println();
}
}
}
Output:
Enter the number of squares to be printed : 5 Enter 5 widths : 6 4 3 2 8 ****** ****** ****** ****** ****** ****** **** **** **** **** *** *** *** ** ** ******** ******** ******** ******** ******** ******** ******** ********
Do ask if any doubt. Please upvote.