Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Factorial {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int[] inputArray = new int[100];
- int i = 0, input, temp = 0;
- do {
- System.out.print("Enter positive number (or 0) or negative number to end input : ");
- input = scanner.nextInt();
- inputArray[i] = input;
- i++;
- } while(input >= 0);
- do {
- System.out.println(inputArray[temp] + " factorial is " + inputArray[temp] + "! = " + factorial(inputArray[temp]) + "\n\n");
- temp++;
- } while(temp <= i);
- }
- public static int factorial(int n) {
- if (n < 0) {
- System.out.println("Negative numbers do not have factorials!");
- System.exit(0);
- }
- if (n == 0 || n == 1) return 1;
- return n * factorial(n-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement