Skip to main content
fixed text
Source Link
axcdnt
  • 14.7k
  • 7
  • 29
  • 32

Usually recursive methods demands a few lines of code but a deeply thought about your algorithm. If you make a logical mistake, you'll probably obtain a StackOverflowError.

Here follows 2 programming examples for factorial:

Iteractive:Iteractive:

public int calculateIteractiveFactorial(int n) { // Base case if (n == 1) { return 1; } int factorial = 1; for (int i = 1; i <= n; i++) { factorial = factorial * i; } return factorial; } 

Recursive:Recursive:

public int calculateRecursiveFactorial(int n) { // Base case if (n == 1) { return 1; } return n * calculateRecursiveFactorial(n - 1); } 

Hope you get the idea. It's always good to know how to thinkreflect about different solutionsideas for any ideaeach proposal, always considering lines of code, complexity, clarity, cohesion, etc.

Usually recursive methods demands a few lines of code but a deeply thought about your algorithm. If you make a logical mistake, you'll probably obtain a StackOverflowError.

Here follows 2 programming examples for factorial:

Iteractive:

public int calculateIteractiveFactorial(int n) { // Base case if (n == 1) { return 1; } int factorial = 1; for (int i = 1; i <= n; i++) { factorial = factorial * i; } return factorial; } 

Recursive:

public int calculateRecursiveFactorial(int n) { // Base case if (n == 1) { return 1; } return n * calculateRecursiveFactorial(n - 1); } 

Hope you get the idea. It's always good to know how to think about different solutions for any idea, always considering lines of code, complexity, clarity, etc.

Usually recursive methods demands a few lines of code but a deeply thought about your algorithm. If you make a logical mistake, you'll probably obtain a StackOverflowError.

Here follows 2 programming examples for factorial:

Iteractive:

public int calculateIteractiveFactorial(int n) { // Base case if (n == 1) { return 1; } int factorial = 1; for (int i = 1; i <= n; i++) { factorial = factorial * i; } return factorial; } 

Recursive:

public int calculateRecursiveFactorial(int n) { // Base case if (n == 1) { return 1; } return n * calculateRecursiveFactorial(n - 1); } 

It's always good to reflect about different ideas for each proposal, always considering lines of code, complexity, clarity, cohesion, etc.

fixed grammar
Source Link
axcdnt
  • 14.7k
  • 7
  • 29
  • 32

Usually recursive methods allowdemands a few lines of code but a deeply thought about your algorithm. If you make a logical mistake, you'll probably obtain a StackOverflowError.

Here follows 2 programming common examples for factorial:

Iteractive:

public int calculateIteractiveFactorial(int n) { // Base case if (n == 1) { return 1; } int factorial = 1; for (int i = 1; i <= n; i++) { factorial = factorial * i; } return factorial; } 

Recursive:

public int calculateRecursiveFactorial(int n) { // Base case if (n == 1) { return 1; } return n * calculateRecursiveFactorial(n - 1); } 

Hope you get the idea. It's always good to know how to think about different solutions for any idea, always considering *lines of code, complexity, clarity, etc.lines of code, complexity, clarity, etc.

Usually recursive methods allow a few lines of code but a deeply thought about your algorithm. If you make a logical mistake, you'll probably obtain a StackOverflowError.

Here follows 2 programming common examples:

Iteractive:

public int calculateIteractiveFactorial(int n) { // Base case if (n == 1) { return 1; } int factorial = 1; for (int i = 1; i <= n; i++) { factorial = factorial * i; } return factorial; } 

Recursive:

public int calculateRecursiveFactorial(int n) { // Base case if (n == 1) { return 1; } return n * calculateRecursiveFactorial(n - 1); } 

Hope you get the idea. It's always good to know how to think about different solutions for any idea, always considering *lines of code, complexity, clarity, etc.

Usually recursive methods demands a few lines of code but a deeply thought about your algorithm. If you make a logical mistake, you'll probably obtain a StackOverflowError.

Here follows 2 programming examples for factorial:

Iteractive:

public int calculateIteractiveFactorial(int n) { // Base case if (n == 1) { return 1; } int factorial = 1; for (int i = 1; i <= n; i++) { factorial = factorial * i; } return factorial; } 

Recursive:

public int calculateRecursiveFactorial(int n) { // Base case if (n == 1) { return 1; } return n * calculateRecursiveFactorial(n - 1); } 

Hope you get the idea. It's always good to know how to think about different solutions for any idea, always considering lines of code, complexity, clarity, etc.

Source Link
axcdnt
  • 14.7k
  • 7
  • 29
  • 32

Usually recursive methods allow a few lines of code but a deeply thought about your algorithm. If you make a logical mistake, you'll probably obtain a StackOverflowError.

Here follows 2 programming common examples:

Iteractive:

public int calculateIteractiveFactorial(int n) { // Base case if (n == 1) { return 1; } int factorial = 1; for (int i = 1; i <= n; i++) { factorial = factorial * i; } return factorial; } 

Recursive:

public int calculateRecursiveFactorial(int n) { // Base case if (n == 1) { return 1; } return n * calculateRecursiveFactorial(n - 1); } 

Hope you get the idea. It's always good to know how to think about different solutions for any idea, always considering *lines of code, complexity, clarity, etc.