title: how to code a factorial
body: <p>my question is not so much about code as it is the logic behind writing a factorial program. I am currently taking a MOOC at the University of Helsinki and I have become stuck on this exercise. As the course moves on to new exercises the instructions have become more and more vague. I realize this probably isn't the place to ask this question and if you must tag it or remove it, I do understand. I am trying to learn this on my own as I do not have the time or money to actually attend a university. This course has no time constraint and I wont be receiving a certificate of achievement for it, I simply want the knowledge. </p>
  
  <p>these are the instructions given for the exercise </p>
  
  <p>Create a program that calculates the factorial of the number n. The factorial n! is calculated using the formula 1*2*3*...*n. For example 4! = 1*2*3*4 = 24. Additionally, it is defined that 0! = 1.</p>
  
  <pre><code>    // i don't understand the example that 4!= 1*2*3*4 = 24 
      // or how 0! = 1 pertains to multiplying numbers in succession to find the
      // factorial of the user input number.
      // i understand that 0! = 1 simply delclares that 0 is not equal to 1 
      // and 4 is not equal to 24, however if the 4! = portion of this statement
      // is in reference to the user input number 4 that statement would not be
      // true as 1*2*3*4 does equal 24 and 4 would be the number of executions
      // of the block execution of the loop required to write the factorial 
      // program.
  
     // EDIT: okay so according to this http://en.wikipedia.org/wiki/Factorial
     // i am wrong about what is being done here as they are not declaring 
     // that 4 not equals 24 but yet that 4! is a way of correlating the non
     // negative numbers up to 4, but given that math is not my strong suit 
     // it is even more confusing to me as to what i should be doing.
  </code></pre>
  
  <p>Example outputs:</p>
  
  <p>Type a number: 3
  Factorial is 6</p>
  
  <p>Type a number: 10
  Factorial is 3628800</p>
  
  <p>my current code attempt is as follows</p>
  
  <pre><code>     public static void main(String[] args) {
      Scanner reader = new Scanner(System.in);
  
  
  
      System.out.println("Type a number:");
      int userIn = Integer.parseInt(reader.nextLine());
      int factorial = 1;
      int extra = 1;
      int sum = 0;
  
      while (factorial &lt;= userIn) {
          factorial++;
          sum = factorial + userIn + extra;
          }
      System.out.println("The factorial is:"+sum);
      }
  }
  </code></pre>
  
  <p>I do not understand what it is that i am missing, i know from research that in the real world you would not code this as there are libraries you can download to perform the factorial function that are much more efficient than what i could code, but i don't want to simply skip this exercise with the knowledge that someone else has already coded and created a library to make our lives easier, i want to learn everything that this course has to offer. if i have made a simple error i don't mind an offered code correction, however i want to understand what makes the factorial operation tick so to speak, not just be given the answer so i can move on.</p>