• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

outputting a triangle of numbers

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How could I print numbers in this form:

100
200 202
300 304 308
... ..... ....
900 916 932 ...1028

using for loops and not an array?

I tried messing with using incremeneters and precedence to change the values, but it didn't work.

Any ideas? Thanks
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, your name does not follow the JavaRanch naming convention. a bartender/sherrif will probably remind you of this soon, and ask you to change it.

Now, to your problem. What do you know how to do? can you write a loop at all?

i'd suggest starting by simplifying the problem... can you get your program to just print the 100-900? Get that to work, then post your code. We'll go from there...
 
kenji mapes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I am going to start from scratch and see what I come up with. Naming convention as far as my name looking like an identifier?

Thanks.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Names at JavaRanch have to at least look like real names.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"FlimFlam"

What fred was referring to as far as your name is the JavaRanch Naming Policy where we require that your name contain a real-sounding first and last name. While we prefer that you use your real name (or some variation thereof), we only insist that the name you choose is not obviously fictitous.

You can change your display name here
 
kenji mapes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Done.

Thanks.
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Kenji!
 
kenji mapes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!

I have switched the coed around, and then proceed to drift away from stepwise refinement.

I mean the code is going to be short, but there are so many factors involved.

My mind is so muddled right now that I can't think:

public class Numbers
{
public static void main(String[]args)
{
int n = 100;
int product = 2;


System.out.println(n);
for(int i = 1;i<2;i++)
{
for(int j = 1;j<2;j++)
{

System.out.print(n*2 + " " + (n*2 + ((Math.round(Math.pow(product,2))))));
System.out.println(n += 1);
System.out.println(n + " " + (n*2 + ((Math.round(Math.pow(product,2))))));
System.out.println(n += 2);

}



}


}//end of main method

}//end of class

I am wondering if I should use multiple expressions in the4 for loops, how many to nest. I can't get the double to round to an int. What I can't seem to figure out also is how to add one number onto each successive line up until 1028. Maybe I can use the controlling expression to do this? Hmm.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm... ok, i may be dense, but i'm not seeing the pattern...

can you print the whole thing, or explain it better? i get that each line has one more element than the previous one, and i get the pattern of the first and second column... but after that, i just don't see it...
 
kenji mapes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ugh. Couldn't get it. Thanks.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You seem to able to write loops etc., so I'll just provide you one possible approach and leave the coding to you.

It usually helps if you break down the problem into smaller ones.. The problem is to use for-loops to print out a certain number of lines, each with a certain number of words.

So we'll use a for-loop to print line-by-line and an inner for-loop to print the words in each line.

First you need to print 9 lines - so that means using an outer for-loop going from 1 to 9 (say the index is "i")

Then for each line, how many words do you need to print?
By inspection, it is equal to the line number (first line 1 word, 2nd line 2 words, etc.)
So this means the inner for-loop goes from 1 to i


So,

This will give you the correct number of words in the correct number of lines but, as you will see, all the values are wrong.

I'll leave it to you to see how to tweak this so that you get the right values.
Hint:- What you have to tweak is the amount that is added to 100*i every time


I mean the code is going to be short, but there are so many factors involved


Yes it'll take just 4 lines.

Hope this helps, let us know how it goes

[ July 18, 2005: Message edited by: Ravi Srinivas ]
[ July 18, 2005: Message edited by: Ravi Srinivas ]
 
kenji mapes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I'll try that. I definitely think that I was making it more complicated than it was. I started out with two variables and a couple of lines. Then after thinking and tweaking, I just started to add code; obviously it was superfluous.

I'll see what I can do. I can conceive it being quite simple, yet just a matter of playing with the "margins" of the code, so to speak. Taking what conventions and syntax will allow me, and working off of that. I think that I was trying to force it.

Thanks so much for the input, encouragement, and suggestions.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello kenji, this will solve the purpose i guess.though not sure of the mathematical theory behind it.
==================

import java.io.*;
class triangle{
public static void main(String args[])
{
int k=0,x=0;

for (int i=1;i<=9;i++)
{ x=i*100;
System.out.print(x + " ");
for(int j=1;j<=i;j++)
{ x+=k;
System.out.print(x + " ");
}
System.out.println();
k+=2;
}
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic