











C++ Program to Replace all 0’s with 1 in a given integer
Program to Replace all 0’s with 1 in a given integer
Here we will discuss how to replace all the 0’s with 1 in a given integer using C++ programming language.
The concept is simple, find the digits of the integer. Compare each digit with 0 if the digit is equal to 0 then replace it with 1.
Construct the new integer with the replaced digits.




Algorithm:-
- User gives an input.
- The input is stored in an int type variable say num.
- A loop is started
- Digits of the num is founded and stored in another variable say rem
int rem = num%10;
2. Each digit is compared with 0
- If the digit is equal to 0 then replace it with 1
if(rem == 0)
rem = 1;
3. Construct the new number digit by digit.
new=new*10+rem;
4. Print the output.
C++ Code:-
//C++ Program
//Convert all 0’s to 1
#include<iostream>
using namespace std;
//main program
int main()
{
int num,num2=0;
cout<<“Enter number: “;
//user input
cin>>num;
//checking for 0 input
if(num == 0)
num2=1;
//converting 0 to 1
while(num>0)
{
int rem = num%10;
if(rem == 0)
rem = 1;
num = num/10;
num2=num2*10+rem;
}
//converted number
cout<<“Converted number is: “<<num2;
return 0;
}
Output
Enter number: 101011
Converted number is: 111111


- Highest Common Factor(HCF): C | C++ | Java | Python
- Lowest Common Multiple (LCM) : C | C++ | Java | Python
- Greatest Common Divisor : C | C++ | Java | Python
- Binary to Decimal to conversion : C | C++ | Java | Python
- Binary to Octal conversion : C | C++ | Java | Python
- Decimal to Binary conversion: C | C++ | Java | Python
- Decimal to octal Conversion: C | C++ | Java | Python
- Octal to Binary conversion : C | C++ | Java | Python
- Octal to Decimal conversion : C | C++ | Java | Python
- Quadrants in which a given coordinate lies : C | C++ | Java | Python
- Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
- Maximum number of handshakes: C | C++ | Java | Python
- Addition of two fractions: C | C++ | Java | Python
- Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
- Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
- Count possible decoding of a given digit sequence : C | C++ | Java
- Check whether a character is a vowel or consonant : C | C++ | Java | Python
- Check whether a character is a alphabet or not : C | C++ | Java | Python
- Calculate the area of a circle : C | C++ | Java | Python
- Find the ASCII value of a character : C | C++ | Java | Python
- Find the prime numbers between 1 to 100 : C | C++ | Java | Python
- Calculate the number of digits in an integer : C | C++ | Java | Python
- Convert digit/number to words : C | C++ | Java | Python
- Counting number of days in a given month of a year: C | C++ | Java | Python
- Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
- Finding number of integers which has exactly x divisors: C | C++ | Java | Python
- Finding Roots of a quadratic equation : C | C++ | Java | Python
//C++ Program
//Convert all 0’s to 1
#include
using namespace std;
//main program
int main()
{
int num,num2=0,i=1;
cout<>num;
//checking for 0 input
if(num == 0)
num2=1;
//converting 0 to 1
while(num>0)
{
int rem = num%10;
if(rem == 0)
rem = 1;
num = num/10;
num2=num2+rem*i;
i *=10;
}
//converted number
cout<<"Converted number is: "<<num2;
return 0;
}