Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Find_Factorial {
- public static void main(String[] args) {
- int inputNum = 5; //The int you would like to find the factorial of.
- 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
- int sum = 1; //sum will be the sum
- //Since the factorial of a number is the product of all the lesser numbers incdluding the number itself
- //We will set a loop that takes the value of i and multiply it by sum until i reaches inputNum
- while (i <= inputNum){ //while i is less than or equal to inputNum
- sum = sum * 1;
- //this will be sum * 1; sum * 2; sum * 3; etc.. until i equals inputNum
- i = i + 1; //this will increment i by one everytime the loop finishes
- }
- System.out.println("The factorial of " + inputNum + " is: " + sum);
- //prints out the factorial of inputNum
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement