The Penguins Posted August 9, 2017 Share Posted August 9, 2017 I'm thoroughly stuck on this one problem in a Basic Java lesson. Can't figure out how to get the factorial calculator right as the answer is always = (Actual factorial answer * user input). Please help Link to comment Share on other sites More sharing options...
Khaos Posted August 9, 2017 Share Posted August 9, 2017 The prpblem is you multiplied 4 with 4 in the beginning The Penguins 1 Link to comment Share on other sites More sharing options...
Axle Change Posted August 9, 2017 Share Posted August 9, 2017 It should be int i = factorial-1 In factorials we don't need to multiply by the number it is, in your code it would start at the same number, i.e. 5*5*4*3*2*1, it should start with the number-1 so we get 5*4*3*2*1 The Penguins and Mengh. 2 Link to comment Share on other sites More sharing options...
A Delicious Cashew Posted August 9, 2017 Share Posted August 9, 2017 The easiest way to debug simpler programs is to write it out - you can see the steps being taken to reach the output Let's say factorial is 4. That means product is initially 4. i goes from 4 down to 1. Product will equal product * 4 * 3 * 2 * 1. Since product was 4 originally, you're multiplying the first number twice effectively. You would want to set product = 1 initially or use the above solution. Happysedits and The Penguins 2 Link to comment Share on other sites More sharing options...
The Penguins Posted August 10, 2017 Author Share Posted August 10, 2017 Thanks for help guys, got it right now Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now