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

Replace all 0’s with 1 in a given integer:-

The replace all program in C programming works to replace the numbers with zero, where the number must be an integer. All the zeros (if encountered) in the given program will be replaced by 1.

Ex- number is 12004509 all 0’s are replays of 1’s so number is 12114519.

Replace all 0’s with 1 in a given integer in C programming

Working:-

Step 1: Start

Step 2: The user is asked to insert an integer value as an input

Step 3: Navigate the inserted integer digit by digit

Step 4: If 0 is found, then replace it with 1, and print the integer variable

Step 5: Stop

C Program :-

// C program to replace all 0’s with 1 in a given integer
#include  
int replace (long int num)
{
     // Base case for recursion termination
     if (num == 0)
        return 0;
     // Extract the last digit and change it if needed
     int digit = num % 10;
     if (digit == 0)
         digit = 1;
     // Convert remaining digits and append the last digit

     return replace(num/10) * 10 + digit;
}
int Convert(long int num)
{
     if (num == 0)
        return 1;
     else
       return replace(num);
}
int main()
{
      long int num;
//To take user input printf("\nInsert the num : "); scanf("%d", &number); //display final result
printf
("\n Num after replacement : %d\n", Convert (num)); return 0; }

Output

Insert the num: 80150

Num after replacement = 81151

Above is 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.

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


  • Vikas

    n=int(str(input())[::-1])
    a=0
    while(n>1):
    r=n%10
    if r==0:
    a=a*10+1
    else:
    a=a*10+r
    n=n//10
    print(a)


  • ritsika

    can we use array instead???????
    #include

    int main ()
    {
    int N[7];
    for (int i = 0; i < 7; i++)
    {
    scanf ("%d", &N[i]);
    }
    for (int i = 0; i < 7; i++)
    {
    if (N[i] == 0)
    N[i] = 1;

    }
    for (int i = 0; i < 7; i++)
    {
    printf("%d", N[i]);
    }
    return 0;
    }


  • Harshit

    #include

    using namespace std;

    int main(){

    int n;
    cin >> n;

    int m{0};
    int i = 1;
    while(n != 0){
    int digit = n%10;
    if (digit == 0) digit = 1;
    m = m + digit*i;
    i = i*10;
    n = n/10;
    }

    cout << m << endl;
    }


  • Kavita

    using namespace std;

    int main()
    {
    int x,y;
    cin>>x;
    int rem,i,bin;
    int sum;
    sum=0;i=1;
    while(x!=0)
    {
    rem=x%10;
    if(rem==0)
    {
    rem=1;
    }
    sum=sum+rem*i;
    i=i*10;;
    x=x/10;
    }

    cout<<sum;

    return 0;
    }


  • Bhoma

    def replaces(num):
    if (num==0):
    return 0

    #check last digit
    digit = num%10
    if (digit==0):
    digit = 1
    #else:
    return replaces(num//10)*10 + digit

    num = int(input(‘enter number: ‘))
    print(‘result are: ‘ , replaces(num))


    • HelpPrepInsta

      Thanks for the code Bhoma, you seems to be a active prepster, please continue learning with us, we’ll help you in all the possible ways we can.