Jump to content

Help on a Basic Java Problem


The Penguins
 Share

Recommended Posts

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 

Factorial   CodeHS (1).png

Link to comment
Share on other sites

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

  • Like 2
Link to comment
Share on other sites

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. 

  • Like 2
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...