In: Computer Science
In Java:
Implement a program that repeatedly asks the user to input a positive integer and outputs the factorial of that input integer. Do not use recursion, solution should use stack.
//Main.java
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
Stack stack = new Stack<>();
System.out.print("enter an integer: ");
n = scanner.nextInt();
int f = 1;
for(int i = 1;i<=n;i++){
stack.push(i);
}
while (!stack.empty()){
f *= stack.peek();
stack.pop();
}
System.out.println("Factorial of "+n+" is "+f);
}
}
