C++ Program to Convert all 0 with 5














































C++ Program to Convert all 0 with 5



Convert all 0 with 5

You are given an integer N. You need to convert all zeroes of N to 5 but without using extra space.

The Approach is to take the last digit using modulus and check if the number is 0 then add 5 else  return function(n%10 *10) + remainder

Time Complexity: O(n)
Space Complexity: O(1)

Code
#include<iostream>
using namespace std;

int convert_to_five(int n) {
    
if(n == 0)
return n;
int rem = (n%10) ? (n%10) : 5;
return ( convertFive(n/10)*10 + rem); } int main(){
int n = 1005;
cout << convert_to_five(n);
return 0; }

Output:

1555


Comments