Triangle using Numbers

• 06-04-2010, 06:39 PM
Anandt88
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....
• 06-04-2010, 06:49 PM
JosAH
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
• 06-04-2010, 07:38 PM
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
• 06-04-2010, 07:56 PM
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();
}
}
• 06-04-2010, 08:01 PM
JosAH
Quote:

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

So, no, no rep points for you.

Jos
• 06-04-2010, 08:05 PM
Webuser
Quote:

Originally Posted by JosAH
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
• 06-04-2010, 08:12 PM
Webuser
Quote:

Originally Posted by JosAH
Nah,

a) shakey loop bounds
b) no spaces between the numbers

So, no, no rep points for you.

Jos

But I thought the question is

Quote:

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:
• 06-04-2010, 08:14 PM
StormyWaters
Where do you want to put more spaces? What is it doing now and what do you want it to be doing?
• 06-04-2010, 08:20 PM
JosAH
Quote:

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 ;-)
• 06-04-2010, 08:37 PM
Webuser
Quote:

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:
• 06-04-2010, 09:04 PM
tim
Hey everyone

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

Have fun guys
Tim
• 06-04-2010, 09:11 PM
Webuser
Quote:

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
• 06-05-2010, 03:50 PM
Anandt88
Hope u guys r having fun....but pls execute my code n then gimme a suggestion..
• 06-05-2010, 03:55 PM
Anandt88
Quote:

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
• 06-05-2010, 03:59 PM
JosAH
Quote:

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:

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
• 06-05-2010, 04:04 PM
Anandt88
Quote:

Originally Posted by JosAH
Sure, I ran it (with '4' as an argument) and this was its output:

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
• 06-05-2010, 04:10 PM
JosAH
Quote:

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