In: Computer Science
IN JAVA Create a program that uses a nested for loop to display a rectangle of #'s with a given number of rows and columns,. This should only display the # when the column is an odd number (see examples below).
Get the number of rows and columns from the user, and display the result.
Examples:
If the user provided rows=4, and columns=7, the program should display a pattern as follows:
# # # #
# # #
# # # #
# # #
If the user provide rows=2, and columns=5, the program should display a pattern as follows:
# # #
# #
etc.
Upload
Hi,
Below is your program..itwas lil tricky but i did it finally
//IN JAVA Create a program that uses a nested for loop to
display a rectangle of #'s with a given number of rows and
columns,.
//This should only display the # when the column is an odd number
(see examples below).
import java.util.Scanner;
public class RectanglePattern {
private static Scanner sc;
public static void main(String[] args)
{
int rows, columns, i, j;
sc = new Scanner(System.in);
System.out.print(" Please Enter
Number of Rows : ");
rows =
sc.nextInt();
System.out.print(" Please Enter
Number of Columns : ");
columns = sc.nextInt();
for(i = 1; i <= rows; i++)
{
for(j = 1; j
<= columns; j++)
{
if(j%2!=0){
System.out.print("# "); }
}
System.out.print("\n");
if(i%2!=0){
columns = columns-1;}
else{
columns = columns+1;}
}
}
}