Advertisement
Guest User

Finding Factorial in Java

a guest
May 13th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. public class Find_Factorial {
  2.     public static void main(String[] args) {
  3.         int inputNum = 5; //The int you would like to find the factorial of.
  4.         int i = 1; //i will be used as a counter in our while loop and will be 1 because any number multiplied by 0 is 0
  5.         int sum = 1; //sum will be the sum
  6. //Since the factorial of a number is the product of all the lesser numbers incdluding the number itself
  7. //We will set a loop that takes the value of i and multiply it by sum until i reaches inputNum
  8.         while (i <= inputNum){ //while i is less than or equal to inputNum
  9.             sum = sum * 1;
  10.             //this will be sum * 1; sum * 2; sum * 3; etc.. until i equals inputNum
  11.             i = i + 1; //this will increment i by one everytime the loop finishes
  12.         }
  13.         System.out.println("The factorial of " + inputNum + " is: " + sum);
  14.         //prints out the factorial of inputNum
  15.     }
  16.  
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement