

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to replace all zeros with one in a given integer.
Problem
Write a program to replace all zeros (0's) with 1 in a given integer.
Given an integer as an input, all the 0's in the number has to be replaced with 1.
Solution
Consider an example given below −
Here, the input is 102410 and the output is 112411.
Algorithm
Refer an algorithm given below to replace all the 0’s to 1 in an integer.
Step 1 − Input the integer from the user.
Step 2 − Traverse the integer digit by digit.
Step 3 − If a '0' is encountered, replace it by '1'.
Step 4 − Print the integer.
Example
Given below is the C program to replace all 0's with 1 in a given integer −
#include<stdio.h> int replace(long int number){ if (number == 0) return 0; //check last digit and change it if needed int digit = number % 10; if (digit == 0) digit = 1; // Convert remaining digits and append to its last digit return replace(number/10) * 10 + digit; } int Convert(long int number){ if (number == 0) return 1; else return replace(number); } int main(){ long int number; printf("\nEnter any number : "); scanf("%d", &number); printf("\nAfter replacement the number is : %dn", Convert(number)); return 0; }
Output
When the above program is executed, it produces the following output −
Enter any number: 1056110010 After replacement the number is: 1156111111
- Related Questions & Answers
- Java Program to replace all occurrences of given String with new one
- Java Program to replace all occurrences of a given character with new character
- C# program to print all distinct elements of a given integer array in C#
- Write a program in C++ to replace all the 0’s with 5 in a given number
- Java Program to replace only first occurrences of given String with new one
- Java Program to replace all occurrences of a given character in a string
- C# program to replace all spaces in a string with ‘%20’
- Multiply a given Integer with 3.5 in C++
- Java Program to replace one specific character with another
- Write a program in Python to replace all the 0’s with 5 in a given number
- Python program to print all distinct elements of a given integer array.
- Left pad an integer in Java with zeros
- Python program to replace all Characters of a List except the given character
- Print All Distinct Elements of a given integer array in C++
- C program to replace all occurrence of a character in a string
Advertisements