aspose file tools*
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Triangular Output " Watch "Triangular Output " New topic
Author

Triangular Output

Moni Marva
Greenhorn

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

0
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;
System.out.println(val);

int showval=2,count=1;

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

val=(int)Math.pow(i,2);

if(count==showval){
showval++;
count=0;
System.out.println(val);
continue;
}

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
 
wood burning stoves
 
subject: Triangular Output