Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Week 2 Assignment 1/Factorial_W2A1_3.class
Binary file not shown.
15 changes: 15 additions & 0 deletions Week 2 Assignment 1/Factorial_W2A1_3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Factorial_W2A1_3 {
static int product = 1;
public static void main(String args[]) {
int num = 4;
System.out.println(recursion(num));
}

static int recursion(int num) {
if(num > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the terminating condition to add a terminating return type for the function. It will remove the necessity for the product variable.

product = product * num;
recursion(num - 1);
}
return product;
}
}
Binary file added Week 2 Assignment 1/Fibonacci_W2A1_1.class
Binary file not shown.
18 changes: 18 additions & 0 deletions Week 2 Assignment 1/Fibonacci_W2A1_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Fibonacci_W2A1_1 {
static int temp1 = 0, temp2 = 1, temp3 = 0;
public static void main(String arg[]) {
int num = 9;
System.out.print(temp1 + " " + temp2 + " ");
recursion(num);
}

static void recursion(int num) {
if(num > 0) {
temp3 = temp1 + temp2;
temp1 = temp2;
temp2 = temp3;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary variables used. Again, consider using the terminating condition to your benefit.

System.out.print(temp3 + " ");
recursion(num - 1);
}
}
}
Binary file added Week 2 Assignment 1/Reverse_W2A1_4.class
Binary file not shown.
19 changes: 19 additions & 0 deletions Week 2 Assignment 1/Reverse_W2A1_4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Arrays;

public class Reverse_W2A1_4 {
static int[] arr = {3,4,6,7,9};
static int[] reversedArray = new int[arr.length];
static int counter = 0;
public static void main(String args[]) {
System.out.println(recursion(arr.length));
}

static String recursion(int arrLength) {
if(arrLength > 0) {
reversedArray[counter] = arr[(arr.length - 1) - counter];
counter++;
recursion(arrLength - 1);
}
return Arrays.toString(reversedArray);
}
}
Binary file added Week 2 Assignment 1/SumOFArray_W2A1_2.class
Binary file not shown.
15 changes: 15 additions & 0 deletions Week 2 Assignment 1/SumOFArray_W2A1_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class SumOFArray_W2A1_2 {
static int sum = 0, i = 0;
static int[] arr = {3,4,2,7,9};
public static void main(String args[]) {
System.out.println(recursion(arr.length));
}
static int recursion(int arrLength) {
if(arrLength > 0) {
sum += arr[i];
i++;
recursion(arrLength - 1);
}
return sum;
}
}