Last active
February 6, 2022 06:20
-
-
Save TrickSumo/a0c13ac7fab9df1d0e37f0c7def402bf to your computer and use it in GitHub Desktop.
Odd Stars Diamond With Single Mid Max (Using Only Three FOR Loops)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Program to print below pattern:- | |
* | |
*** | |
***** | |
*** | |
* | |
*/ | |
public class Demo { | |
public static void main(String args[]) { | |
int n =3; | |
pattern2(n); | |
} | |
public static void pattern2(int n) { | |
int col = 0; | |
// For rows | |
for (int i=0; i<2*n -1; i++){ | |
// For spaces | |
col = i<n? n-i-1: i-n+1; | |
for (int j = 0; j<col; j++){ | |
System.out.print(" "); | |
} | |
// Stars before mid increasing oddly. i.e. 2*i +1 | |
//After mid, decreasing oddly. i.e. max stars at mid - 2(i-n-1) == 2n - 1 - 2(i-n-1) == 4n - 2i - 1 | |
col = i<n? 2*i+1 : 4*n - 2*i - 3; | |
// For Stars | |
for (int j = 0; j<col; j++){ | |
System.out.print("*"); | |
} | |
System.out.println(""); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment