Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
*
**
**
***
***
***
and so on....


What I have tried:

Java
 <pre>
public class Pattern {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i,j,n=6,k;
		for(i=0;i<=n;i++) {
			for(j=0;j<=i+1;j++) {
                           System.out.print("* "+"");
					j++;
			}
			System.out.println();
                        
		}
}
}
Posted
Updated 29-May-18 21:33pm
Comments
Richard MacCutchan 30-May-18 3:21am    
What is the question?

You need one more iteration. Try
Java
public class Pattern
{
  public static void main(String[] args)
  {
    int i,j,n=6,k;

    for ( i = 0; i < n; ++i)
      for(j=0; j <=i; ++j)
      {
        for (k=0; k<=i; ++k)
          System.out.print("*");
        System.out.println();
      }
  }
}
 
Share this answer
 
Run your program and check your output. Then you should see what is wrong:
  • There are spaces between the stars on each line
  • The number of stars is not correct
  • There is only one line for multiple stars

Then think what you have to change to get the required output.

Because this is homework, you won't get it completely written for you here. Doing it yourself helps you to learn.

But I have some hints:
You are incrementing the loop variable j twice with each loop iteration. Don't do such if you have no specific reason.

You probably need three loops (the general count, the number of stars on a line, and the repeated lines).

I suggest to modify your code so that it prints the correct number of stars on a line. If that is done, enhance the code to implement the printing of repeated lines.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900