Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a new line after each printing call

Tags:

java

I want to get as many stars i give in the function with a line break. But I am not able to get them with line break.

public class prac11 {

    public static void main(String[] args) {
        //printStars(1);
        printStars(2);
        printStars(3);
    }

    public static void printStars(int x) {
        int i=1;
        while(i<=x) {
            System.out.print("*");
            i++;
        }
    }
}

My output

Desired Output

like image 396
Harsha Vardhan Reddy Avatar asked Jan 01 '26 01:01

Harsha Vardhan Reddy


1 Answers

You have to add a println statement to put the linebreak after your loop:

public class prac11 {

    public static void main(String[] args) {
        printStars(5);
        printStars(3);
        printStars(9);
    }

    public static void printStars(int x) {
        int i=1;
        while(i<=x) {
            System.out.print("*");
            i++;
        }
        System.out.println(); // this will produce a linebreak
    }
}

Output:

*****
***
*********
like image 72
Juan Carlos Mendoza Avatar answered Jan 06 '26 11:01

Juan Carlos Mendoza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!