# Need some help with this Numbers triangle

• 12-16-2012, 10:59 AM
UmairBaloch
Need some help with this Numbers triangle
Hi everyone,
I'm assigned to make a triangle using numbers and I'm only allowed to use while loop
the program should give output like this
*****1
****121
***12221
**1222221
*122222221
(Ignore the "asterisks,*")
I've sorted out how to make a triangle but I can't figure out how to display the above written output.
this is the code
Code:

```class numtriangle{     public static void main(String a[]){     int trianglesize =10;     int num = 1;     int spaces = trianglesize / 2;     int count = 0;     while (num <= trianglesize)     {     while (count < spaces)     {     System.out.print(" ");     count++;     }     count = 0;     while (count < num)     {     System.out.print("1");     count++;     }     count = 0;     spaces--;     num++;     num++;     System.out.println();     }     }     }```
and this generates the below output
*****1
****111
***11111
**1111111
*111111111

any of you guys might want to help would be highly appreciated
(Sorry about 2 previous threads, I was jst testing things, how to post etc.)
• 12-16-2012, 05:15 PM
Norm
Re: Need some help with this Numbers triangle
• 12-16-2012, 05:44 PM
JosAH
Re: Need some help with this Numbers triangle
Quote:

Originally Posted by UmairBaloch
Code:

```    count = 0;     while (count < num)     {     System.out.print("1");     count++;     }```

I quoted the part of the code that prints digits on a line; you want the first and the last digit to be a '1' and the rest of the digits should be a '2'. So, when count == 0 || count == num-1 the digit should be a '1', else it should be a '2'.

kind regards,

Jos