Printing * s like triangles in Java?

My job in my Java course is to make 3 triangles. One left aligned, one right aligned and one centered. I have to create a menu for what type of triangle, and then enter the number of lines. Triangles should look like this:

* ** *** **** * ** *** **** * *** ***** 

So far, I have managed to make a left aligned triangle, but I can not imagine the other two. I tried to walk, but nothing worked. Can anyone help? I still have it.

 import java.util.*; public class Prog673A { public static void leftTriangle() { Scanner input = new Scanner (System.in); System.out.print("How many rows: "); int rows = input.nextInt(); for (int x = 1; x <= rows; x++) { for (int i = 1; i <= x; i++) { System.out.print("*"); } System.out.println(""); } } public static void rightTriangle() { Scanner input = new Scanner (System.in); System.out.print("How many rows: "); int rows = input.nextInt(); for (int x = 1; x <= rows; x++) { for (int i = 1; i >= x; i--) { System.out.print(" "); } System.out.println("*"); } } public static void centerTriangle() { } public static void main (String args []) { Scanner input = new Scanner (System.in); System.out.println("Types of Triangles"); System.out.println("\t1. Left"); System.out.println("\t2. Right"); System.out.println("\t3. Center"); System.out.print("Enter a number: "); int menu = input.nextInt(); if (menu == 1) leftTriangle(); if (menu == 2) rightTriangle(); if (menu == 3) centerTriangle(); } } 

Output Example:

 Types of Triangles 1. Left 2. Right 3. Center Enter a number (1-3): 3 How many rows?: 6 * *** ***** ******* ********* *********** 
+6
source share
20 answers

Tip:. For each line, you need to print a few spaces first, and then print a few stars. The number of spaces should decrease by one per line, and the number of stars should increase.

For a centered output, increase the number of stars by two for each row.

+15
source

Ilmari Karonen has good advice, and I would just like to generalize it. In general, before you ask: "How can I get a computer for this?" ask: "How would I do this?"

So, if someone gave you an empty Word document and asked you to create triangles, how would you do it? No matter what solution you come up with, it’s usually easy to translate it into Java (or any other programming language). This may not be the best solution, but (hopefully)! This will work, and this may indicate a better solution.

So, for example, maybe you say that you typed the base, then go along the line, then enter the next highest line, etc. This suggests that you can do the same in Java - create a list of Strings, from bottom to top, and then cancel them. This may mean that you can simply create them in reverse order and then you do not need to undo them. And then this may mean that you no longer need this list, because you just create and print them in the same order - at that moment you came up with the advice of Ilmari Karonen.

Or maybe you will come up with another way to do this - maybe you came up with the idea of ​​Ilmari Karonen more directly. Despite this, this should help you solve this and many other problems.

+6
source

For the right triangle for each row:

  • First: you need to print spaces from 0 to rowNumber - 1 - i .
  • Second: you need to type \* from rowNumber - 1 - i to rowNumber .

Note. i is the row index from 0 to rowNumber and rowNumber is the number of rows.

For the central triangle: it looks like a “right triangle” plus adds \* according to the index of the line (for ex: you will not add anything in the first line, because the index is 0, in the second line you will add one '*', etc. d.).

+1
source

// This is for a normal triangle

 for (int i = 0; i < 5; i++) { for (int j = 5; j > i; j--) { System.out.print(" "); } for (int k = 1; k <= i + 1; k++) { System.out.print(" *"); } System.out.print("\n"); } 

// This is for the left triangle, just deleted the space before printing *

 for (int i = 0; i < 5; i++) { for (int j = 5; j > i; j--) { System.out.print(" "); } for (int k = 1; k <= i + 1; k++) { System.out.print("*"); } System.out.print("\n"); } 
+1
source

1) Normal triangle

 package test1; class Test1 { public static void main(String args[]) { int n=5; for(int i=0;i<n;i++) { for(int j=0;j<=ni;j++) { System.out.print(" "); } for(int k=0;k<=2*i;k++) { System.out.print("*"); } System.out.println("\n"); } } 

2) right triangle

 package test1; class Test1 { public static void main(String args[]) { int n=5; for(int i=0;i<n;i++) { for(int j=0;j<=ni;j++) { System.out.print(" "); } for(int k=0;k<=i;k++) { System.out.print("*"); } System.out.println("\n"); } } } } 

