Cout <<"Illegal argument to Cout <<"Illegal argument to power"; power"; Break ; Break ; } } for(int i = 1; i <= n; ++i) if(n > 0) result = result * x; return ( x * power(x, n-1) ); //recursive case return result; else } return(1); //base case } Write an iterative method to compute the factorial of a Write a recursive method to compute the factorial of a number. number. public static int factorial(int n) public static int iterativeFactorial(int n) { { if(n < 0) if(n < 0) { { Cout <<“Sorry negative numbers Cout << negative numbers not allowed.” ; ”Sorry not allowed.”; System.exit(0); Break; } } int result = 1; if(n == 1) //base case for(int i = 1; i <= n; ++i) return(1); result = result * i; else return (n * factorial(n-1)); return result; //recursive case } }
Cout <<"Illegal argument to power"; Break ; } for(int i = 1; i <= n; ++i) result = result * x; return result; } Cout <<"Illegal argument to power"; Break ; } if(n > 0) return ( x * power(x, n-1) ); //recursive case else return(1); //base case } Write an iterative method to compute the factorial of a number. public static int iterativeFactorial(int n) { if(n < 0) { Cout << ”Sorry negative numbers not allowed.”; Break; } int result = 1; for(int i = 1; i <= n; ++i) result = result * i; return result; } Write a recursive method to compute the factorial of a number. public static int factorial(int n) { if(n < 0) { Cout <<“Sorry negative numbers not allowed.” ; System.exit(0); } if(n == 1) //base case return(1); else return (n * factorial(n-1)); //recursive case }
doc-9-docx-3138f2d16a2894a0b029ac115da88f8f-original.docx