### Select Articles

Program to replace all 0's with 1 in a given integer

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

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

#include

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 ;

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 : %dn”, Convert(number));

return 0;

}

C++

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

#include

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 ;

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>

return 0;

}

JAVA 8

// 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 ;

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 3

# 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

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

Enter the number : 80150

The number after replacement : 81151

Time complexity:O(n)