square of character inside square of another character in java -
i want print square of character in format basad on the number of lines example :
number of lines : 4 output : b b b b number of lines : 5 output : a b b b b c b b b b a
but didn't know how result , code
import java.util.scanner; public class test { public static void main ( string arugs [] ) { scanner read= new scanner(system.in) ; system.out.println ( " please inter number of line : " ) ; int size = read.nextint(); int []array = new int[size ]; int c = 97; for(int = 0; < size; ++i) { for(int j = 0; j < size; ++j){ array[i]= c; system.out.print( (char)array [i]);} system.out.println(); } } } }
a funny way :
public static void main(string arugs[]) { scanner sc = new scanner(system.in); system.out.println("please enter number of lines : "); int size = sc.nextint(); sc.close(); (int = 0; < size; i++) { (int j = 0; j < size; j++) { system.out.print((char) ('a' + math.min(size - j - 1, math.min(size - - 1, math.min(i, j)))) + " "); } system.out.println(); } }
the idea :
- we need calculate "depth" of particular point
(i, j)
. value of cell'a' + depth
- the depth defined minimum distance of point edge of matrix
Comments
Post a Comment