 Given a integer as a input and replace all the ‘0’ with ‘5’ in the integer

Input Format

Enter an integer n

Constraints

0<=n<=1000000000000

Output Format

All zeroes are replaced with 5

Sample Input

102

Sample Output

152

Explanation

Check each digit , if it is nonzero, then no change required but if it is zero then replace it by 5.

If you are using C you can take in the input in form of character by using getchar() in a while or for loop, then just use the ternary operator( ?: ) to print 5 if 0 comes and the number otherwise.

You can use take input in a string and then compare each character in string with ‘0’, if true replace with ‘5’ else do nothing.

``````#include <bits/stdc++.h>
using namespace std;

int main() {
string s;
cin>>s;
for(int i=0; i<s.size(); i++)
if(s[i] == '0')
s[i] = '5';

cout<<s<<'\n';
return 0;
}``````
2 Likes

@shivam_gupta14 As the input size you are taking is 10^12 taking it in int data type will exceed its range…you can alternatively use long long int but that would also make the code complicated as you need to check for each digit.
The best way is to you can input the data in string or array of characters and just traverse the array using loop to check each element if it is equal to ‘0’(character) or not… 2 Likes

if you are using python ,the best way is to replace

x=str(input(‘enter the number’))
print(x.replace(‘0’,‘5’))

1 Like