How can i print a triangle with "*"s using for loop in java?

19.1k views Asked by At

I want to draw a triangle with stars like below using for loop, but i really don't have any idea of how to do this ? Triangle is going to be like this:

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

and so on. Can anybody please help me ?

public class Project1 {
    public static void main (String[] args){
        int c, d, e;
        for (c = 1 ; c <= 8 ; c++){
            for (d = 1 ; d <= c ; d++){
                System.out.print ("*");
            }
            System.out.println("");
        }

        for (e = 1 ; e <= 4 ; e++){
            System.out.println ("***");
        }
    } 
} 

This is what i have found from the internet, but i did not understand the reason why it uses two loops. ( i understood the one used to construct the stem. )

9

There are 9 answers

0
Mary Joy Rosellosa On
import java.util.Scanner;
public class apple{
public static void main(String[] args){

int c,r;
for(c=1; c<=10; c++){
for(r=1; r<=c; r++){
System.out.print("*");
}
System.out.println();
}
}
0
wattostudios On

Just to comment on your internet-found code...

  1. The for loops should always start from 0, unless you have a specific reason to start from 1. Its a good habit to practice starting from 0 for everything, as it'll help you when it comes to using java arrays.
  2. The 2 for loops inside each other... The outside loop is just controlling how many lines there are in the triangle (8 in this case). The inner loop is writing the number of stars for that line. This isn't the best way of achieving the result, but it would work correctly.
  3. The for loop at the bottom is writing out stars to appear like the trunk of a tree.

Hope this helps your understanding.

2
Alex W On
public static void main(String[] args)
{

    StringBuilder stars = new StringBuilder();

    for(int i = 0; i <= 10; i++)
    {
           stars.append("*");
           System.out.println(stars);
    }

}

Or alternatively using nested loops: (This is what the exercise was really trying to get you to do)

public static void main(String[] args)
{
    for(int i = 0; i <= 10; i++)
    {
        for(int j=0; j<=i; j++)
        {
            System.out.print("*");
        }
        System.out.print("\n");
    }
}
2
Joost On

You will need two for-loops; one to print the line, and one to print the characters in a line. The number of the current line can be used to print a certain number of stars.

Use System.out.print("*") to print without adding a new line, at the end of the second loop do a System.out.println()

I'll leave the implementation of the loops as an exercise, here is the syntax: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

0
Tushar Paliwal On

This can give you solution of your question.

class Seven 
{
public static void main(String arg[])
{
for(int i=0;i<=8;i++)
{
for(int j=8;j>=i;j--)
{
System.out.print(" ");
}
for(int k=1;k<=(2*i+1);k++)
{
System.out.print("*");
}
System.out.println("\n");
}
}
}
1
user3811027 On
public class StarA {

public static void main(String[] args) {

    for( int i = 1; i <= 5; i++ )
    {
        for( int j = 0; j < i; j++ )
        {
            System.out.print("*");

        }
        System.out.println();
    }

    }
 }
0
Rakesh On
for(int aj =5;aj>=1;aj--){

            for (int a1 = 0; a1 < aj; a1++) {
                System.out.print(" ");
            }
                    for(int a2 = 5;a2>=aj;a2--) {
                        System.out.print("$");
                    }
                for(int a2 = 5;a2>aj;a2--) {
                    System.out.print("$");
                }
            System.out.println();
0
Syed Salman Hassan On

you just need two loops for your required goal the third loop is useless the first outer loop is for rows and the inner loop is for printing "*".The outerloop is used here for changing the rows and maintaining the number of required rows.

public static void tri()
{ 
     for(int i=0;i<8;i++)
       {
          for(int j=0;j<=i;j++)
             {
                System.out.print("*");
                }
                   System.out.println();
                     }
}
1
abdul salam On
          import java.util.*;

          class StarForloop
{
          public static void main(String arg[])
    {
          Scanner ob=new Scanner(System.in); //getting input
          System.out.println("Enter any NO");

          int count=ob.nextInt(); 
        String ab="*";  // initialize string variable

          for(int i=1; i<=count; i++)
        {

           ab=ab+"*"; // here you add one another string
            System.out.println(ab);


        }           
    }
}