-
Notifications
You must be signed in to change notification settings - Fork 0
Week 2 assignment 1 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
phnsh
wants to merge
1
commit into
master
Choose a base branch
from
w2a1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| product = product * num; | ||
| recursion(num - 1); | ||
| } | ||
| return product; | ||
| } | ||
| } | ||
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.