1

I am very new in the programming world and in Javaland too. I am trying to learn for loop and solving pattern with it.

I want to draw this pattern using for loop :

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

I am using this code to achieve this:

public class PatterWithForLoop {

    public static void main(String args[]){
        for(int i = 0; i <= 5; i++){
            System.out.println("*" + "\n");
        }
    }
}

But, I am only getting * in 15 lines, not in the pattern. Please, anyone can help to solve this issue here?

Thanks in advance.

5
  • 1
    Please show a bit more context. That for loop shouldn't run any iteration at all. Commented May 29, 2020 at 8:05
  • It is iterating but I am not getting an expected output. Commented May 29, 2020 at 8:07
  • That code does not produce the output you are describing. Commented May 29, 2020 at 8:07
  • Hint: search the doc for the difference between print and println. Commented May 29, 2020 at 8:11
  • Thanks Serge. Khelwood, I have updated my code with more detail. Commented May 29, 2020 at 8:12

3 Answers 3

4

If you are using JDK 11, you can write the following code:

for(int i = 0; i < 5; i++) {
    System.out.println("*".repeat(i+1));
}

With String.repeat(), you can do the same thing with less code.

Sign up to request clarification or add additional context in comments.

Comments

2

Your for-loop seems strange.

The loop should be read like this:

"For int i starting at 0, loop as long as i<5 and after every iteration, do i++"

for(int i = 0; i<5, i++){
   System.out.println("*" \n);
}

This will print 5 rows:

*

*

*

*

*

now in every row you want one more * and you might want to remove the \n which is already included in println So instead of printing "*" you create a String before the loop and you add one new * to it every iteration:

String line ="";             // we start with an empty string
for(int i=0; i<5; i++){      
   line= line+"*";           // before printing we add a new *
   System.out.println(line); // then we print it 
}

It should (haven't tested it) result in this:

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

1 Comment

I am trying to give tick your answer but not able to do as I don't have enough reputation but I will do it later. Thanks.
1

Try this code :

public class Main {
    public static void main(String[] args) {

        int i, j, n =5;  
        for(i=0; i<n; i++) {           
            for(j=0; j<=i; j++) {       
                System.out.print("*");
            }
            System.out.println();
        }
    }       
}

2 Comments

Thanks, Som. That's works. I never thought for loop inside for loop. Sorry, I tried to tick your answer but I don't have enough reputation to do it. Will accept it later Thanks.
@NipamPatel : Welcome bro !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.