# Thread: Need some help with this Numbers triangle

1. Member
Join Date
Nov 2012
Posts
5
Rep Power
0

## 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
Java 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.)

3. ## Re: Need some help with this Numbers triangle

Originally Posted by UmairBaloch
Java 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•