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
*##############
**$$$$$$$$$$$$$
***$$$$$$$$$$$$
****$$$$$$$$$$$
*****$$$$$$$$$$
******$$$$$$$$$
*******$$$$$$$$
********$$$$$$$
*********$$$$$$
**********$$$$$
**********$$$$$
*********$$$$$$
********$$$$$$$
*******$$$$$$$$
******$$$$$$$$$
*****$$$$$$$$$$
****$$$$$$$$$$$
***$$$$$$$$$$$$
**$$$$$$$$$$$$$
*##############
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");
}
}
Get Answers For Free
Most questions answered within 1 hours.