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++;
}
}
}


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:
*****
***
*********
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With