-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7aa247d
commit e9e0da7
Showing
16 changed files
with
535 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,30 @@ | ||
/** | ||
* Product of array elements using recursion | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
import java.util.Scanner; | ||
|
||
public class ArrayProd { | ||
public static double findProduct (double arr[], int n) { | ||
if (n <= 0) return 1; | ||
return arr[n-1]*findProduct(arr, n-1); | ||
} | ||
public static void main(String[] args) { | ||
Scanner input = new Scanner (System.in); | ||
System.out.println("/* ===== Product of Array elements using recursion ===== */"); | ||
|
||
// Input the array | ||
System.out.print("\nEnter the number of elements in the array: "); | ||
int n = input.nextInt(); | ||
double arr[] = new double[n]; | ||
for (int i=0; i<n; i++) { | ||
System.out.print("Enter arr[" + i + "]: "); | ||
arr[i] = input.nextDouble(); | ||
} | ||
|
||
// Print the product | ||
System.out.println("The product of numbers is: " + findProduct(arr, n)); | ||
} | ||
} |
This file contains 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,65 @@ | ||
# Array Product | ||
|
||
WAP to calculate the product of array elements using recursion | ||
|
||
**Example** | ||
|
||
``` | ||
input: [1, 2, 3, 4, 5] | ||
output: 120 | ||
``` | ||
|
||
## Solution | ||
|
||
### [JavaScript Implementation](./arrayProd.js) | ||
|
||
```js | ||
/** | ||
* Find Product of array elements | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
function findProduct (arr, num) { | ||
if (num <= 0) return 1; | ||
return arr[num-1]*findProduct(arr, num-1); | ||
} | ||
|
||
let arr = [2, 3, 4, 5]; | ||
console.log ('Product of elements of [2, 3, 4, 5] is: ', findProduct (arr, arr.length)); | ||
``` | ||
|
||
### [Java Implementation](./ArrayProd.java) | ||
|
||
```java | ||
/** | ||
* Product of array elements using recursion | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
import java.util.Scanner; | ||
|
||
public class ArrayProd { | ||
public static double findProduct (double arr[], int n) { | ||
if (n <= 0) return 1; | ||
return arr[n-1]*findProduct(arr, n-1); | ||
} | ||
public static void main(String[] args) { | ||
Scanner input = new Scanner (System.in); | ||
System.out.println("/* ===== Product of Array elements using recursion ===== */"); | ||
|
||
// Input the array | ||
System.out.print("\nEnter the number of elements in the array: "); | ||
int n = input.nextInt(); | ||
double arr[] = new double[n]; | ||
for (int i=0; i<n; i++) { | ||
System.out.print("Enter arr[" + i + "]: "); | ||
arr[i] = input.nextDouble(); | ||
} | ||
|
||
// Print the product | ||
System.out.println("The product of numbers is: " + findProduct(arr, n)); | ||
} | ||
} | ||
``` |
This file contains 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,13 @@ | ||
/** | ||
* Find Product of array elements | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
function findProduct (arr, num) { | ||
if (num <= 0) return 1; | ||
return arr[num-1]*findProduct(arr, num-1); | ||
} | ||
|
||
let arr = [2, 3, 4, 5]; | ||
console.log ('Product of elements of [2, 3, 4, 5] is: ', findProduct (arr, arr.length)); |
This file contains 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,31 @@ | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Recursive Array Sum | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
public class ArraySum { | ||
public static int findArraySum (int arr[], int num) { | ||
if (num <= 0) return 0; | ||
return arr[num-1] + findArraySum(arr,num-1); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner input = new Scanner (System.in); | ||
System.out.println("/* ===== Sum of Array elements using recursion ===== */"); | ||
|
||
// Input the array | ||
System.out.print("\nEnter the number of elements in the array: "); | ||
int n = input.nextInt(); | ||
int arr[] = new int[n]; | ||
for (int i=0; i<n; i++) { | ||
System.out.print("Enter arr[" + i + "]: "); | ||
arr[i] = input.nextInt(); | ||
} | ||
|
||
// Print the product | ||
System.out.println("The sum of numbers is: " + findArraySum(arr, n)); | ||
} | ||
} |
This file contains 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,66 @@ | ||
# Array Sum | ||
|
||
WAP to calculate the sum of elements in a given integer array using recursion | ||
|
||
**Example** | ||
|
||
``` | ||
input: [1, 2, 3, 4, 5] | ||
output: 15 | ||
``` | ||
|
||
## Solution | ||
|
||
### [JavaScript Implementation](./arraySum.js) | ||
|
||
```js | ||
/** | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
* Array Elements Sum using Recursion | ||
*/ | ||
|
||
function findArraySum (arr, num) { | ||
if (num <= 0) return 0; | ||
return arr[num-1] + findArraySum(arr, num-1); | ||
} | ||
|
||
let arr = [1, 2, 3, 4, 5]; | ||
console.log (`Sum of Elements of array ${arr} is: ${findArraySum(arr, arr.length)}`); | ||
``` | ||
|
||
### [Java Solution](./ArraySum.java) | ||
|
||
```java | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Recursive Array Sum | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
public class ArraySum { | ||
public static int findArraySum (int arr[], int num) { | ||
if (num <= 0) return 0; | ||
return arr[num-1] + findArraySum(arr,num-1); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner input = new Scanner (System.in); | ||
System.out.println("/* ===== Sum of Array elements using recursion ===== */"); | ||
|
||
// Input the array | ||
System.out.print("\nEnter the number of elements in the array: "); | ||
int n = input.nextInt(); | ||
int arr[] = new int[n]; | ||
for (int i=0; i<n; i++) { | ||
System.out.print("Enter arr[" + i + "]: "); | ||
arr[i] = input.nextInt(); | ||
} | ||
|
||
// Print the product | ||
System.out.println("The sum of numbers is: " + findArraySum(arr, n)); | ||
} | ||
} | ||
``` |
This file contains 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,13 @@ | ||
/** | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
* Array Elements Sum using Recursion | ||
*/ | ||
|
||
function findArraySum (arr, num) { | ||
if (num <= 0) return 0; | ||
return arr[num-1] + findArraySum(arr, num-1); | ||
} | ||
|
||
let arr = [1, 2, 3, 4, 5]; | ||
console.log (`Sum of Elements of array ${arr} is: ${findArraySum(arr, arr.length)}`); |
This file contains 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,26 @@ | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Nth element of fibonacci series using recursion | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
public class Fibonacci { | ||
public static int findElement (int num) { | ||
if (num <= 2) return 1; | ||
return findElement(num-1) + findElement(num-2); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner input = new Scanner (System.in); | ||
System.out.println("/* ===== Fibonacci using recursion ===== */"); | ||
|
||
// Take the input | ||
System.out.print("\nEnter the value of n: "); | ||
int n = input.nextInt(); | ||
|
||
// Print the result | ||
System.out.println(n + "th number of fibonacci series is: " + findElement(n)); | ||
} | ||
} |
This file contains 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,63 @@ | ||
# Fibonacci Series | ||
|
||
WAP to find the nth number in fibonacci series using recursion | ||
|
||
**Example** | ||
|
||
``` | ||
input: 5 | ||
output: 5 | ||
input: 7 | ||
output: 13 | ||
``` | ||
|
||
## Solution | ||
|
||
### [JavaScript Solution](./fibonacci.js) | ||
|
||
```js | ||
/** | ||
* Fibonacci Series Using Recursion | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
function fibonacci (n) { | ||
if (n <= 2) return 1; | ||
return fibonacci (n-1) + fibonacci (n-2); | ||
} | ||
|
||
console.log ('7th element in fibonacci series is:', fibonacci(7)); | ||
``` | ||
|
||
### [Java Solution](./Fibonacci.java) | ||
|
||
```java | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Nth element of fibonacci series using recursion | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
public class Fibonacci { | ||
public static int findElement (int num) { | ||
if (num <= 2) return 1; | ||
return findElement(num-1) + findElement(num-2); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner input = new Scanner (System.in); | ||
System.out.println("/* ===== Fibonacci using recursion ===== */"); | ||
|
||
// Take the input | ||
System.out.print("\nEnter the value of n: "); | ||
int n = input.nextInt(); | ||
|
||
// Print the result | ||
System.out.println(n + "th number of fibonacci series is: " + findElement(n)); | ||
} | ||
} | ||
``` |
This file contains 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,12 @@ | ||
/** | ||
* Fibonacci Series Using Recursion | ||
* @author MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
function fibonacci (n) { | ||
if (n <= 2) return 1; | ||
return fibonacci (n-1) + fibonacci (n-2); | ||
} | ||
|
||
console.log ('7th element in fibonacci series is:', fibonacci(7)); |
This file contains 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,29 @@ | ||
/** | ||
* @author @MadhavBahlMD | ||
* @date 18/01/2019 | ||
*/ | ||
|
||
import java.util.Scanner; | ||
|
||
public class Power { | ||
public static double findPower (double a, double n) { | ||
if (n <= 0) | ||
return 1; | ||
return a*findPower(a, n-1); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner input = new Scanner (System.in); | ||
System.out.println("/* ===== Exponent ===== */"); | ||
|
||
// Input the base and exponent | ||
System.out.print("\nEnter a: "); | ||
double a = input.nextDouble(); | ||
System.out.print("Enter n: "); | ||
double n = input.nextDouble(); | ||
|
||
// Find and print the power | ||
double power = findPower(a, n); | ||
System.out.println(a + " raised to the power " + n + " is = " + power); | ||
} | ||
} |
Oops, something went wrong.