Created
May 23, 2021 15:33
-
-
Save Raghav2211/df8bc1c8c73162a4e9dcb97d655e97bb to your computer and use it in GitHub Desktop.
Touching triangle in java
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
public class TouchingTriangle { | |
public static void main(String[] args) { | |
touchingTriangle(); | |
} | |
private static void touchingTriangle() { | |
int r = 10; | |
int qty = 10; | |
for (int iu = 1, id = r; iu <= r; ++iu, --id) { | |
// up | |
for (int su = 1; su <= r - iu; ++su) { | |
System.out.print(" "); | |
} | |
for (int i = 1; i <= qty; i++) { | |
if (i % 2 != 0) { | |
exact_up(r, iu, i); | |
} else { | |
excat_down(r, id); | |
} | |
} | |
System.out.println(); | |
} | |
} | |
private static void excat_down(int r, int id) { | |
for (int cd = 0; cd < 2 * id - 1; cd++) { | |
if (id != r) { | |
if (cd == 2 * id - 2) { | |
System.out.print("* "); | |
} else { | |
System.out.print(" "); | |
} | |
} else { | |
System.out.print("* "); | |
} | |
} | |
} | |
private static void exact_up(int r, int iu, int qty) { | |
for (int cu = 0; cu < 2 * iu - 1; cu++) { | |
if (iu != r) { | |
if ((qty == 1 && cu == 0) || cu == 2 * iu - 2) { | |
System.out.print("* "); | |
} else { | |
System.out.print(" "); | |
} | |
} else { | |
System.out.print("* "); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment