Hello everyone.
I'm working on some exercises and I'm having some problems with a method. I want to create a method to calculate the Factorial of an int number. I already wrote code that asks the user to input an int number and it calculates the Factorial, and it works fine (ie: if I input 5 it outputs, as it should. Here's the code:5! = 120
import java.util.Scanner; public class Factorial1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number; int total = 1; System.out.print("Enter positive int number: "); number = input.nextInt(); while(number < 0) { System.out.print("Enter positive int number: "); number = input.nextInt(); } for(int i = number; i > 0; i--) total *= i; System.out.printf("%d! = %d", number, total); } }
Now I want to make a method to re-use this code in other programs and I wrote this program:
But when I run this program it outputs 0 instead of 120. I don't have the slightest idea of what is wrong with this code as it compiles just fine but doesn't work as intended.
Any help will be very appreciated, and thanks in advance!
Ricardo