Skip to content

Commit 854deb3

Browse files
committed
😴 Replace all 0 with 1 in an input integer
1 parent 4f79d99 commit 854deb3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Given a integer as a input and replace all the β€˜0’ with β€˜5’ in the integer.
3+
* Use of array to store all digits is not allowed.
4+
*/
5+
6+
class ReplaceAll0with5InInputInteger {
7+
8+
public static int recursivelyReplaceDigits(int num){
9+
10+
if(num == 0)
11+
return 0;
12+
int digit = num %10;
13+
if(digit == 0)
14+
digit = 5;
15+
return recursivelyReplaceDigits(num/10)*10+digit;
16+
}
17+
18+
public static int replaceDigits(int num){
19+
if(num==0)
20+
return 5;
21+
return recursivelyReplaceDigits(num);
22+
}
23+
24+
public static void main(String [] args){
25+
System.out.println(replaceDigits(1230450));
26+
System.out.println(replaceDigits(0));
27+
System.out.println(replaceDigits(50));
28+
}
29+
}

0 commit comments

Comments
Β (0)