Last active
December 11, 2022 13:01
-
-
Save AymaneHrouch/c07dc5caa62b851e158a882f257f801b to your computer and use it in GitHub Desktop.
My solution to the triangle coding challenge.
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
// Challenge description: | |
// Make a function that takes as an argument n the number of lines, and output a triangle a triangle drawed by stars. | |
// Example: for n = 3 | |
// * | |
// *** | |
// ***** | |
public class SolutionTriangleCodingChallenge { | |
public static void draw(int n) { | |
int total = 1 + 2*(n-1); // number of stars in the last line | |
for(int i = 0; i < n; i++) { | |
int stars = 1 + 2*i; | |
int emptySpace = total - stars; | |
emptySpace /= 2; | |
for(int e=0;e<emptySpace;e++) System.out.print("_"); | |
for(int s=0;s<stars;s++) System.out.print("*"); | |
for(int e=0;e<emptySpace;e++) System.out.print("_"); | |
System.out.print("\n"); | |
} | |
} | |
public static void main(String[] args) { | |
draw(10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment