# Program to replace all 0’s with 1 in a given integer | Faceprep

Program to replace all 0’s with 1 in a given integer is discussed here. Given an integer as an input, all the 0’s in the number has to be replaced with 1.

For example, consider the following number

Input: 102405
Output: 112415

Input: 56004
Output: 56114

## Algorithm to replace all 0’s with 1 in a given integer

• Input the integer from the user.
• Traverse the integer digit by digit.
• If a ‘0’ is encountered, replace it by ‘1’.
• Print the integer.

Program to replace all 0’s with 1 in a given integer is given below.

// C program to replace all 0’s with 1 in a given integer

#include<stdio.h>
int replace(long int number)
{
// Base case for recursion termination
if (number == 0)
return 0;

// Extract the last digit and change it if needed
int digit = number % 10;
if (digit == 0)
digit = 1;

// Convert remaining digits and append the last digit
return replace(number/10) * 10 + digit;
}
int Convert(long int number)
{
if (number == 0)
return 1;
else
return replace(number);
}
int main()
{
long int number;
printf(“\nEnter the number : “);
scanf(“%d”, &number);
printf(“\nNumber after replacement : %d\n”, Convert(number));
return 0;
}

// C++ program to replace all 0’s with 1 in a given integer

#include<iostream>
using namespace std;

int replace(long int number)
{
// Base case for recursion termination
if (number == 0)
return 0;

// Extract the last digit and change it if needed
int digit = number % 10;
if (digit == 0)
digit = 1;

// Convert remaining digits and append the last digit
return replace(number/10) * 10 + digit;
}

int Convert(long int number)
{
if (number == 0)
return 1;
else
return replace(number);
}

int main()
{
long int number;
cout << “\nEnter the number : “;
cin >> number;
cout << “\nNumber after replacement : ” << Convert(number) << endl;
return 0;
}

// Java program to replace all 0’s with 1 in a given integer

import java.util.*;
public class Main
{
static int replace(int number)
{
// Base case for recursion termination
if (number == 0)
return 0;

// Extract the last digit and change it if needed
int digit = number % 10;
if (digit == 0)
digit = 1;

// Convert remaining digits and append the last digit
return replace(number/10) * 10 + digit;
}

static int Convert(int number)
{
if (number == 0)
return 1;
else
return replace(number);
}

public static void main(String[] args)
{
int number;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the number : “);
number = sc.nextInt();
System.out.print(“\nNumber after replacement : “);
System.out.print(replace(number));
}
}

# Python  program to replace all 0’s with 1 in a given integer

def replace(number):
if (number == 0):
return 0;

# Extract the last digit and change it if needed
digit = number % 10
if (digit == 0):
digit = 1

# Convert remaining digits and append the last digit
return replace(int(number/10)) * 10 + digit

number = int(input(“Enter the number : “))
print(“Number after replacement : “,replace(number))

Output: Time complexity: O(n)