Replace all 0's with 5 | Practice | GeeksforGeeks

Given a number N. The task is to complete the function convertFive() which replace all zeros in the number with 5 and returns the number.
Input:
The first line of input contains an integer T, denoting the number of testcases. Then T testcases follow. Each testcase contains a single integer N denoting the number.
Output:
The function will return integer where all zero's are converted to 5.
User Task:
Since this is a functional problem you don't have to worry about input, you just have to complete the function convertFive().
Constraints:
1 <= T <= 103
1 <= N <= 104
Example:
Input
2
1004
121
Output
1554
121
Explanation:
Testcase 1:  At index 1 and 2 there is 0 so we replace it with 5.
Testcase 2: There is no ,0 so output will remain same.
0
heyankit22 minutes ago
simple solution in python
def convertFive(self,n):       new_str = str(n)       return int(new_str.replace("0","5"))
0
yashvardhanyadav82 days ago
int convertFive(int n)   {       int num=0,i=0;       while (n!=0){           if (n%10==0){               num = num + 5*pow(10,i);               n = n/10;               i++;           }           else{               num = num + (n%10)*pow(10,i);               n = n/10;               i++;           }       }       return num;   }
0
sanskarnaredi0075 days ago
public static int convertFive(int n){   //add code here.      String str = Integer.toString(n);      String str1 = str.replace("0","5");      n = Integer.parseInt(str1);      return n;   }
0
sanskarnaredi007
This comment was deleted.
0
vishalsinghrajput1 week ago
def convertFive(self,n):
        ss= str(n)
        ss2=[]
        ss3=""
        for i in range(len(ss)):
            if ss[i]=='0':
                ss2.insert(i,'5')
             
            else:
                ss2.insert(i,ss[i])
        for i in range(len(ss2)):
            ss3+=ss2[i]
        return int(ss3)
0
ritikraaj771 week ago
Without using String just MATH
class Solution{
	public static int convertFive(int n){
    //add code here.
    int r = 0,s= 0;
    int c = 0,d = 0;
    int t = n;
    while(n>0){
        r=n%10;
        if(r==0){
            if(c!=0){
                s=s+10*c*5;
                c=c*10;
            }
            else if(c==0){
                s=s+5;
                c=c+1;
            }
        }
        else if(r!=0){
          if(c!=0){
              s=s+r*10*c;
              c=c*10;
          }
          else if(c==0){
              s=s+r;
              c=c+1;
          }
        }
    n=n/10;
    }
    return s;
    }
}
0
tarunyarram98
This comment was deleted.
0
monsotal3 weeks ago
JAVA
 
class Solution{public static int convertFive(int n){       String number = String.valueOf(n);       String newnumber = "";       for(int i=0 ; i< number.length() ; i++){           // check if current index equal to ASCII value of the number 0(48).           if(number.charAt(i) == 48){               newnumber += "5";           }           else{               newnumber += number.charAt(i);           }       }       int parsednumber = Integer.parseInt(newnumber);       return parsednumber;   } 
0
prateekkasaudhan1233 weeks ago
//  java solutions
class Solution{public static int convertFive(int n){   String s = Integer.toString(n);   String newS = "";   for(int i=0; i<s.length(); i++){       if(s.charAt(i)=='0')           newS += '5';       else           newS += s.charAt(i);   }   int ans = Integer.parseInt(newS);   return ans;   }}
0
aravindmn75833 weeks ago
simple solution in c++_____
by converting from int to string after that if char is “0” replace with “5”  return string to int____
int conversion(int n){
	string s =to_string(n);
	for(int i=0; i<s.length(); i++){
		if(s[i]=='0'){
			s[i]='5';
		}
	}
	return n = stoi(s);
}


 
 
 
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.