- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Write a program in C++ to replace all the 0’s with 5 in a given number

Given an integer N, the task is to replace all the 0’s that appear in the number with ‘5’. However, the number with leading ‘0’ cannot be replaced with ‘5’ as it remains unchanged. For example,

**Input-1** −

N= 1007

**Output** −

1557

**Explanation** − The given number has 2 zeros which when replaced by ‘5’ results in the output as 1557.

**Input-2** −

N = 00105

**Output** −

155

**Explanation** − Since the given number starts with the leading ‘0’ which can be ignored and the output after replacing the 0 in the middle with ‘5’ results the output as 155.

## Approach to Solve this Problem

To replace all the 0’s in the given number with ‘5’ we can find and extract the last digit of the number. If the last digit of that number is ‘0’ then change and replace the value with ‘5’ and extract another digit. However, any leading ‘0’s in the given number must be ignored.

The problem can be solved using a recursion approach where we will first extract the last digit and then again call the same function while extracting the other digit of that number.

Take Input a number N.

An Integer function converToFive(int N) takes a number as input and returns the modified number by replacing all 0’s with ‘5’.

In a base case, if the number is ‘0’, then return 0, otherwise extract the last digit of that number and check,

If the last digit of the number is ‘0’ then replace the value with ‘5’.

Return the recursive function which takes another digit of the number by dividing ‘10’ and multiplying by ‘10’.

Return the output which extracts the last digit by adding it into it.

## Example

#include <bits/stdc++.h> using namespace std; int convertToFive(int N) { if(N==0){ return 0; } int last_digit= N%10; if(last_digit==0) last_digit=5; return convertToFive(N/10)*10 +last_digit; } int main() { int N= 14006; cout << convertToFive(N) << endl; }

## Output

If we will run the above code, then it will print the output as,

14556

Since there are two 0s in the given number, after replacing the number 14006, it will become 14556.

- Related Questions & Answers
- Write a program in Python to replace all the 0’s with 5 in a given number
- C program to replace all zeros with one in a given integer.
- Write a Python program to shuffle all the elements in a given series
- Python Program to replace list elements within a range with a given number
- Write a program in Python to replace all odd index position in a given series by random uppercase vowels
- C# program to print all the numbers divisible by 3 and 5 for a given number
- Python program to print all the numbers divisible by 3 and 5 for a given number
- Write a program in Python to round all the elements in a given series
- Java Program to replace all occurrences of a given character in a string
- Java Program to replace all occurrences of a given character with new character
- Write a program in Python to count the number of digits in a given number N
- Python program to replace all Characters of a List except the given character
- Write a program in Python to print the power of all the elements in a given series
- Write a program in Python to sort all the elements in a given series in descending order
- Write a Golang program to find the factorial of a given number (Using Recursion)