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.

Replace all 0’s with 1 in C++

The program for Implement a C Program to replace all 0’s with 1 in a given integer as an input, all the 0’s in the number has to be replaced with 1.

Algorithm

  • Take Input in num and initialize a variable num with with value 0
  • If num is equals to zero then update the value of num2 to 1
  • Iterate using a while loop until num is greater then 0
  • For each iteration initialize a variable rem and store unit digit of num
  • If rem equals to 0 then set rem to 1
  • Set num to num divide by 10 & num2 equals to num2*10+rem
  • Reverse and print num2
Replace all 0's with 1 in a given integer

C++ code

Run
 #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;

        }

       num = 0 ; // Store the reverse of num2

       while(num2>0){

        int r = num2%10;

        num= num*10 + r;

        num2 /= 10;

      }

        //converted number

        cout<<"Converted number is: "<<num;

        return 0;

    }

Output

Enter number: 12090
Converted number is: 12191

8 comments on “C++ Program to Replace all 0’s with 1 in a given integer”


  • Sonu

    #include

    using namespace std;

    int main()
    {
    int num;
    cin>>num;
    int n=num;
    int i=10;

    while(n!=0){

    if(n%10==0)num=num+i/10;

    n=n/10;
    i=i*10;

    }

    cout<<num;

    return 0;
    }


  • rohangupta029

    #include
    using namespace std;

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

    cout<<s;
    return 0;
    }


  • Ashish

    This code is wrong. It returns reversed string replacing 0 with 1.
    Here is the correct code:

    #include
    using namespace std;

    int calculateAddedValue(int number)
    {

    int result = 0;

    int decimalPlace = 1;

    if (number == 0)
    {
    result += (1 * decimalPlace);
    }

    while (number > 0)
    {
    if (number % 10 == 0)
    {

    result += (1 * decimalPlace);

    }

    number /= 10;
    decimalPlace *= 10;
    }
    return result;
    }

    int replace0with1(int number)
    {
    return number += calculateAddedValue(number);
    }

    int main()
    {
    int num;
    cout<>num;
    cout << replace0with1(num);
    }
    Thank me later!


  • Shubham

    //C++
    #include
    #include
    using namespace std;
    int main()
    {
    string s;
    cin>>s;
    for(int i=0;i<s.length();i++)
    {
    if(s[i]=='0')
    s[i]='1';
    }
    cout<<s<<endl;
    }


  • Shubham

    //C++
    #include
    using namespace std;
    int main()
    {
    int n,n1=0,i=1;
    cin>>n;
    while(n!=0)
    {
    int r=n%10;
    if(r==0)
    {
    r=1;
    }
    n=n/10;
    n1=n1+(r*i);
    i=i*10;
    }
    cout<<n1<<endl;
    }


  • Roshni

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


  • Pankaj Kumar

    /* c++ code for replacing all 0’s with 1 in given integer*/

    #include
    using namespace std;

    int main()
    {
    int n,step=1,rem,res=0;
    // input
    cout<>n;
    while (n!=0)
    {
    rem=n%10;

    if (rem==0)
    {
    // if rem==0 replace it with 1
    rem=1;
    }

    res+=rem*step;
    n/=10;
    step*=10;
    }
    // printing the result
    cout<<res;
    return 0;
    }