java programming Write program to find factorial of a number
 java programming
Solution
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 package chegg;
public class TestFactorial {
/*
 Space Complexity : O(1)
 Time Complexity : O(n)
 */
 public static int factIter(int n)
 {
 int fact = 1;
 int i=1;
 while(i<=n)
 {
 fact = fact*i;
 i++;
 }
   
 return fact;
 }
   
 /*
 Space Complexity = O(n) since n calls will be present at max in rec stack
 */
 public static int factRec(int n)
 {
 if(n==0 || n==1)
 return 1;
 else
 return n*factRec(n-1);
 }
   
 public static void main(String[] args)
 {
 System.out.println(\"Factorial of 5 is : \"+factRec(5));
 System.out.println(\"Factorial of 5 is : \"+factIter(5));
 }
   
 }

