-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace0TO5.cpp
More file actions
76 lines (67 loc) · 1.4 KB
/
replace0TO5.cpp
File metadata and controls
76 lines (67 loc) · 1.4 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
#include <bits/stdc++.h>
using namespace std;
int convertFive(int n);
/*
// Driver program to test above function
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
cout << convertFive(n) << endl;
}
} //} Driver Code Ends
int convertFive(int n) {
// Your code here
int m=n;
int i=1;
while(m>0)
{
if(m%10==0)
{
n+=(i*5);
}
i*=10;
m/=10;
}
return n;
} */
// C++ program to replace all ‘0’
// with ‘5’ in an input Integer
#include <bits/stdc++.h>
using namespace std;
// A recursive function to replace all 0s
// with 5s in an input number It doesn't
// work if input number itself is 0.
int convert0To5Rec(int num)
{
// Base case for recursion termination
if (num == 0)
return 0;
// Extraxt the last digit and
// change it if needed
int digit = num % 10;
if (digit == 0)
digit = 5;
// Convert remaining digits and
// append the last digit
return convert0To5Rec(num / 10) * 10 + digit;
}
// It handles 0 and calls convert0To5Rec()
// for other numbers
int convert0To5(int num)
{
if (num == 0)
return 5;
else
return convert0To5Rec(num);
}
// Driver code
int main()
{
int num = 10120;
cout << convert0To5(num);
return 0;
}
// This code is contributed by Code_Mech.