-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRecursion-4 solution
More file actions
172 lines (164 loc) · 7.06 KB
/
Recursion-4 solution
File metadata and controls
172 lines (164 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
Q1 - Given a number n. Print if it is an armstrong number or not.
An armstrong number is a number if the sum of every digit in that number raised to the power
of total digits in that number is equal to the number.
(Easy)
Inpu\ 153
Expec\ed Ou\pu\ Yes
Explana\ion
First, we calculated a power function which calculates the power of any number a raised to the power b[
If b = 0, it means that the power to which the number needs to be raised is zero, then the value is equal to 1
and this would be our base case from where we need to terminate this code[
If the power is even then the number can be splitted in half power[
If the power is odd then the number can be splitted in half power and 1 additional ‘a’ value can be multiplied
externally[
Create another function where we pass two parameters n(the original number) and dig(number of digits in
the number) . Now we need every single digit of the number raised to ‘dig’ power. To extract any digit we can
do n%10 this will give us the last digit of the number. But since we only want sum and nature of sum is
commutative, that is in any order we do the sum it is independent of the order[
After extracting the last digit we do not need it any more so we will pass the value n/10 to the next recursive
call and this will be repeated until n does not turn out to be 0. Once n = 0 that means we have touched the
base case where it shows that no digit is left in the original number[
In the main function we calculate the number of digits present in ‘n’. The number of times we are required to
divide n by 10 till it becomes zero indicates the number of digits in that number. Eg. let n = 124
import java.util.*;
import java.util.Scanner;
public class Test{
static int level = -1;
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the number n: ");
int n = scn.nextInt();
int digits = 0;
int temp = n;
while(temp > 0){
digits++;
temp/=10;
}
if(n == isArmstrong(n , digits))
System.out.println("yes");
else
System.out.println("no");
}
public static int pow(int a, int b){
if(b == 0)return 1;
if(b%2 == 0)return pow(a , b/2) * pow(a , b/2);
return a * pow(a , b/2) * pow(a , b/2);
}
public static int isArmstrong(int n , int dig){
if(n == 0)return 0;
return pow(n%10 , dig) + isArmstrong(n/10 , dig);
}
}
Q2 - Given two number x and y find product using recursion. (Easy)
Input t n = 5, y = 2
Expected Output t 10
ExplanatiYns
X If n is lWss than y, swap thW two variablWs valuV
X RWcursivWly find y timWs thW sum of m
X If any of thWm bWcomW zWro, rWturn 0
import java.util.*;
import java.util.Scanner;
public class Test{
static int level = -1;
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the numbers: ");
int x = scn.nextInt();
int y = scn.nextInt();
System.out.println(product(x,y));
}
public static int product(int x, int y) {
// if x is less than y swap the numbers
if (x < y)
return product(y, x);
// iteratively calculate y times sum of x
else if (y != 0)
return (x + product(x, y - 1));
// if any of the two numbers is zero return zero
else
return 0;
}
}
Q3 - Given a number n, check whether it's a prime number or not using recursion. (Easy)
Input t n = 11
Output t YWs
ExplanatiYns
X WW usW thW gWnWral algorithm to chWck if a numbWr is primW ̧
X 1 numbWr is primW if it cannot bW dividWd WntirWly(with rWmaindWr 0) by any numbWr othWr than 1 and itsWlf ̧
X This can bW shortWnWd by dividing thW numbWr from 1 till i whWrW i*i <= n ̧
X WW call a rWcursivW function with n and i as paramWtWrs, i rWprWsWnting thW divisor and initializWd with 2 ̧
X If at any point, n%i bWcomWs 0, wW rWturn falsW as thW numbWr is not primW ̧
X WW stop whWn i*i WncWWds n.
import java.util.*;
import java.util.Scanner;
public class Test{
static int level = -1;
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the number n: ");
int n = scn.nextInt();
if (isPrime(n, 2))
System.out.println("Yes");
else
System.out.println("No");
}
public static boolean isPrime(int n, int i) {
if (n <= 2)
return (n == 2) ? true : false;
if (n % i == 0)
return false;
if (i * i > n)
return true;
// Check for next divisor
return isPrime(n, i + 1);
}
}
Q4 - Given a decimal number as input, we need to write a program to convert the given
decimal number into its equivalent binary number.
import java.util.*;
import java.util.Scanner;
public class Test{
static int level = -1;
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the number n: ");
int n = scn.nextInt();
System.out.println(find(n));
}
public static int find(int decimal_number) {
if (decimal_number == 0)
return 0;
else
return (decimal_number % 2 + 10 *
find(decimal_number / 2));
}
}
Q5 - Given the Binary code of a number as a decimal number, we need to convert this into its
equivalent Gray Code. In gray code, only one bit is changed in 2 consecutive numbers.
import java.util.*;
import java.util.Scanner;
public class Test{
static int level = -1;
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the binary number n: ");
int n = scn.nextInt();
System.out.println(binary_to_gray(n, 0));
}
public static int binary_to_gray(int n, int i) {
int a, b;
int result = 0;
if (n != 0) {
// Taking last digit
a = n % 10;
n = n / 10;
// Taking second last digit
b = n % 10;
if ((a & ~b) == 1 || (~a & b) == 1) {
result = (int)(result + Math.pow(10, i));
}
return binary_to_gray(n, ++i) + result;
}
return 0;
}
}