# Thread: Triangle using Numbers

1. Member
Join Date
Jun 2010
Posts
9
Rep Power
0

## Triangle using Numbers

Hello Everyone,

I want the code for which the o/p ll be

1
2 2
3 3 3
4 4 4 4
and so on....

2. Java Code:
```System.out.println("                        1");
System.out.println("                   2        2");
System.out.println("              3        3       3");
System.out.println("        4        4         4        4");
System.out.println("and so on....");```
You're welcome.

kind regards,

Jos

3. Member
Join Date
Jun 2010
Posts
9
Rep Power
0
Hello Jos,

Thank You for the reply, but i wasn't expecting this....Anyways this is the code i hav written

public class Triangle
{
public static void main(String[] args)
{
int n = Integer.parseInt(args[0]);
for(int i=1;i<=n;i++)
{
for(int j=n;j>=i;j--)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print(i+" ");
}
System.out.println("");
}

}
}
but i wanna give more spaces...can u give me a hint

Thanks n Regards

Anand

4. Senior Member
Join Date
Dec 2008
Posts
526
Rep Power
0
Woo

void gogo(int limit){

for(int i=0; i<limit; i++)
{
for(int j=0; j<i+1; j++)
{
System.out.print(i+1);
}
System.out.println();
}
}

5. Originally Posted by Webuser
Woo

void gogo(int limit){

for(int i=0; i<limit; i++)
{
for(int j=0; j<i+1; j++)
{
System.out.print(i+1);
}
System.out.println();
}
}
Nah,

a) shakey loop bounds
b) no spaces between the numbers
c) no code tags in your reply.

So, no, no rep points for you.

Jos

6. Senior Member
Join Date
Dec 2008
Posts
526
Rep Power
0
Originally Posted by JosAH
Java Code:
```System.out.println("                        1");
System.out.println("                   2        2");
System.out.println("              3        3       3");
System.out.println("        4        4         4        4");
System.out.println("and so on....");```
You're welcome.

kind regards,

Jos
Not bad... but how you think to "and so on"? Manually only :P

7. Senior Member
Join Date
Dec 2008
Posts
526
Rep Power
0
Originally Posted by JosAH
Nah,

a) shakey loop bounds
b) no spaces between the numbers
c) no code tags in your reply.

So, no, no rep points for you.

Jos
But I thought the question is

Hello Everyone,

I want the code for which the o/p ll be

1
2 2
3 3 3
4 4 4 4
and so on....
:rolleyes:

8. Senior Member
Join Date
Feb 2009
Posts
312
Rep Power
8
Where do you want to put more spaces? What is it doing now and what do you want it to be doing?

9. Originally Posted by Webuser
Not bad... but how you think to "and so on"? Manually only :P
Nope, that was what the OP wanted: an "and so on ... " last line. I supplied "the" code so I'm a hero, hurray!

kind regards,

Jos ;-)

10. Senior Member
Join Date
Dec 2008
Posts
526
Rep Power
0
Originally Posted by JosAH
Nope, that was what the OP wanted: an "and so on ... " last line. I supplied "the" code so I'm a hero, hurray!

kind regards,

Jos ;-)
And still ? :rolleyes:
Last edited by Webuser; 06-04-2010 at 08:41 PM.

11. Hey everyone

Looks like this thread is about having a laugh or two. ;) Seriously, this is a textbook problem. :p

Have fun guys
Tim
Last edited by tim; 06-04-2010 at 09:51 PM.

12. Senior Member
Join Date
Dec 2008
Posts
526
Rep Power
0
Originally Posted by Webuser

void gogo(int limit){

for(int i=0; i<limit; i++)
{
for(int j=0; j<i+1; j++)
{
System.out.print(i+1);
}
System.out.println();
}
}
Thank you for "improving", pal
but starting loop from 0 it's a classic manner owing to OP ;)
The thread author is speechless of choosing I guess :D

13. Member
Join Date
Jun 2010
Posts
9
Rep Power
0
Hope u guys r having fun....but pls execute my code n then gimme a suggestion..

14. Member
Join Date
Jun 2010
Posts
9
Rep Power
0
Originally Posted by StormyWaters
Where do you want to put more spaces? What is it doing now and what do you want it to be doing?
Hello,

Now its displaying a Equilateral Triangle using no's, but i want to give a tab space b/w the no's...the o/p am expecting is not visible exactly, so pls execute the program n let me know...n if i give number above 9 the triangular structure ll be destroyed...

Thanks n Regards
Anand

15. Originally Posted by Anandt88
Hello Jos,

Thank You for the reply, but i wasn't expecting this....Anyways this is the code i hav written

public class Triangle
{
public static void main(String[] args)
{
int n = Integer.parseInt(args[0]);
for(int i=1;i<=n;i++)
{
for(int j=n;j>=i;j--)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print(i+" ");
}
System.out.println("");
}

}
}
but i wanna give more spaces...can u give me a hint

Thanks n Regards

Anand
Sure, I ran it (with '4' as an argument) and this was its output:

Java Code:
```    1
2 2
3 3 3
4 4 4 4```
What do you want me to suggest? It looks fine to me.

kind regards,

Jos

16. Member
Join Date
Jun 2010
Posts
9
Rep Power
0
Originally Posted by JosAH
Sure, I ran it (with '4' as an argument) and this was its output:

Java Code:
```    1
2 2
3 3 3
4 4 4 4```
What do you want me to suggest? It looks fine to me.

kind regards,

Jos
Ya rite, but if i give a no above 9 the triangular format ll be destroyed, i want the o/p it in the same way for the limit greater than 9 also...

Thanks n Regards
Anand

17. Originally Posted by Anandt88
Ya rite, but if i give a no above 9 the triangular format ll be destroyed, i want the o/p it in the same way for the limit greater than 9 also...
Start to use the printf( ... ) method and take care that all the numbers take up the same amount of spaces ...

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
•