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.
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.
Question can come like Way 1
Write a code to change all zero's as one's (0s as 1s) in a given number? ex: 120014 needs to be printed as 121114
Question can come like Way 2
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
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
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
#include
#include
int main()
{
int n=80150,sum=0,rem,i=1;
while(n>0)
{
rem=n%10;
if(rem==0)
{
rem=1;
}
sum=sum+rem*i;
i=i*10;
n=n/10;
}
printf(“%d”,sum);
}
n=input()
for i in n:
if i==’0′:
n=n.replace(‘0′,’1’)
print(n)
n=input()
for i in n:
if i==’0′:
n=n.replace(‘0′,’1’)
print(n)
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)
a =input(“Enter an integer: “)
b = list(a)
print(b)
c= []
for i in b:
if (i == ‘0’):
i = ‘1’
c.append(i)
else:
c.append(i)
for i in c:
print(i, end = ” “)
int main()
{
char str[18];
scanf(“%s”,str);
for(int i=0;str[i]!=’\0′;i++)
{
if(str[i]==’0′)
str[i]=’1′;
}
printf(“%s”,str);
}
Python Solution
a = str(int(input()))
print(int(a.replace(‘0′,’1’)))
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;
}
#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;
}
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;
}
Thanks for contributing the code Kavita
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))
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.
good