Replace All 0’s With 1 In A Given Integer | C Program

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 with 1

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 C

C code

Run
#include<stdio.h>

    //main program

    int main()

    {

        int num,num2=0;

        printf("Enter number: ");

        //user input

        scanf("%d", &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

        printf("Converted number is: %d" ,num);

        return 0;

    }

Output

Enter number: 900120678
Converted number is: 911121678

17 comments on “Replace All 0’s With 1 In A Given Integer | C Program”


  • 18-558

    #include
    int main()
    {
    int s,base=1,sum=0,n;
    scanf(“%d”,&n);
    while(n>0){
    s=n%10;
    if(s==0)
    {
    s=1;
    }
    n=n/10;
    sum=sum+s*base;
    base*=10;
    }
    printf(“%d”,sum);

    }


  • Shashi Kiran

    #Solution in Python
    def replace_0(num):
    temp = ”
    for i in str(num):
    i = str(i)

    if i == ‘0’:
    temp += ‘1’
    else:
    temp += i
    return int(temp)
    Code complexity:
    O(n) Time, O(n) space


  • Gunaseelan

    #include

    int main() {
    int n,r,d=0,i=1;
    scanf(“%d”,&n);
    if(n==0)
    printf (“1”);
    else
    while (n>0)
    {
    r=n%10;
    if(r==0)
    {
    r=1;
    }
    d=d+r*i;
    n=n/10;
    i=i*10;
    }
    printf (“%d”,d);
    return 0;
    }