Skip to content

Commit

Permalink
Add bonus problems on recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Jan 18, 2019
1 parent 7aa247d commit e9e0da7
Show file tree
Hide file tree
Showing 16 changed files with 535 additions and 1 deletion.
30 changes: 30 additions & 0 deletions BONUS/Recursion/ArrayProduct/ArrayProd.java
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));
}
}
65 changes: 65 additions & 0 deletions BONUS/Recursion/ArrayProduct/README.md
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));
}
}
```
13 changes: 13 additions & 0 deletions BONUS/Recursion/ArrayProduct/arrayProd.js
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));
31 changes: 31 additions & 0 deletions BONUS/Recursion/ArraySum/ArraySum.java
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));
}
}
66 changes: 66 additions & 0 deletions BONUS/Recursion/ArraySum/README.md
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));
}
}
```
13 changes: 13 additions & 0 deletions BONUS/Recursion/ArraySum/arraySum.js
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)}`);
26 changes: 26 additions & 0 deletions BONUS/Recursion/Fibonacci/Fibonacci.java
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));
}
}
63 changes: 63 additions & 0 deletions BONUS/Recursion/Fibonacci/README.md
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));
}
}
```
12 changes: 12 additions & 0 deletions BONUS/Recursion/Fibonacci/fibonacci.js
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));
29 changes: 29 additions & 0 deletions BONUS/Recursion/Power/Power.java
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);
}
}
Loading

0 comments on commit e9e0da7

Please sign in to comment.