|
| 1 | + |
| 2 | + |
| 3 | +# Day 14 - Recursion Series Part B |
| 4 | + |
| 5 | +Today's Problems - Sum of digits and product of numbers |
| 6 | + |
| 7 | +## Question 1 |
| 8 | + |
| 9 | +Given a number, write a program to find the sum of it's digits using recursion |
| 10 | + |
| 11 | +**Example** |
| 12 | + |
| 13 | +``` |
| 14 | +input: 12345 |
| 15 | +output: 15 |
| 16 | +
|
| 17 | +input: 91827 |
| 18 | +output: 27 |
| 19 | +``` |
| 20 | + |
| 21 | +## Question 2 |
| 22 | + |
| 23 | +Given 2 numbers, write a program to find their product using recursion |
| 24 | + |
| 25 | +**Example** |
| 26 | + |
| 27 | +``` |
| 28 | +input: 10,5 |
| 29 | +output: 50 |
| 30 | +
|
| 31 | +input: -8,4 |
| 32 | +output: -32 |
| 33 | +``` |
| 34 | + |
| 35 | +**Hint**: Multiplication is repeated addition! |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +## Part A - Sum of Digits |
| 40 | + |
| 41 | +### JavaScript Implementation |
| 42 | + |
| 43 | +#### [Solution](./JavaScript/sum_madhav.js) |
| 44 | + |
| 45 | +```js |
| 46 | +/** |
| 47 | + * @author MadhavBahlMD |
| 48 | + * @date 08/01/2018 |
| 49 | + */ |
| 50 | + |
| 51 | +function sumDigits (num) { |
| 52 | + if (num/10 < 1) |
| 53 | + return num; |
| 54 | + else |
| 55 | + return (num % 10) + sumDigits (parseInt(num/10)); |
| 56 | +} |
| 57 | + |
| 58 | +let num1 = 12345, num2 = 91827; |
| 59 | +console.log(`Sum of digits of ${num1} is ${sumDigits(num1)}`); |
| 60 | +console.log(`Sum of digits of ${num2} is ${sumDigits(num2)}`); |
| 61 | +``` |
| 62 | + |
| 63 | +### Java Implementation |
| 64 | + |
| 65 | +#### [Solution](./Java/SumDigits.java) |
| 66 | + |
| 67 | +```java |
| 68 | +/** |
| 69 | + * @author MadhavBahlMD |
| 70 | + * @date 08/01//2018 |
| 71 | + */ |
| 72 | + |
| 73 | +import java.util.Scanner; |
| 74 | + |
| 75 | +public class SumDigits { |
| 76 | + public static int sum (int num) { |
| 77 | + if (num/10 < 1) |
| 78 | + return num; |
| 79 | + else |
| 80 | + return (num%10) + sum(num/10); |
| 81 | + } |
| 82 | + |
| 83 | + public static void main(String[] args) { |
| 84 | + Scanner input = new Scanner (System.in); |
| 85 | + System.out.println("/* ===== Sum of digits using recursion ===== */"); |
| 86 | + System.out.print("\nEnter a number: "); |
| 87 | + int num = input.nextInt(); |
| 88 | + System.out.println("Sum of digits of " + num + " is: " + sum(num)); |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +<hr /> |
| 94 | + |
| 95 | +## Part B - Product of numbers |
| 96 | + |
| 97 | +### JavaScript Implementation |
| 98 | + |
| 99 | +#### [Solution](./JavaScript/prod_madhav.js) |
| 100 | + |
| 101 | +```js |
| 102 | +/** |
| 103 | + * @author MadhavBahlMD |
| 104 | + * @date 08/01/2018 |
| 105 | + * METHOD - We keep thte second argument (num2) positive and add the first arguement num2(second arg) times |
| 106 | + */ |
| 107 | + |
| 108 | +function recursiveProd (num1, num2) { |
| 109 | + // If num 2 becomes 1, return num1 |
| 110 | + if (num2 === 1) |
| 111 | + return num1; |
| 112 | + |
| 113 | + // If any of the numbers is zero, return 0 |
| 114 | + if (num1 === 0 || num2 === 0) |
| 115 | + return 0; |
| 116 | + |
| 117 | + // If both numbers are less than zero negative signs can be removed |
| 118 | + if (num1 < 0 && num2 < 0) |
| 119 | + return recursiveProd (-1*num1, -1*num2); |
| 120 | + else if (num2 < 0) |
| 121 | + return recursiveProd (num2, num1); |
| 122 | + else |
| 123 | + return num1 + recursiveProd(num1, num2-1); |
| 124 | +} |
| 125 | + |
| 126 | +let n1 = 5, n2 = 10; |
| 127 | +console.log (`${n1} x ${n2} = ${recursiveProd(n1, n2)}`); |
| 128 | +let n3 = -8, n4 = 4; |
| 129 | +console.log (`${n3} x ${n4} = ${recursiveProd(n3, n4)}`); |
| 130 | +let n5 = 2, n6 = -7; |
| 131 | +console.log (`${n5} x ${n6} = ${recursiveProd(n5, n6)}`); |
| 132 | +let n7 = -4, n8 = -7; |
| 133 | +console.log (`${n7} x ${n8} = ${recursiveProd(n7, n8)}`); |
| 134 | +``` |
| 135 | + |
| 136 | +### Java Implementation |
| 137 | + |
| 138 | +#### [Solution](./Java/Product.java) |
| 139 | + |
| 140 | +```java |
| 141 | +/** |
| 142 | + * @author MadhavBahlMD |
| 143 | + * @date 08/01//2018 |
| 144 | + */ |
| 145 | + |
| 146 | +import java.util.Scanner; |
| 147 | + |
| 148 | +public class Product { |
| 149 | + public static int recursiveProd (int num1, int num2) { |
| 150 | + if (num2 == 1) |
| 151 | + return num1; |
| 152 | + |
| 153 | + // If any of the numbers is zero, return 0 |
| 154 | + if (num1 == 0 || num2 == 0) |
| 155 | + return 0; |
| 156 | + |
| 157 | + // If both numbers are less than zero negative signs can be removed |
| 158 | + if (num1 < 0 && num2 < 0) |
| 159 | + return recursiveProd (-1*num1, -1*num2); |
| 160 | + else if (num2 < 0) |
| 161 | + return recursiveProd (num2, num1); |
| 162 | + else |
| 163 | + return num1 + recursiveProd(num1, num2-1); |
| 164 | + } |
| 165 | + |
| 166 | + public static void main(String[] args) { |
| 167 | + Scanner input = new Scanner (System.in); |
| 168 | + System.out.println("/* ===== Product of numbers using recursion ===== */"); |
| 169 | + |
| 170 | + // Take input |
| 171 | + System.out.print("\nEnter first number: "); |
| 172 | + int num1 = input.nextInt(); |
| 173 | + System.out.print("Enter second number: "); |
| 174 | + int num2 = input.nextInt(); |
| 175 | + |
| 176 | + // Print the result |
| 177 | + System.out.println("Product of numbers " + num1 + " and " + num2 + " is: " + recursiveProd(num1, num2)); |
| 178 | + } |
| 179 | +} |
| 180 | +``` |
0 commit comments