Replace all ‘0’ with ‘5’ in an input Integer
Given a integer as a input and replace all the ‘0’ with ‘5’ in the integer.
Examples:
102 - 152 1020 - 1525
Use of array to store all digits is not allowed.
Source: Amazon interview Experience | Set 136 (For SDE-T)
The idea is simple, we get the last digit using mod operator ‘%’. If the digit is 0, we replace it with 5, otherwise keep it as it is. Then we recur for remaining digits.
Following is the implementation of the above idea.
C++
// 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. |
chevron_right
filter_none
C
// C program to replace all ‘0’ with ‘5’ in an input Integer #include<stdio.h> // 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 program to test above function int main() { int num = 10120; printf ( "%d" , convert0To5(num)); return 0; } |
chevron_right
filter_none
Java
// Java code for Replace all 0 with 5 in an input Integer class GFG { // A recursive function to replace all 0s with 5s in // an input number. It doesn't work if input number // itself is 0. static int convert0To5Rec( int num) { // Base case 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 static int convert0To5( int num) { if (num == 0 ) return 5 ; else return convert0To5Rec(num); } // Driver function public static void main(String[] args) { System.out.println(convert0To5( 10120 )); } } // This code is contributed by Kamal Rawal |
chevron_right
filter_none
Python
# Python program to replace all 0 with 5 in given integer # A recursive function to replace all 0s with 5s in an integer # Does'nt work if the given number is 0 itself def convert0to5rec(num): # Base case for recurssion termination if num = = 0 : return 0 # Extract the last digit and change it if needed 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 to 5 calls convert0to5rec() for other numbers def convert0to5(num): if num = = 0 : return 5 else : return convert0to5rec(num) # Driver Program num = 10120 print convert0to5(num) # Contributed by Harshit Agrawal |
chevron_right
filter_none
C#
// C# code for Replace all 0 // with 5 in an input Integer using System; class GFG { // A recursive function to replace // all 0s with 5s in an input number. // It doesn't work if input number // itself is 0. static int convert0To5Rec( int num) { // Base case 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 static int convert0To5( int num) { if (num == 0) return 5; else return convert0To5Rec(num); } // Driver Code static public void Main () { Console.Write(convert0To5(10120)); } } // This code is contributed by Raj |
chevron_right
filter_none
PHP
<?php // PHP program to replace all 0 with 5 // in given integer // A recursive function to replace all 0s // with 5s in an integer. Does'nt work if // the given number is 0 itself function convert0to5rec( $num ) { // Base case for recurssion termination if ( $num == 0) return 0; // Extract the last digit and // change it if needed $digit = ( $num % 10); if ( $digit == 0) $digit = 5; // Convert remaining digits and append // the last digit return convert0to5rec((int)( $num / 10)) * 10 + $digit ; } // It handles 0 to 5 calls convert0to5rec() // for other numbers function convert0to5( $num ) { if ( $num == 0) return 5; else return convert0to5rec( $num ); } // Driver Code $num = 10120; print (convert0to5( $num )); // This code is contributed by mits ?> |
chevron_right
filter_none
Output:
15125
This article is contributed by Sai Kiran. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Next greater element in same order as input
- Replace every element of the array by sum of all other elements
- Replace every array element by sum of previous and next
- Replace every element of the array with BitWise XOR of all other
- Replace every element of the array by product of all other elements
- Replace two consecutive equal values with one greater
- Replace duplicates with greater than previous duplicate value
- In-place replace multiple occurrences of a pattern
- Replace every array element by multiplication of previous and next
- Replace the maximum element in the array by coefficient of range
- Replace repeating elements with greater that greatest values
- Replace each node in binary tree with the sum of its inorder predecessor and successor
- Replace even nodes of a doubly linked list with the elements of array
- Blum Integer
- Replace each element by the difference of the total size of the array and frequency of that element