Write a program to Replace all 0’s with 1 in a given integer

28 September 2022

Write a program to Replace all 0’s with 1 in a given integer


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

 

Get a number as input from the user and find the zeros present in that number. Then convert the zeros into one and then print it.

Input

310020

Output

311121

#include <stdio.h>

int main()

    {

        int num,newnum=0;

        printf("Enter number: ");

        scanf("%d", &num);

        if(num == 0)

            newnum=1;

        while(num>0)

        {

            int rem = num%10;

            if(rem == 0)

                rem = 1;

            num = num/10;

            newnum=newnum*10+rem;

        }

       num = 0 ;

       while(newnum>0)

       {

        int r = newnum%10;

        num= num*10 + r;

        newnum =newnum / 10;

       }

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

        return 0;

    }

C++ 

#include <iostream>

using namespace std;

int main()

    {

        int num,newnum=0;

        cout<<"Enter number: ";

        cin>>num;

        if(num == 0)

            newnum=1;

        while(num>0)

        {

            int rem = num%10;

            if(rem == 0)

                rem = 1;

            num = num/10;

            newnum=newnum*10+rem;

        }

       num = 0 ;

       while(newnum>0)

       {

        int r = newnum%10;

        num= num*10 + r;

        newnum =newnum / 10;

       }

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

        return 0;

    }

Java 

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

int newnum=0;

if(num == 0)

            newnum=1;

        while(num>0)

        {

            int rem = num%10;

            if(rem == 0)

                rem = 1;

            num = num/10;

            newnum=newnum*10+rem;

        }

       num = 0 ;

       while(newnum>0)

       {

        int r = newnum%10;

        num= num*10 + r;

        newnum =newnum / 10;

       }

        System.out.println("New number is: "+num);

}

}

Python

num=int(input("Enter a number: "))

s=str(num)

new=[]

for i in s:

    if(i=='0'):

        new.append('1')

    else:

        new.append(i)

newnum=""

for i in new:

    newnum+=i

print(int(newnum))


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !