-1

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. )

Tim
  • 35,413
  • 11
  • 95
  • 121
Berk Elmas
  • 37
  • 2
  • 2
  • 5
  • 2
    You should post your best/latest attempt before asking for help. – Sergey Kalinichenko Jun 07 '12 at 13:38
  • @Berk Elmas Hint: It requires 2 loops. But why it requires two loops, figure out. – Mahesh Jun 07 '12 at 13:38
  • This is really not my homework. I am an high school student right now, and all i want is to learn java by myself. Trust me ! – Berk Elmas Jun 07 '12 at 13:38
  • A loop from 1 to n, within which you use [this method](http://stackoverflow.com/questions/2306235/java-repeat-character) would work. – assylias Jun 07 '12 at 13:39
  • Search how to write a line, if you got basic concept of algorithmic, it's straight forward. – Michael Laffargue Jun 07 '12 at 13:39
  • @Mahesh You can even save the inner loop. – assylias Jun 07 '12 at 13:39
  • 5
    @BerkElmas You should attempt to write some code first, so you can practice and learn how to do it. Then you can ask us a question, showing your code, and we will be able to help you improve, correct, and offer suggestions. – wattostudios Jun 07 '12 at 13:41
  • This code is short enough that you should be able to walk through it yourself. Pretend you're the computer, grab a piece of paper, and "execute" every line, jotting down what variables would be and what would be printed out. It should quickly become clear what is happening. – Derek Jun 07 '12 at 13:54
  • @BerkElmas - Even assuming that this is not homework, you will still learn how to program faster and more thoroughly if you TREAT it as homework. In other words, try to write your own programs rather than reading someone else's solution. – Stephen C Jun 07 '12 at 14:28
  • Go through some basic Java tutorials. This question should be closed. – aglassman Jun 07 '12 at 15:04

9 Answers9

5
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");
    }
}
Foozball
  • 3
  • 1
Alex W
  • 37,233
  • 13
  • 109
  • 109
2

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

Joost
  • 3,169
  • 2
  • 22
  • 40
  • theres better implementations. – Kevin Jun 07 '12 at 13:45
  • 2
    The implementation of Alex W is maybe more efficient, but since this is quite a basic question I think the asker can benefit from learning how to use nested loops – Joost Jun 07 '12 at 13:47
1

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.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
0

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");
}
}
}
Tushar Paliwal
  • 301
  • 4
  • 11
0
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
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();
    }

    }
 }
Jhanvi
  • 5,069
  • 8
  • 32
  • 41
0
          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);


        }           
    }
}
  • Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written – WhatsThePoint Jan 23 '18 at 09:58
0
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();
realr
  • 3,652
  • 6
  • 23
  • 34
Rakesh
  • 1
0

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();
                     }
}