Diamond inside Rectangle/square

Write a java program to print following pattern:


import java.util.Scanner;
public class DiamondInsideSqure {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Row size :");
int n = sc.nextInt();
int states = n, space = 0;
for (int i = 0; i < 2 * n-1; i++) {
if(i==0) {
states=states;
space=space;
}
else if (i <n) {
states = states - 1;
space = space + 2;
}
else {
states = states + 1;
space = space - 2;
}
for (int j = 0; j < states; j++) {
System.out.print("*");
}
for (int k = 0; k < space; k++) {
System.out.print(" ");
}
for (int j = 0; j < states; j++) {
System.out.print("*");
}
System.out.println();
}

}

}

Comments

Popular posts from this blog

program to calculate product of each column in matrix