Another "while loop problem": anyone, help me please? :-)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aprilmae36
    New Member
    • Nov 2006
    • 2

    #1

    Another "while loop problem": anyone, help me please? :-)

    Nice day to all... Here I am again, having another problem with loops... lol... Can you please help me make a while loop program that the output will be like this:

    1
    121
    12321
    1234321
    123454321

    :-)

    Thank you so much....
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by aprilmae36
    Nice day to all... Here I am again, having another problem with loops... lol... Can you please help me make a while loop program that the output will be like this:

    1
    121
    12321
    1234321
    123454321

    :-)

    Thank you so much....
    Probably half a dozen ways of doing this
    Code:
     
    class aprilmae36 {
    	 public static void main(String[] args) {
       int n = 4;
       int i = 1;
       int row = 0;
       boolean up = true;
    		 while(i < 6) {
    	for(int k = 0; k < n;k++) {
    	 System.out.print(" ");
    	}
    		  for(int l = 1; l < i; l++) {
    	 System.out.print(""+l);
    	}
    	for(int l = i; l > 0; l--) {
    	   System.out.print(""+l);
    	}
    	i++;
    	n--;
    	System.out.println();
       }
     }
    }

    Comment

    Working...