3) The triangle of the left corner

 package test1; class Test1 { public static void main(String args[]) { int n=5; for(int i=0;i<n;i++) { for(int j=0;j<=i;j++) { System.out.print("*"); } System.out.println("\n"); } } } 
+1
source
 package apple; public class Triangle { private static final int row = 3; public static void main(String...strings){ printLeftTrangle(); System.out.println(); printRightTrangle(); System.out.println(); printTrangle(); } /* Pattern will be * ** *** **** */ public static void printLeftTrangle(){ for(int y=1;y<=row;y++){ for(int x=1;x<=y;x++) System.out.print("*"); System.out.println(); } } /* Pattern will be * ** *** **** */ public static void printRightTrangle(){ for(int y=1;y<=row;y++){ for(int space=row;space>y;space--) System.out.print(" "); for(int x=1;x<=y;x++) System.out.print("*"); System.out.println(); } } /* Pattern will be * *** ***** */ public static void printTrangle(){ for(int y=1, star=1;y<=row;y++,star +=2){ for(int space=row;space>y;space--) System.out.print(" "); for(int x=1;x<=star;x++) System.out.print("*"); System.out.println(); } } } 
+1
source

I know this is pretty late, but I want to share my decision.

 public static void main(String[] args) { String whatToPrint = "aword"; int strLen = whatToPrint.length(); //var used for auto adjusting the padding int floors = 8; for (int f = 1, h = strLen * floors; f < floors * 2; f += 2, h -= strLen) { for (int k = 1; k < h; k++) { System.out.print(" ");//padding } for (int g = 0; g < f; g++) { System.out.print(whatToPrint); } System.out.println(); } } 

The spaces to the left of the triangle will automatically adjust depending on which character or word you want to print.

if whatToPrint = "x" and floors = 3 it will print

x xxx xxxxx If there is no automatic space setting, it will look like this ( whatToPrint = "xxx" the same number of floors)

xxx xxxxxxxxx xxxxxxxxxxxxxxx

So, I added a simple code to prevent this from happening.

For the left polygon, just change strLen * floors to strLen * (floors * 2) and f +=2 to f++ .

For the right polygon, simply delete this for (int k = 1; k < h; k++) loop for (int k = 1; k < h; k++) or change h to 0 , if you decide to delete it, do not delete System.out.print(" "); .

+1
source

good for a triangle, you need to have three loops instead of two, one outer loop to iterate the line two parallel loops inside the main loop the first print cycle reduces the number of cycles the second cycle prints, increasing the number '' I could also give exact logic, but better if first try just concentrate the number of spaces and how much you need in each line to associate the no character with a loop, iterate no lines and you did ..... if this bothers you more, let me know, I will also explain the logic and code

0
source

This will print the stars in the triangle:

 ` public class printstar{ public static void main (String args[]){ int m = 0; for(int i=1;i<=4;i++){ for(int j=1;j<=4-i;j++){ System.out.print("");} for (int n=0;n<=i+m;n++){ if (n%2==0){ System.out.print("*");} else {System.out.print(" ");} } m = m+1; System.out.println(""); } } }' 

Reading and understanding this should help you design your logic next time.

0
source
 import java.util.Scanner; public class A { public void triagle_center(int max){//max means maximum star having int n=max/2; for(int m=0;m<((2*n)-1);m++){//for upper star System.out.print(" "); } System.out.println("*"); for(int j=1;j<=n;j++){ for(int i=1;i<=nj; i++){ System.out.print(" "); } for(int k=1;k<=2*j;k++){ System.out.print("* "); } System.out.println(); } } public void triagle_right(int max){ for(int j=1;j<=max;j++){ for(int i=1;i<=j; i++){ System.out.print("* "); } System.out.println(); } } public void triagle_left(int max){ for(int j=1;j<=max;j++){ for(int i=1;i<=max-j; i++){ System.out.print(" "); } for(int k=1;k<=j; k++){ System.out.print("* "); } System.out.println(); } } public static void main(String args[]){ A a=new A(); Scanner input = new Scanner (System.in); System.out.println("Types of Triangles"); System.out.println("\t1. Left"); System.out.println("\t2. Right"); System.out.println("\t3. Center"); System.out.print("Enter a number: "); int menu = input.nextInt(); Scanner input1 = new Scanner (System.in); System.out.print("maximum Stars in last row: "); int row = input1.nextInt(); if (menu == 1) a.triagle_left(row); if (menu == 2) a.triagle_right(row); if (menu == 3) a.triagle_center(row); } } 
0
source
 public static void main(String[] args) { System.out.print("Enter the number: "); Scanner userInput = new Scanner(System.in); int myNum = userInput.nextInt(); userInput.close(); System.out.println("Centered Triange"); for (int i = 1; i <= myNum; i+=1) {//This tells how many lines to print (height) for (int k = 0; k < (myNum-i); k+=1) {//Prints spaces before the '*' System.out.print(" "); } for (int j = 0; j < i; j++) { //Prints a " " followed by '*'. System.out.print(" *"); } System.out.println(""); //Next Line } System.out.println("Left Triange"); for (int i = 1; i <= myNum; i+=1) {//This tells how many lines to print (height) for (int j = 0; j < i; j++) { //Prints the '*' first in each line then spaces. System.out.print("* "); } System.out.println(""); //Next Line } System.out.println("Right Triange"); for (int i = 1; i <= myNum; i+=1) {//This tells how many lines to print (height) for (int k = 0; k < (myNum-i); k+=1) {//Prints spaces before the '*' System.out.print(" "); } for (int j = 0; j < i; j+=1) { //Prints the " " first in each line then a "*". System.out.print(" *"); } System.out.println(""); //Next Line } } 
0
source

This is the least complicated program, requiring only 1 cycle to print a triangle. This only works for the center triangle, but a little tweak will make it work for others too -

 import java.io.DataInputStream; public class Triangle { public static void main(String a[]) throws Exception{ DataInputStream in = new DataInputStream(System.in); int n = Integer.parseInt(in.readLine()); String b = new String(new char[n]).replaceAll("\0", " "); String s = "*"; for(int i=1; i<=n; i++){ System.out.print(b); System.out.println(s); s += "**"; b = b.substring(0, ni); System.out.println(); } } } 
0
source

Question:

  * *** ***** 

Answer:

  int i,a,j; i=5; while (i>=3) { a=1; while (a<=i) { System.out.print(" "); a++; } j=10; while (j/2>=i) { System.out.print("*"); --j; } System.out.println(""); i--; } 

Enjoy !!

0
source

For a left aligned right triangle you can try this simple code in java:

 import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int size=sc.nextInt(); for(int i=0;i<size;i++){ for(int k=1;k<size-i;k++){ System.out.print(" "); } for(int j=size;j>=size-i;j--){ System.out.print("#"); } System.out.println(); } } } 
0
source

Find the following, this will help you print a complete triangle.

 package com.raju.arrays; 

public class CompleteTriange {

 public static void main(String[] args) { int nuberOfRows = 10; for(int row = 0; row<nuberOfRows;row++){ for(int leftspace =0;leftspace<(nuberOfRows-row);leftspace++){ System.out.print(" "); } for(int star = 0;star<2*row+1;star++){ System.out.print("*"); } for(int rightSpace =0;rightSpace<(nuberOfRows-row);rightSpace++){ System.out.print(" "); } System.out.println(""); } } 

}

  * *** ***** ******* ********* *********** ************* 



0
source
 public class triangle { public static void main ( String arg[] ){ System.out.print("Enter Triangle Size : "); int num=0; try{ num=Integer.parseInt( read.readLine() ); } catch(Exception Number){ System.out.println("Invalid Number!"); } for(int i=1;i<=num;i++){ for(int j=1;j<num-(i-1);j++){ System.out.print(" "); } for(int k=1;k<=i;k++){ System.out.print("*"); for(int k1=1;k1<k;k1+=k){ System.out.print("*"); } } System.out.println(); } } } 
0
source

You might also be interested in this.

  Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int b=0; for(int i=n;i>=1;i--){ if(i!=n){ for(int k=1;k<=b;k++){ System.out.print(" "); } } for(int j=i;j>=1;j--){ System.out.print("*"); if(i!=1){ System.out.print(" "); } } System.out.println(); b=b+2; } 

Output :: 5

  * * * * * * * * * * * * * * * 
0
source

For the central triangle

  Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int b=(n-1)*2; for(int i=1;i<=n;i++){ int t= i; for(int k=1;k<=b;k++){ System.out.print(" "); } if(i!=1){ t=i*2-1; } for(int j=1;j<=t;j++){ System.out.print("*"); if(j!=t){ System.out.print(" "); } } System.out.println(); b=b-2; } 

output:

  * * * * 
0
source
 (a) (b) (c) (d) * ********** ********** * ** ********* ********* ** *** ******** ******** *** **** ******* ******* **** ***** ****** ****** ***** ****** ***** ***** ****** ******* **** **** ******* ******** *** *** ******** ********* ** ** ********* ********** * * ********** int line; int star; System.out.println("Triangle a"); for( line = 1; line <= 10; line++ ) { for( star = 1; star <= line; star++ ) { System.out.print( "*" ); } System.out.println(); } System.out.println("Triangle b"); for( line = 1; line <= 10; line++ ) { for( star = 1; star <= 10; star++ ) { if(line<star) System.out.print( "*" ); else System.out.print(" "); } System.out.println(); } System.out.println("Triangle c"); for( line = 1; line <= 10; line++ ) { for( star = 1; star <= 10; star++ ) { if(line<=star) System.out.print( "*" ); //else // System.out.print(" "); } System.out.println(); } System.out.println("Triangle d"); for( line = 1; line <= 10; line++ ) { for( star = 1; star <= 10; star++ ) { if(line>10-star) System.out.print( "*" ); else System.out.print(" "); } System.out.println(); } 
-1
source

Left triangle, * **



 from above pattern we come to know that- 1)we need to print pattern containing n rows (for above pattern n is 4). 2)each row contains star and no of stars i each row is incremented by 1. So for Left alinged triangle we need to use 2 for loop. 1st "for loop" for printing n row. 2nd "for loop for printing stars in each rows. Code for Left alinged triangle- public static void leftTriangle() { /// here no of rows is 4 for (int a=1;a<=4;a++)// for loop for row { for (int b=1;b<=a;b++)for loop for column { System.out.print("*"); } System.out.println();} } 

Correctly selected triangle- *
**



 from above pattern we come to know that- 1)we need to print pattern containing n rows (for above pattern n is 4). 2)In each row we need to print spaces followed by a star & no of spaces in each row is decremented by 1. So for Right alinged triangle we need to use 3 for loop. 1st "for loop" for printing n row. 2nd "for loop for printing spaces. 3rd "for loop" for printing stars. Code for Right alinged triangle - public void rightTriangle() { // here 1st print space and then print star for (int a=1;a<=4;a++)// for loop for row { for (int c =3;c>=a;c--)// for loop fr space { System.out.print(" "); } for (int d=1;d<=a;d++)// for loop for column { System.out.print("*"); } System.out.println(); } } 

Triangle Center- *
* * *



from the above figure we learn that - 1) we need to print a template containing n lines (for the above figure, n is 4). 2) Inside each line, we must print spaces followed by a star, and then again a space. The number of spaces in each line at startup decreases by 1. So, for a regular triangle with a triangle, we need to use 3 for the loop. 1st "cycle" for printing n lines. 2nd "for print cycles. Third" cycle "for printing stars.

The code for the central triangle is

 public void centerTriangle() { for (int a=1;a<=4;a++)// for lop for row { for (int c =4;c>=a;c--)// for loop for space { System.out.print(" "); } for (int b=1;b<=a;b++)// for loop for column { System.out.print("*"+" "); } System.out.println();} } 

CODE FOR PRINTING ALL 3 SAMPLES - public class space 4 {public static void leftTriangle () {/// there are no lines 4 for (int a = 1; a <= 4; a ++) // for the loop for the line {
for (int b = 1; b <= a; b ++) for the loop for the column {System.out.print ("*"); }

 System.out.println();} } public static void rightTriangle() { // here 1st print space and then print star for (int a=1;a<=4;a++)// for loop for row { for (int c =3;c>=a;c--)// for loop for space { System.out.print(" "); } for (int d=1;d<=a;d++)// for loop for column { System.out.print("*"); } System.out.println(); } } public static void centerTriangle() { for (int a=1;a<=4;a++)// for lop for row { for (int c =4;c>=a;c--)// for loop for space { System.out.print(" "); } for (int b=1;b<=a;b++)// for loop for column { System.out.print("*"+" "); } System.out.println();} } public static void main (String args []) { space4 s=new space4(); s.leftTriangle(); s.rightTriangle(); s.centerTriangle(); } } 
-1
source

All Articles