Question

In: Computer Science

1. Write a program that prints a set of sqaures based on the widths stored in...

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)

Solutions

Expert Solution

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.


Related Solutions

Write a RARS assembly language program to solve the following: For a set of integers stored...
Write a RARS assembly language program to solve the following: For a set of integers stored in an array, calculate the sum of the positive numbers and the sum of the negative numbers, storing the results into memory. In the data segment of the program, define an array consisting of 25 integers (words) that are a mix of positive and negative values. You can select any values you wish but try to create a balance of positive and negative numbers....
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> For this project, you are to: 1) You should validate any data coming from the...
Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> c++ project. need help.
Write a c program that prints the final sum of all values from 1 to n...
Write a c program that prints the final sum of all values from 1 to n only when n is a positive value. The program is to print "Poor!" when the final sum is less than 70, print "Good" when the sum is between 71 and 90. or "Great!" when the sum is 91 or better.
Write a program in C++ that prints out the even numbers between 1 and 21 using...
Write a program in C++ that prints out the even numbers between 1 and 21 using WHILE loop. Also, find the sum AND product of these numbers and display the resulting sum and product.
/* Problem 1 * Write and run a java program that prints out two things you...
/* Problem 1 * Write and run a java program that prints out two things you have learned * so far in this class, and three things you hope to learn, all in different lines. */ You could write any two basic things in Java. /* Problem 2 * The formula for finding the area of a triangle is 1/2 (Base * height). * The formula for finding the perimeter of a rectangle is 2(length * width). * Write a...
Write a program that prints the insurance fee to pay for a pet according to the...
Write a program that prints the insurance fee to pay for a pet according to the following rules:(NOTE:You must use a switch statement to determine pet fee.)A dog that has been neutered costs $50.A dog that has not been neutered costs $80.A cat that has been spayedcosts $40.A cat that has not been spayedcosts $60.A bird or reptile costs nothing.Any other animal generates an error message.The program should prompt the user for the appropriate information: a character code for the...
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT