Question

JAVA How would you use for loops to print a triangle inside of a rectangle given...

JAVA

How would you use for loops to print a triangle inside of a rectangle given the user inputs a specific number of rows and columns for the rectangle? And edit the first and last lines. Not using an array

Ex:

Rows = 10, Columns = 15

*##############

**$$$$$$$$$$$$$

***$$$$$$$$$$$$

****$$$$$$$$$$$

*****$$$$$$$$$$

******$$$$$$$$$

*******$$$$$$$$

********$$$$$$$

*********$$$$$$

**********$$$$$

**********$$$$$

*********$$$$$$

********$$$$$$$

*******$$$$$$$$

******$$$$$$$$$

*****$$$$$$$$$$

****$$$$$$$$$$$

***$$$$$$$$$$$$

**$$$$$$$$$$$$$

*##############

Homework Answers

Answer #1

import java.util.*;


class Pattern
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int r,c,k,d,row,col;
System.out.print("\nEnter number of Rows : ");
row=input.nextInt();
System.out.print("Enter number of Columns : ");
col=input.nextInt();
System.out.println("\n");
d=row/2;
for(r=1;r<=row;r++)
{
if(r<=d)
{
for(k=1;k<=r;k++)
{
System.out.print("*");
}
if(r==1)
{
for(c=col-1;c>=r;c--)
{
System.out.print("#");
}
}
else
{
for(c=col-1;c>=r;c--)
{
System.out.print("$");
}
}
}
else
{
for(k=d;k>=r-d;k--)
{
System.out.print("*");
}
if(r==row)
{
for(c=row-r;c<col-1;c++)
{
System.out.print("#");
}
}
else
{
for(c=row-r;c<col-1;c++)
{
System.out.print("$");
}
}
}
System.out.println();
}
System.out.println("\n");
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Summary In this lab, you add nested loops to a Java program provided. The program should...
Summary In this lab, you add nested loops to a Java program provided. The program should print the letter E. The letter E is printed using asterisks, three across and five down. Note that this program uses System.out.print("*"); to print an asterisk without a new line. Instructions Write the nested loops to control the number of rows and the number of columns that make up the letter E. In the loop body, use a nested if statement to decide when...
Python Code Use (for loops) inside another for loop (a nested loop) to do the following:...
Python Code Use (for loops) inside another for loop (a nested loop) to do the following: Print Hello 30 times followed by Bye 10 times. Do that whole thing 15 times. (for a total of 600 lines of output) #Hint: First write the code that prints 30 "Hello" and then 10 "Bye". Think of that as two separate tasks, each of which requires its own for loop. THEN, once you get that working, put all of that inside ANOTHER loop...
Instructions Write a Java code Your goal is to take N integer inputs from the user...
Instructions Write a Java code Your goal is to take N integer inputs from the user -- N's value will be given by the user as well. You can assume the user provides a valid value for N, i.e., >0. Store the input integers in an array of size N in the order they are provided. These tasks should be done in the main() method. Create a new method called checkArray() that will take the previously created array as input...
Please write a java program Instructions Your goal is to take N integer inputs from the...
Please write a java program Instructions Your goal is to take N integer inputs from the user -- N's value will be given by the user as well. You can assume the user provides a valid value for N, i.e., >0. Store the input integers in an array of size N in the order they are provided. These tasks should be done in the main() method. Create a new method called checkArray() that will take the previously created array as...
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an...
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an unknown number of cells filled with data. Your goal is to iterate through the 2D array and keep a count of how many cells are full. You will be given the dimensions of the array. INPUT: All input has been handled for you: Two integers denoting the dimensions of the array. These are the integer variables rows and cols. A partially filled 2-D array....
This program will output a right triangle based on user specified height triangle_height and symbol triangle_char....
This program will output a right triangle based on user specified height triangle_height and symbol triangle_char. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangle_char character. (1 pt) (2) Modify the program to use a loop to output a right triangle of height triangle_height. The first line will have one user-specified character, such as % or *. Each subsequent line will have...
Using Java in this task, you will use the switch statement to provide your activity of...
Using Java in this task, you will use the switch statement to provide your activity of whole week. First, you will ask to provide the number of the day, where number of a day means: 1 = Monday 2 = Tuesday 3 = Wednesday 4 = Thursday 5 = Friday 6 = Saturday 7 = Sunday When the user provides a day number then you will print the statement that shows your lectures on that day and your other activities...
answer for how it would be evaluated in Java without writing a code for it. a)...
answer for how it would be evaluated in Java without writing a code for it. a) 17 + 2 - 5 / 5 b) 2 + 3 * 5 - 2 c) 3 * 2 / 3 + 8 d) 6 % 2 * 10 Problem 2 : Write an application in java that inputs one number consisting of 5 digits (e.g. 12345) from the user, separates the number into its individual digits and prints the digits separated from one...
Create a program to calculate and print basic stats on a set of a given input...
Create a program to calculate and print basic stats on a set of a given input values representing students' scores on an quiz. 1) Ask the user for the total number of quiz scores to be input (assume a positive integer will be given). 2) Create an array to hold all the quiz scores and then get all the scores from input to put into the array. For example, if the user input 10 in step (1) then you should...
This is in java and you are not allowed to use Java API classes for queues,...
This is in java and you are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own implementations for them. You should construct a BST by inserting node values starting with a null tree. You can re-use the code for the insert method given in the sample code from the textbook. -insert method is provided below Your code should have a menu driven user interface at the command line with...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT