aspose file tools*
The moose likes Beginning Java and the fly likes recursion: print from 1 to n Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "recursion: print from 1 to n" Watch "recursion: print from 1 to n" New topic
Author

recursion: print from 1 to n

Fendel Coulptz
Greenhorn

Joined: Sep 23, 2004
Posts: 24
Hello,
as you all can see im very new to java and recursion here...

"Given an integer n, write recursive function that prints 1 through n"

i can easily do it with a method with 2 variables, eg. display(int m, int n),

but how can i do it with only a single variable? eg. display(int n)

thanks.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi
i can easily do it with a method with 2 variables, eg. display(int m, int n),
Could you post the program here? As we know, you can do with 2 variables, some edit are needed to do with only one variable.
Following structure is used for writing recursion

HTH


Francis Siu
SCJP, MCDBA
Fendel Coulptz
Greenhorn

Joined: Sep 23, 2004
Posts: 24
oops sorry i meant parameters not variables so sorry

method is display(int m, int n)

so to print "12345678910" from one to 10 is display(1,10)
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
I don't see the answer either.
Are you sure you aren't supposed to print numbers from n to 0?
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Fendel,
but how can i do it with only a single variable? eg. display(int n)



Joyce
Fendel Coulptz
Greenhorn

Joined: Sep 23, 2004
Posts: 24
thank you
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I think this is called "tail recurssion". You should be able to easily revise it to use so-called "head recurssion" in order to print the numbers from n down to 1.


Java API Documentation
The Java Tutorial
 
Don't get me started about those stupid light bulbs.
 
subject: recursion: print from 1 to n