• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursion  RSS feed

 
Melanie Ruth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

How do i make this code recursive:



Thanks,
Melanie
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of the following loop


what you need is a recursive function.
 
Melanie Ruth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Swastik,

I have this code


But I don't know where to add that to...          
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Melanie Ruth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does the calculations go?
 
Melanie Ruth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added the code and did a number of different tries here are the results:

Enter The Number: 10
The Factorial of 10 is 1

Enter The Number: 1
The Factorial of 1 is 1

Enter The Number: 0
The Factorial of 0 is 1

Enter The Number: 6
The Factorial of 6 is 1

Enter The Number: 8
The Factorial of 8 is 1

Enter The Number: 4
The Factorial of 4 is 1

Everything turned out to be 1...
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Melanie Ruth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its still turning into everything equals 1
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to have a long somewhere and claim to return an int from that method. Stick to one type, probably long, which will go about as far as 21! whereas an int runs out of range after 12! If you need any more that that, use BigInteger. You seem to me mixing types, so I am surprised that code will compile. You probably have an old version of the .class file, so get rid of the compiler errors and run the code again.
Your actual formula looks correct, but you should as shown in the old stye guide, use the one‑line format.
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I guess here it is



should be

 
Melanie Ruth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't really understand...
I'm a beginner at java.
 
Melanie Ruth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its still turning to 1
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This the complete code and its working for me, try it once again

 
Melanie Ruth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Very Much Swastik!
It works now!    
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are most welcome Melanie.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't give out complete solutions in Beginning Java.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!