This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Triangular Output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Triangular Output " Watch "Triangular Output " New topic

Triangular Output

Moni Marva

Joined: Jan 24, 2005
Posts: 23
I am trying the program that can generate following output

1 4
9 16 25
36 49 64 81

I tried to do it in following ways, but not getting the exact output.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
U can modify this...

public class Triangle {

public static void main(String[] args) {
int num=14; //u can make it as a user input
int val=0;

int showval=2,count=1;

for(int i=1;i<= num; i++,count++){



System.out.print(val + (val < 10 ? " ":val < 100 ?" ":" "));


[ August 10, 2005: Message edited by: A Kumar ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'd look for something that expresses what we're doing as clearly as I could:

Your code dealt with leading spaces, but I don't see them in your sample output. If you want to right justify this thing:

you might pick a line length & pad each line to that length before you print it.

You could figure the line length ... this kind of thing would be close enough to play with and fix up ...

Any of that sound like fun?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Triangular Output
Similar Threads
Some help with a problem
math combinations
urgent help on magic square
outputting a triangle of numbers