Wednesday, October 11, 2017

Using two for-loops (row-column)

Challenge: Write a program that reads an integer and displays, using asterisks, a hollow square. For example, if the side length is 5, the program should display.


*****
import java.util.Scanner;
System.out.print("Enter an integer: ");
int number = in.nextInt();
for(int j=0;j<number-1;j++)
System.out.print("*");
else 

*      *
*      *
*      *
*****
class lab6_5
{
public static void main(String[] args)
{
Scanner in = new Scanner (System.in);


for(int i=0;i<number;i++)




{
System.out.print("*");

{
if(j==number-2)

    if(i==0 || i==number-1)
    {
System.out.print("*");
    }
else
    {
    System.out.print(" ");
    } 
    }
System.out.println("");
}
}


}

No comments:

Post a Comment