Plase solve it in Jave and make it very simple. Jave program In this programming
ID: 3850740 • Letter: P
Question
Plase solve it in Jave and make it very simple.
Jave program
In this programming assignment, you are asked to simulate the recursive factorial function. Your program is to be a non-recursive version of the factorial function implemented using a stack. Your program also should show the stack contents along with the calls, their parameter returns and the return value in order to show the execution of the non-recursive function. That is, for each call, your GUI should show the call with its' parameter values, for each return it should show the value returned along with the changes in the stack similar to what we did in the class. The quality of your GUI will greatly influence your homework score. You are required to submit the following: Source code with proper comments Sample outputExplanation / Answer
import java.util.*;
public class Fact {
static int n;
public static void main(String[] args) {
// TODO Auto-generated method stub
int ans = 1;
Stack stack = new Stack();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of N");
n=sc.nextInt();
if(n<=0)
{
System.out.println("The number can not be less than 0");
}
else
{
// push the numbers n,n-1 ....1 in the stack
// you can also go in reverse manner here. the result will be the same
// because in multiplication, order does not matter
for(int i = n ; i >0 ; i--)
{
System.out.println("Stack push with value "+i);
stack.push(i);
}
System.out.println("Stack is created");
// now pop all the elements one by one
// multiply them with the answer variable
for(int i = n ; i >0 ; i--)
{
System.out.println("Stack pop with value "+i);
ans=ans* (int) stack.pop();
}
System.out.println("Factorial is "+ans);
}
}
}
Output:
Enter the value of N
6
Stack push with value 6
Stack push with value 5
Stack push with value 4
Stack push with value 3
Stack push with value 2
Stack push with value 1
Stack is created
Stack pop with value 6
Stack pop with value 5
Stack pop with value 4
Stack pop with value 3
Stack pop with value 2
Stack pop with value 1
Factorial is 720